iTextSharp + FileStream = 损坏的 PDF 文件
我正在尝试使用 iTextSharp 创建 pdf 文件。我尝试将 pdf 的内容写入 MemoryStream,以便我可以将结果写入文件和数据库 BLOB。该文件已创建,大小约为 21kB,用 Notepad++ 打开时看起来像 pdf。但我的 PDF 查看器说它已损坏。 这是一个小代码片段(仅尝试写入文件,而不是数据库):
Document myDocument = new Document();
MemoryStream myMemoryStream = new MemoryStream();
PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);
myDocument.Open();
// Content of the pdf gets inserted here
using (FileStream fs = File.Create("D:\\...\\aTestFile.pdf"))
{
myMemoryStream.WriteTo(fs);
}
myMemoryStream.Close();
我犯的错误在哪里?
谢谢你, 诺伯特
I am trying to create a pdf file with iTextSharp. My attempt writes the content of the pdf to a MemoryStream so I can write the result both into file and a database BLOB. The file gets created, has a size of about 21kB and it looks like a pdf when opend with Notepad++. But my PDF viewer says it's currupted.
Here is a little code snippet (only tries to write to a file, not to a database):
Document myDocument = new Document();
MemoryStream myMemoryStream = new MemoryStream();
PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);
myDocument.Open();
// Content of the pdf gets inserted here
using (FileStream fs = File.Create("D:\\...\\aTestFile.pdf"))
{
myMemoryStream.WriteTo(fs);
}
myMemoryStream.Close();
Where is the mistake I make?
Thank you,
Norbert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您的问题是您没有正确地将内容添加到 PDF 中。这是通过 Document.Add() 方法完成的,然后通过调用 Document.Close() 来完成。
但是,当您调用 Document.Close() 时,您的 MemoryStream 也会关闭,因此您将无法像以前那样将其写入 FileStream。您可以通过将 MemoryStream 的内容存储到字节数组来解决此问题。
以下代码片段对我有用:
I think your problem was that you weren't properly adding content to your PDF. This is done through the Document.Add() method and you finish up by calling Document.Close().
When you call Document.Close() however, your MemoryStream also closes so you won't be able to write it to your FileStream as you have. You can get around this by storing the content of your MemoryStream to a byte array.
The following code snippet works for me:
我有类似的问题。我的文件已下载,但文件大小为 13 字节。当我使用二进制编写器写入文件时解决了这个问题
I had similar issue. My file gets downloaded but the file size will be 13Bytes. I resolved the issue when I used binary writer to write my file
只是一些想法 - 如果用文件流替换内存流会发生什么?这能给你你需要的结果吗?这至少会告诉您问题可能出在哪里。
如果这确实有效,那么文件有何不同(大小和二进制表示形式)?
只是猜测,但是您是否尝试在写入之前查找内存流的开头?
Just some thoughts - what happens if you replace the memory stream with a file stream? Does this give you the result you need? This will at least tell you where the problem could be.
If this does work, how do the files differ (in size and binary representation)?
Just a guess, but have you tried seeking to the beginning of the memory stream before writing?
尝试仔细检查使用 iText 操作 PDF 的代码。确保调用任何 PdfContentByte 对象的相应 EndText 方法,并确保在将文件写入磁盘之前调用 myDocument.Close()。这些是我过去在使用 iTextSharp 生成 PDF 时遇到的问题。
Try double checking your code that manipulates the PDF with iText. Make sure you're calling the appropriate EndText method of any PdfContentByte objects, and make sure you call myDocument.Close() before writing the file to disk. Those are things I've had problems with in the past when generating PDFs with iTextSharp.