8

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 rollover 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 a 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.

My other option if this doesn't work is trying to add fields to the original pdf that will be filled by the table contents (so it will instead be a field-based table).

Any suggestions?

EDIT:

I'm new to iText and have not used columntext before, but I'm trying to test it out in the following code but the table is not being displayed. I looked at other columntext examples and I have not seen exactly where the columntext is added back into the pdf.

//CREATE FILLED FORM PDF
PdfReader reader = new PdfReader(sourcePath);  
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(destPath));
pdfStamper.setFormFlattening(true);
AcroFields form = pdfStamper.getAcroFields();

form.setField("ID", "99999");
form.setField("ADDR1", "425 Test Street");
form.setField("ADDR2", "Test, WA 91334");
form.setField("PHNBR", "(999)999-9999");
form.setField("NAME", "John Smith");

//CREATE TABLE
PdfPTable table = new PdfPTable(3);
Font bfBold12 = new Font(FontFamily.HELVETICA, 12, Font.BOLD, new BaseColor(0, 0, 0)); 
insertCell(table, "Table", Element.ALIGN_CENTER, 1, bfBold12);
table.completeRow(); 

ColumnText column = new ColumnText(pdfStamper.getOverContent(1));
column.addElement(table);

pdfStamper.close();
reader.close();
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Jennifer
  • 95
  • 1
  • 1
  • 6
  • Did you try adding the table using `ColumnText` and then create a new page if the table doesn't fit? The principle is explained in [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html), more specifically in this question: [How to continue field output on a second page?](http://stackoverflow.com/questions/26853894/how-to-continue-field-output-on-a-second-page) Instead of some paragraphs as is done in the example I wrote for that question, you add your table to the `ColumnText`. You may want to create a field to define where the table goes. – Bruno Lowagie Feb 18 '15 at 18:17
  • I'm very new to itext and I have looked at some code samples for columntext but I'm unsure how to use it in my situation. – Jennifer Feb 18 '15 at 19:54
  • 1
    Work started at 11:01 AM – Bruno Lowagie Feb 19 '15 at 10:03
  • 1
    Work finished at 11:26. That means that I've spent 25 minutes to write you a Proof of Concept. Please download [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) if you have further questions. It's free. You only need to register (which is a small reward for the 25 minutes I've donated). – Bruno Lowagie Feb 19 '15 at 10:27

1 Answers1

9

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.

See the following questions and answers of the official FAQ:

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you! This worked. I was mostly confused on how column text worked with the table but after some playing around with it I understand it much better now! And I did download the book, it's already helped! – Jennifer Feb 19 '15 at 21:31
  • Excellent code. I loved it. You saved my day. – jalpa Aug 12 '22 at 07:03