How to add a table on a form (and maybe insert a new page)?

I have two parts to my java project.

  • I need to populate the fields of a PDF
  • I need to add a table below the populated section on the blank area of the page (and this table needs to be able to roll over to the next page).

I am able to do these things separately (populate the PDF and create a table). But I cannot effectively merge them. I have tried doing doc.add(table) which will result in the table being on the next page of the PDF, which I don't want.

I essentially just need to be able to specify where the table starts on the page (so it wouldn't overlap the existing content) and then stamp the table onto the existing PDF.

Posted on StackOverflow on Feb 18, 2015 by Jennifer

Please take a look at the AddExtraTable example. It's a simplification of the AddExtraPage example written in answer to the question How to continue field output on a second page?

That question is almost an exact duplicate of your question, with as only difference the fact that your requirement is easier to achieve.

I simplified the code like this:

public void manipulatePdf(String src, String dest)
    throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    Rectangle pagesize = reader.getPageSize(1);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    AcroFields form = stamper.getAcroFields();
    form.setField("Name", "Jennifer");
    form.setField("Company", "iText's next customer");
    form.setField("Country", "No Man's Land");
    PdfPTable table = new PdfPTable(2);
    table.setWidths(new int[]{ 1, 15 });
    for (int i = 1; i 

As you can see, the main differences are:

  1. We create a rectPage1 for the first page and a rectPage2 for page 2 and all pages that follow. That's because we don't need a full page on the first page.

  2. We don't need to load a PdfImportedPage, instead we're just adding blank pages of the same size as the first page.

Possible improvements: I hardcoded the Rectangle instances. It goes without saying that rect1Page depends on the location of your original form. I also hardcoded rect2Page. If I had more time, I would calculate rect2Page based on the pagesize value.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now