0

I am having some issues with iTextSharp and memory streams. I am able to fill out a single form and write it to the memory stream. However, as soon as I fill out the second form, it corrupts the final PDF file and it's unreadable. Can someone let me know what I'm doing wrong?

public byte[] GetPDFDoc(string pdfTemplate){
byte[] single;

using (MemoryStream ms = new MemoryStream())
{
    var theforms = db.getforms();
    foreach (var f in theforms)
    {
        using (MemoryStream ms2 = new MemoryStream())
        {
            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, ms2, '\0', true);
            AcroFields form = pdfStamper.AcroFields;

            // fill out form

            pdfStamper.Close();
            pdfStamper.Dispose();

            single = ms2.ToArray();
        }
        ms.Write(single, 0, (int)single.Length);
    }
    return ms.ToArray();
}
}

Thanks

jimiyash
  • 2,494
  • 2
  • 20
  • 29
  • What you're doing with `ms` violates all PDF logic. You need to use `PdfSmartCopy` to concatenate the PDFs. Read the documentation! – Bruno Lowagie Dec 28 '16 at 20:07
  • 1
    Bruno, thanks. I ended up using the answer from Jonathan here: https://stackoverflow.com/questions/6778721/merging-memory-streams-to-create-a-http-pdf-response-in-c-sharp/6780582#6780582 – jimiyash Dec 28 '16 at 21:13
  • Great! I was answering from my phone and I didn't find a C# example immediately:D – Bruno Lowagie Dec 28 '16 at 21:22

0 Answers0