简体   繁体   English

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

[英]Create PDF in memory instead of physical file

How do one create PDF in memorystream instead of physical file using itextsharp. 如何使用itextsharp在内存流中而不是物理文件中创建PDF。

The code below is creating actual pdf file. 下面的代码创建实际的pdf文件。

Instead how can I create a byte[] and store it in the byte[] so that I can return it through a function 相反,我如何创建一个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

Switch the filestream with a memorystream. 用内存流切换文件流。

MemoryStream memStream = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, memStream);
...
return memStream.ToArray();
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();
}

I've never used iTextPDF before but it sounded interesting so I took upon the challenge and did some research on my own. 我以前从未使用过iTextPDF,但听起来很有趣,所以我接受了挑战并自行进行了一些研究。 Here's how to stream the PDF document via memory. 以下是通过内存流式传输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();
}

Where your code has new FileStream , pass in a MemoryStream you've already created. 您的代码具有new FileStream ,传递您已经创建的MemoryStream (Don't just create it inline in the call to PdfWriter.GetInstance - you'll want to be able to refer to it later.) (不要只是在对PdfWriter.GetInstance的调用中内联创建它-您以后希望能够引用它。)

Then call ToArray() on the MemoryStream when you've finished writing to it to get a byte[] : 完成写入后,在MemoryStream上调用ToArray()以获取byte[]

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

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

I haven't used iTextSharp, but I suspect some of these types implement IDisposable - in which case you should be creating them in using statements too. 我没有使用过iTextSharp,但我怀疑其中某些类型实现了IDisposable在这种情况下,您也应该在using语句中创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM