1

I'm trying to add a table accross multiple pages but the rest of the PDF to keep the same. It's a delivery note. Just wondering what I'm doing wrong, I'm guessing it's the PdfCopy as without it, the AcroFields work. Thanks.

enter image description here

string templateFile = @"c:\DeliveryNote.pdf";

using (Document doc = new Document())
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (PdfReader pdfReader = new PdfReader(templateFile))
        {
            using (PdfStamper pdfStamper = new PdfStamper(pdfReader, ms, '\0', true))
            {
                pdfStamper.Writer.CloseStream = false;

                AcroFields pdfFormFields = pdfStamper.AcroFields;

                pdfFormFields.SetField("CompanyName", "Test Limited");
                pdfFormFields.SetField("CompanyTelephoneNumber", "Test Limited");
                pdfFormFields.SetField("CompanyFaxNumber", "+44");

                Rectangle rect = pdfFormFields.GetFieldPositions("ItemTable")[0].position;
                ColumnText column = new ColumnText(pdfStamper.GetOverContent(1));
                column.SetSimpleColumn(rect);

                PdfPTable table = new PdfPTable(3);
                table.AddCell("Qty");
                table.AddCell("Product Code");
                table.AddCell("Description");

                for (int i = 0; i < 100; i++)
                {
                    table.AddCell($"Value {i}");
                    table.AddCell($"Value {i}");
                    table.AddCell($"Value {i}");
                }

                column.AddElement(table);

                while (true)
                {
                    column.SetSimpleColumn(rect);
                    if (!ColumnText.HasMoreText(column.Go()))
                        break;

                    PdfCopy copy = new PdfCopy(doc, ms);
                    copy.CloseStream = false;
                    doc.Open();
                    copy.SetMergeFields();
                    copy.AddDocument(new PdfReader(templateFile));
                }
            }
        }

        doc.Close();

        File.WriteAllBytes(@"C:\Temp\Lol.pdf", ms.ToArray());
        Process.Start(@"C:\Temp\Lol.pdf");
    }
}

1:

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Paul
  • 318
  • 5
  • 19
  • 1
    You have numerous different objects writing to your one `MemoryStream`. Obviously the result is garbage, a hodgepodge of the outputs of all those classes. – mkl Dec 04 '17 at 16:13
  • Even if you split the MemoryStream up, I still get garbage results. So, I don't believe it's to do with the MemoryStreams but thanks for the comment. – Paul Dec 04 '17 at 16:29
  • 2
    *"So, I don't believe it's to do with the MemoryStreams"* - This is not about *believing*. Using the same `MemoryStream` for outputs of different objects obviously results in garbage. There are additional issues in your code, though. E.g. you re-use the same `Document` instance for multiple `PdfCopy` instances. Actually I don't understand what you intend to achieve in the `while (true)` loop but as is it does not do anything sensible; even after separating the streams it wouldn't. – mkl Dec 04 '17 at 17:17
  • 1
    You shouldn't talk back to @mkl like that, Paul. Mkl is correct. The code you are sharing is broken beyond repair. Please save yourself time: don't argue with the expect, throw away your code, start anew using common sense. – Bruno Lowagie Dec 04 '17 at 18:44
  • I know that the answer I marked as a duplicate is not an iTextSharp question or answer, but (1.) iTextSharp no longer exists; we call it iText for .NET now, (2.) you are using an old version of iText for .NET. The latest version is iText 7 and you're using iText 5 or earlier, (3.) the code needed for iText for .NET is almost identical as the code needed for iText for Java. Throw away your `PdfCopy` code and look at the `triggerNewPage()` method. That will solve your problem. – Bruno Lowagie Dec 04 '17 at 18:48
  • I didn't mean to offend mkl. I just tried what they suggested. The while loop I was trying to put the table onto the next page of the document was the table was too big for the first PDF page. I'll download itext7 and do some research. Thanks for your help. – Paul Dec 04 '17 at 23:15

0 Answers0