在内存中创建PDF而不是物理文件

时间:2010-05-12 02:17:55

标签: c# itextsharp

如何使用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

4 个答案:

答案 0 :(得分:32)

使用内存流切换文件流。

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

答案 1 :(得分:21)

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();
}

答案 2 :(得分:17)

我之前从未使用过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();
}

答案 3 :(得分:8)

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

然后在您写完MemoryStream之后致电byte[]以获得using (MemoryStream output = new MemoryStream()) { PdfWriter wri = PdfWriter.GetInstance(doc, output); // Write to document // ... return output.ToArray(); }

IDisposable

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