【问题标题】:Create PDF in memory instead of physical file在内存中创建 PDF 而不是物理文件
【发布时间】:2011-02-18 10:26:29
【问题描述】:

如何使用 itextsharp 在内存流中创建 PDF 而不是物理文件。

下面的代码正在创建实际的 pdf 文件。

相反,我如何创建一个 byte[] 并将其存储在 byte[] 中,以便我可以通过函数返回它

using iTextSharp.text;
using iTextSharp.text.pdf;
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create));
doc.Open();//Open Document to write
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");

doc.Add(paragraph);

doc.Add(pharse);

doc.Add(chunk);
doc.Close(); //Close document

【问题讨论】:

标签: c# itextsharp


【解决方案1】:

如果您的代码有 new FileStream,则传入您已经创建的 MemoryStream。 (不要只是在对 PdfWriter.GetInstance 的调用中内联创建它 - 您希望以后能够引用它。)

然后在您写完信后在MemoryStream 上调用ToArray() 以获得byte[]

using (MemoryStream output = new MemoryStream())
{
    PdfWriter wri = PdfWriter.GetInstance(doc, output);

    // Write to document
    // ...
    return output.ToArray();
}

我没有使用过 iTextSharp,但我怀疑其中一些类型实现了 IDisposable - 在这种情况下,您也应该在 using 语句中创建它们。

【讨论】:

    【解决方案2】:
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
    
    byte[] pdfBytes;
    using(var mem = new MemoryStream())
    {
        using(PdfWriter wri = PdfWriter.GetInstance(doc, mem)) 
        {
            doc.Open();//Open Document to write
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");
    
            doc.Add(paragraph);
    
            doc.Add(pharse);
    
            doc.Add(chunk); 
        }
        pdfBytes = mem.ToArray();
    }
    

    【讨论】:

    • PdfWriter 没有实现 IDisposable 所以你不能在 using 语句中使用它。也许这只是我正在使用的版本(5.0.5),因为我知道版本 4 有一些类更改
    • @musefan,是的,在 5.0.5 中就是这样。在当前版本 5.5 中,PdfWriter 扩展了 DocWriter,它实现了 IDocListener,它扩展了 IDisposable。我不知道IDisposable 何时添加到IDocListener 的确切时间,但那是在 5.0.5 之后和 5.5.0 之前的一段时间。
    【解决方案3】:

    用内存流切换文件流。

    MemoryStream memStream = new MemoryStream();
    PdfWriter wri = PdfWriter.GetInstance(doc, memStream);
    ...
    return memStream.ToArray();
    

    【讨论】:

    • 如何在不创建文件的情况下让 memStream 的内容显示在 PDF 阅读器中?
    【解决方案4】:

    我以前从未使用过 iTextPDF,但它听起来很有趣,所以我接受了挑战并自己做了一些研究。下面介绍如何通过内存流式传输 PDF 文档。

    protected void Page_Load(object sender, EventArgs e)
    {
        ShowPdf(CreatePDF2());
    }
    
    private byte[] CreatePDF2()
    {
        Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
    
        using (MemoryStream output = new MemoryStream())
        {
            PdfWriter wri = PdfWriter.GetInstance(doc, output);
            doc.Open();
    
            Paragraph header = new Paragraph("My Document") {Alignment = Element.ALIGN_CENTER};
            Paragraph paragraph = new Paragraph("Testing the iText pdf.");
            Phrase phrase = new Phrase("This is a phrase but testing some formatting also. \nNew line here.");
            Chunk chunk = new Chunk("This is a chunk.");
    
            doc.Add(header);
            doc.Add(paragraph);
            doc.Add(phrase);
            doc.Add(chunk);
    
            doc.Close();
            return output.ToArray();
        }
    
    }
    
    private void ShowPdf(byte[] strS)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
    
        Response.BinaryWrite(strS);
        Response.End();
        Response.Flush();
        Response.Clear();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 2020-11-11
      • 2015-03-04
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2011-07-24
      相关资源
      最近更新 更多