iTextSharp + FileStream = 损坏的 PDF 文件

发布于 2024-08-20 08:44:59 字数 577 浏览 15 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕梦 2024-08-27 08:44:59

我认为您的问题是您没有正确地将内容添加到 PDF 中。这是通过 Document.Add() 方法完成的,然后通过调用 Document.Close() 来完成。

但是,当您调用 Document.Close() 时,您的 MemoryStream 也会关闭,因此您将无法像以前那样将其写入 FileStream。您可以通过将 MemoryStream 的内容存储到字节数组来解决此问题。

以下代码片段对我有用:

using (MemoryStream myMemoryStream = new MemoryStream()) {
    Document myDocument = new Document();
    PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);

    myDocument.Open();

    // Add to content to your PDF here...
    myDocument.Add(new Paragraph("I hope this works for you."));

    // We're done adding stuff to our PDF.
    myDocument.Close();

    byte[] content = myMemoryStream.ToArray();

    // Write out PDF from memory stream.
    using (FileStream fs = File.Create("aTestFile.pdf")) {
        fs.Write(content, 0, (int)content.Length);
    }
}

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:

using (MemoryStream myMemoryStream = new MemoryStream()) {
    Document myDocument = new Document();
    PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);

    myDocument.Open();

    // Add to content to your PDF here...
    myDocument.Add(new Paragraph("I hope this works for you."));

    // We're done adding stuff to our PDF.
    myDocument.Close();

    byte[] content = myMemoryStream.ToArray();

    // Write out PDF from memory stream.
    using (FileStream fs = File.Create("aTestFile.pdf")) {
        fs.Write(content, 0, (int)content.Length);
    }
}
在巴黎塔顶看东京樱花 2024-08-27 08:44:59

我有类似的问题。我的文件已下载,但文件大小为 13 字节。当我使用二进制编写器写入文件时解决了这个问题

byte[] bytes = new byte[0];
//pass in your API response into the bytes initialized

using (StreamWriter streamWriter = new StreamWriter(FilePath, true))
{
   BinaryWriter binaryWriter = new BinaryWriter(streamWriter.BaseStream);
   binaryWriter.Write(bytes);
}

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

byte[] bytes = new byte[0];
//pass in your API response into the bytes initialized

using (StreamWriter streamWriter = new StreamWriter(FilePath, true))
{
   BinaryWriter binaryWriter = new BinaryWriter(streamWriter.BaseStream);
   binaryWriter.Write(bytes);
}
め七分饶幸 2024-08-27 08:44:59

只是一些想法 - 如果用文件流替换内存流会发生什么?这能给你你需要的结果吗?这至少会告诉您问题可能出在哪里。

如果这确实有效,那么文件有何不同(大小和二进制表示形式)?

只是猜测,但是您是否尝试在写入之前查找内存流的开头?

myMemoryStream.Seek(0, SeekOrigin.Begin);

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?

myMemoryStream.Seek(0, SeekOrigin.Begin);
往事风中埋 2024-08-27 08:44:59

尝试仔细检查使用 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.

就像说晚安 2024-08-27 08:44:59
documentobject.Close();
using (FileStream fs = System.IO.File.Create(path)){                        
    Memorystreamobject.WriteTo(fs);
}         
documentobject.Close();
using (FileStream fs = System.IO.File.Create(path)){                        
    Memorystreamobject.WriteTo(fs);
}         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文