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:
我有一个带有acroform的pdf。我正在使用iText打开此pdf并在表单字段中填入一些数据。有时我需要生成比原始pdf更多的页面。为此,我创建了一个页面,并使用以下代码替换现有页面:
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.
现在我正在制作该页面的多个副本以避免这种溢出。但我不认为这是一个优雅的解决方案,它不适用于非常大的输入。
1 个解决方案
#1
0
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.
我使用了PdfCopyFields。首先,我打开一个新的PdfCopyFields实例并调用SetFullCompression()。然后我将模板加载到字节缓冲区中,对于每个“模板”页面,我使用PdfStamper,设置表单字段,并将其输出添加到Reader和读取器中的PdfCopyFields。就是这样。在我的测试中,它可以很好地缩放多页,最终的pdf大小也不错。
#1
0
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.
我使用了PdfCopyFields。首先,我打开一个新的PdfCopyFields实例并调用SetFullCompression()。然后我将模板加载到字节缓冲区中,对于每个“模板”页面,我使用PdfStamper,设置表单字段,并将其输出添加到Reader和读取器中的PdfCopyFields。就是这样。在我的测试中,它可以很好地缩放多页,最终的pdf大小也不错。