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

Tags: formsfill formflatten formcontinue on next pagetablesposition tablefield positioniText 5

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.addCell("#");
    table.addCell("description");
    table.setHeaderRows(1);
    table.setWidths(new int[]{ 1, 15 });
    for (int i = 1; i <= 150; i++) {
        table.addCell(String.valueOf(i));
        table.addCell("test " + i);
    }
    ColumnText column = new ColumnText(stamper.getOverContent(1));
    Rectangle rectPage1 = new Rectangle(36, 36, 559, 540);
    column.setSimpleColumn(rectPage1);
    column.addElement(table);
    int pagecount = 1;
    Rectangle rectPage2 = new Rectangle(36, 36, 559, 806);
    int status = column.go();
    while (ColumnText.hasMoreText(status)) {
        status =
            triggerNewPage(stamper, pagesize, column, rectPage2, ++pagecount);
    }
    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
}
public int triggerNewPage(PdfStamper stamper,
    Rectangle pagesize, ColumnText column, Rectangle rect, int pagecount)
        throws DocumentException {
    stamper.insertPage(pagecount, pagesize);
    PdfContentByte canvas = stamper.getOverContent(pagecount);
    column.setCanvas(canvas);
    column.setSimpleColumn(rect);
    return column.go();
}

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.