如何使用TStreamWriter和TStreamReader读写PDF文件?
这是我的代码:
procedure TForm1.Button2Click(Sender: TObject);
var
Reader: TStreamReader;
Writer: TStreamWriter;
begin
Reader := TStreamReader.Create('D:\Downloads\cover.pdf', TEncoding.UTF8, False);
try
Writer := TStreamWriter.Create('D:\Downloads\coverb.pdf', False, TEncoding.UTF8);
try
Writer.Write(Reader.ReadToEnd());
finally
Writer.Free;
ShowMessage('Berhasil');
end;
finally
Reader.Free();
end;
end;
使用上面的代码,Reader.ReadToEnd()
,我没有得到任何字符串,并且coverb.pdf是空的。
我正在使用德尔福XE。
Here is my code:
procedure TForm1.Button2Click(Sender: TObject);
var
Reader: TStreamReader;
Writer: TStreamWriter;
begin
Reader := TStreamReader.Create('D:\Downloads\cover.pdf', TEncoding.UTF8, False);
try
Writer := TStreamWriter.Create('D:\Downloads\coverb.pdf', False, TEncoding.UTF8);
try
Writer.Write(Reader.ReadToEnd());
finally
Writer.Free;
ShowMessage('Berhasil');
end;
finally
Reader.Free();
end;
end;
Using the above code, Reader.ReadToEnd()
, I got no string, and coverb.pdf is empty.
I'm using Delphi XE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PDF 文件通常是压缩的二进制文件,因此无法以 UTF8 格式读取。这样做会导致编解码器错误。请记住,并非所有字节序列都是有效的 UTF8 序列。
看起来您只需要调用 CopyFile 而不是复杂的基于流的代码,但这也许只是一个简化的示例。
PDF files are generally compressed binary files and so cannot be read as UTF8. Doing so will lead to codec errors. Remember that not all sequences of bytes are valid UTF8 sequences.
It looks like you just need to call CopyFile instead of your complex stream based code, but perhaps this is just a cut down sample.
如果文件不为空,但 ReadToEnd() 返回空字符串,则用于将文件字节解码为 Unicode 的 TEncoding 对象遇到转换错误。 RTL 不会引发字符串转换错误的异常。如果您只想制作文件的精确副本,请使用
CopyFile()
,或使用TFileStream
和TStream.CopyFrom()
> 方法。If the file is not empty but
ReadToEnd()
is returning an empty string, then theTEncoding
object being used to decode the file bytes into Unicode is encountering conversion errors. The RTL does not raise an exception on string conversion errors. If all you want to do is make an exact copy of the file, useCopyFile()
, or useTFileStream
and theTStream.CopyFrom()
method.您可以使用 Embarcadero 的 ReadAllText 函数。像这样:
它将正确检测 ANSI、Unicode 和二进制文件。
You can use Embarcadero's ReadAllText function. Like this:
It will correctly detect ANSI, Unicode and binary files.
您需要手动编写一份新的 PDF 文件吗?在这种情况下,您需要了解 PDF 文件的格式结构。使用 ISO 32000-2 格式化 2.0 版,因此可以使用二进制流构建 PDF 文件,但可以肯定的是,如果您使用一些现成的组件会更容易...
以下是如何手动执行的一个示例:https://blogs.embarcadero.com/how-to-create-a-pdf-file-with-delphi-and-add-an-image-to-it/(这个例子非常简单,所以不要在文件内使用压缩)
但我建议库为 GDPicture 或 Gnostice...
Do you need to write one new PDF file manually? In this case you need to know the struct of format to PDF files. Use the ISO 32000-2 to format version 2.0, so is possible to build with binary streams your PDF file, but with sure if you use some ready components will be more easy...
Here is one example how to do manually: https://blogs.embarcadero.com/how-to-create-a-pdf-file-with-delphi-and-add-an-image-to-it/ (this example is very simple, so don't use compress inside file)
But I sugest lybraries as GDPicture or Gnostice...