iTextSharp adding new instances of an existing page

This is explained in the MergeForms2 example. You should split your document into 3 documents each having one page. Use the renameFields() method to create variations of the second page.

Concatenating forms is done like this:

Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(dest));
copy.setMergeFields();
document.open();
List<PdfReader> readers = new ArrayList<PdfReader>();
for (int i = 0; i < 3; ) {
    PdfReader reader = new PdfReader(renameFields(src, ++i));
    readers.add(reader);
    copy.addDocument(reader);
}
document.close();
for (PdfReader reader : readers) {
    reader.close();
}

A common mistake is to forget the setMergeFields() method.


This works but generates a large file. I guess it is because the image on page 2 is duplicated many times instead of being reused.

public static void ExpandRepeatingPages(string sourcePdfPath, string outputPdfPath)
{
    /* figure out how many pages we are working with */
    var transientPdfReader = new PdfReader(sourcePdfPath);
    var numberOfPages = transientPdfReader.NumberOfPages;
    transientPdfReader.Close();

    var outputFileStream = new FileStream(outputPdfPath, FileMode.Create);
    var pdfCopyFields = new PdfCopyFields(outputFileStream);

    foreach (var pageNumber in Enumerable.Range(1, numberOfPages))
    {
        var pdfBytes = ExtractPageToBytes(sourcePdfPath, pageNumber);
        var pdfReader = new PdfReader(pdfBytes);
        pdfCopyFields.AddDocument(pdfReader);
        pdfReader.Close();

        if (pageNumber == 2)
        {
            foreach (var extraPageNumber in Enumerable.Range(2, 200))
            {
                var extraPagePdfBytes = RenamePageFields(pdfBytes, extraPageNumber);
                pdfReader = new PdfReader(extraPagePdfBytes);
                pdfCopyFields.AddDocument(pdfReader);
                pdfReader.Close();
            }
        }
    }

    pdfCopyFields.Close();
}

public static byte[] ExtractPageToBytes(string sourcePdfPath, int pageNumber)
{
    using (var memoryStream = new MemoryStream())
    {
        var pageNumbers = new System.Collections.ArrayList { pageNumber };
        var pdfReader = new PdfReader(sourcePdfPath);
        var pdfCopyFields = new PdfCopyFields(memoryStream);

        pdfReader.SelectPages(pageNumbers);
        pdfCopyFields.AddDocument(pdfReader);
        pdfReader.RemoveUnusedObjects();
        pdfCopyFields.Close();
        pdfReader.Close();
        return memoryStream.ToArray();
    }
}

private static byte[] RenamePageFields(byte[] pdfBytes, int pageNumber)
{
    using (var memoryStream = new MemoryStream())
    {
        var pdfReader = new PdfReader(pdfBytes);
        var pdfStamper = new PdfStamper(pdfReader, memoryStream);
        var acroFields = pdfStamper.AcroFields;
        var fieldNames = acroFields.Fields.Keys.Cast<String>().ToList();

        foreach (var fieldName in fieldNames)
        {
            var newName = String.Format("{0}_{1}", fieldName, pageNumber);
            acroFields.RenameField(fieldName, newName);
        }

        pdfStamper.Close();
        pdfReader.Close();
        return memoryStream.ToArray();
    }
}

Comments

  1. Luka

    • 2020/8/8

    This is explained in the MergeForms2 example. You should split your document into 3 documents each having one page.

  2. Morelli

    • 2015/5/16

    iTextSharp adding new instances of an existing page. Each new instance of page 2 will need to have the field names modified to make them distinct from the other

  3. Aron

    • 2020/7/9

    You can provide any name for the generated file and any text that you want to print in the PDF file. For example here I provided "test.pdf" and 

  4. Emmitt

    • 2018/10/19

    I need to insert a blank page to an existing PDF using Itextsharp in the end of the PDF if it has odd number of pages. I could sucessfully get the. Add Page Numbers to Existing PDF Document in C# PDF, we can add the blank page to any place in the PDF file you want, such as at the first, the middle of . I am generating PDF file with the help of Add+a+new+page+in+ iTextSharp. 0 //insert blank page at the end of the PDF file.

  5. Bertrand

    • 2015/12/30

    Hello Everyone, I am using asp.net 3.5 with c#. I am displaying my existing pdf in iframe. Now i want to add new page to the existing pdf.

  6. Moretti

    • 2015/6/29

    Document document = new Document(PageSize.A4, 0, 0, 50, 50); System.IO.MemoryStream msReport = new System.IO.MemoryStream(); try { // creation of the different writers PdfWriter writer = PdfWriter.GetInstance(document, msReport); // we add some meta information to the document document.AddTitle("My Title"); document.AddAuthor("Me"); document.Open(); for (int i = 1; i <= 5; i++) { document.NewPage(); iTextSharp.text.Table datatable = new iTextSharp.text.Table(3); datatable.Padding = 2

  7. Adrian

    • 2015/9/2

    Hello Everyone I have a QuestionI have a PDF templateabcpdf that defined 1 page with fields to be annotated And I need to create a PDF using the template 

  8. Gray

    • 2016/11/15

    Adding another page on existing pdf using eixsting Yes, you can certainly do this using iTextSharp. Adding another page on existing pdf using itextsharp. I can confirm that for newer version you should add a Chunk instead of a table.

  9. Micah

    • 2021/5/14

    Hello everyone, I'm new here.. Just want to ask, is it possible to add another page on existing pdf using itextsharp.

  10. Kristian

    • 2017/11/20

    You could, for instance, create a ColumnText object iextsharp a series of Paragraphs, then you add these Paragraphs to a number of pages that itextshzrp inserted into the existing document. All the methods from topic 3 are at your disposal. If we want to add content to a content stream, we need to create a PdfCanvas object. Instead of writing the document to a FileOutputStream in the first pass, you keep the file in memory using a ByteArrayOutputStream see section 1.

  11. Colby

    • 2021/7/3

    The AddContent example shows how we can add content to every page in an existing document. Copy to clipboard 

  12. Mustafa

    • 2016/10/17

    Just like in chapter 4, we add the annotation to a page obtained from the PdfDocument instance: //Add text annotation PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(400, 795, 0, 0)) .SetTitle(new PdfString("iText")) .SetContents("Please, fill out the form.") .SetOpen(true); pdfDoc.GetFirstPage().AddAnnotation(ann);

  13. Quinton

    • 2018/3/16

    In this case, we create a new PdfDocument instance from the source pages.pdf and copy the first page of the hero.pdf to it. As you see, the third parameter in 

  14. Maximo

    • 2018/4/8

    ITEXTSHARP ADD BLANK PAGE TO PDF. I need to insert a blank page to an existing PDF using Itextsharp in the end of the PDF if it has odd number of pages. I could sucessfully get the. has a function of adding, removing the blank pages in C#. We have already shown you how to remove the blank page in a PDF file.

  15. Jaden

    • 2017/7/23

    In this topic we'll add content to an existing document using PdfStamper. It's an example that creates a new PDF document containing only a selection of 

  16. Fontana

    • 2015/8/16

    I will add a Header, SubHeader, Line Separator, Image, Table, Hyperlink, and finally page numbers to the PDF document.

  17. Yosef

    • 2018/10/17

    But before you can take copies of pages or add new content, you'll need an object that can “read” an existing PDF document. livebook features:

Comments are closed.

Recent Posts