views:

789

answers:

1

I have a pdf with an acroform. I'm using iText to open this pdf and fill the form fields with some data. Sometimes I need to generate more pages than the original pdf. To do that I create one page and replace with an existing page using this code:

    OutputStream output = new FileOutputStream("C:\\newFile.pdf");
    PdfStamper stamper = PdfHelper.openPdfStamper("C:\\template.pdf", output);
    stamper.insertPage(NEW_PAGE_NUMBER,  new Rectangle(0,0));
    stamper.replacePage(stamper.getReader(), EXISTING_PAGE_NUMBER, NEW_PAGE_NUMBER);

Doing this, the fields in the existing page aren't copied to the new page.

Is this a good way to add a new page? (the new page must be equal to an existing one)

How to copy the form fields into the new page? How to change the new form fields name so there won't be duplicated fields?

EDIT: The page I want to copy has a table. But this table has only ten rows. If the user input more than ten items I want to fill the whole table and create a new page to fill with the other items.

Now I'm making multiple copies of that page to avoid this overflow. But I don't think this is an elegant solution and it doesn't work for very large inputs.

A: 

I used the PdfCopyFields for this. First I open up a new instance of PdfCopyFields and called SetFullCompression(). Then I load up my template into a byte buffer, and, for each "template" page I use the PdfStamper, set the form fields, and add that output of that into a Reader and that reader to the PdfCopyFields. That was it. In my test it scaled very well for multiple pages and the final pdf size was decent.

Trent