38

How do one create PDF in memorystream instead of physical file using itextsharp.

The code below is creating actual pdf file.

Instead how can I create a byte[] and store it in the byte[] so that I can return it through a function

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
John Saunders
  • 160,644
  • 26
  • 247
  • 397
acadia
  • 2,619
  • 18
  • 55
  • 72
  • Encrypted PDF using memory stream, we can refer this at, https://www.aspsnippets.com/Articles/Create-Password-Protected-Secured-PDF-using-iTextSharp-in-ASPNet.aspx – Pranesh Janarthanan Dec 13 '18 at 13:10

4 Answers4

37

Switch the filestream with a memorystream.

MemoryStream memStream = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, memStream);
...
return memStream.ToArray();
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
26
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();
}
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • 1
    PdfWriter does not implement IDisposable so you cannot use it in a using statement. Maybe this is just in the version I am using though (5.0.5) as I know there have been some class changes from version 4 – musefan Jan 05 '12 at 11:49
  • 4
    @musefan, yes, in 5.0.5 that is the case. In the current version, 5.5, `PdfWriter` extends `DocWriter` which implements `IDocListener` which extends `IDisposable`. I don't know exactly when `IDisposable` was added to `IDocListener` but it was some time after 5.0.5 and before 5.5.0. – Samuel Neff Feb 28 '14 at 23:55
21

I've never used iTextPDF before but it sounded interesting so I took upon the challenge and did some research on my own. Here's how to stream the PDF document via memory.

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();
}
Dennis Rongo
  • 4,611
  • 1
  • 25
  • 25
8

Where your code has new FileStream, pass in a MemoryStream you've already created. (Don't just create it inline in the call to PdfWriter.GetInstance - you'll want to be able to refer to it later.)

Then call ToArray() on the MemoryStream when you've finished writing to it to get a 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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194