iTextSharp PDF Stamper outputting corrupted PDFs

181 views Asked by At

I am using iTextSharp's PDF Stamper to add a rectangle to the bottom corner of the PDF with some text inside of that box. This works great sometimes, but other times I get an error opening the PDF and it's corrupted. I'm wondering if anyone can see something I am doing wrong. Here is my code that reads an existing PDF and save a stamped PDF as a copy.

using (var reader = new PdfReader(_InputFileName))
{
    using (var fileStream = new FileStream(_OutputFileName, FileMode.Create, FileAccess.ReadWrite))
    {
        using (PdfStamper PDFStamper = new PdfStamper(reader, fileStream))
        {
            Rectangle cropBox = reader.GetPageSize(1);
            Rectangle bottomLeft = new Rectangle(80, 20);
            EmptyTextBoxSimple(PDFStamper, 1, bottomLeft, BaseColor.ORANGE);
            ColumnText columnText = GenerateTextBox(PDFStamper, 1, bottomLeft);
            columnText.AddText(new Phrase(_StampText));
            columnText.Go();
        }
    }
}
0

There are 0 answers