How to copy AcroForm using iText?

I have a pdf with acroform. I use iText to open this pdf file and fill in the form fields with some data. Sometimes I need to create more pages than the original pdf. To do this, I create one page and replace the existing page with 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); 

By doing this, the fields on the existing page are not copied to the new page.

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

How to copy form fields to a new page? How to change the name of a new form field so that there are no duplicate fields?

EDIT: There is a table on the page I want to copy. But this table has only ten rows. If the user enters more than ten elements, I want to fill in the entire table and create a new page to fill with other elements.

Now I am making several copies of this page to avoid this overflow. But I do not think this is an elegant solution, and it does not work for very large inputs.

+4
source share
1 answer

For this, I used PdfCopyFields. First I open a new instance of PdfCopyFields and is called SetFullCompression (). Then I load my template into the byte buffer, and for each page of the "template" I use PdfStamper, set the form fields and add this output to this in Reader and this reader on PdfCopyFields. That's all. In my test, it scaled very well for several pages, and the final PDF size was decent.

0
source

All Articles