itext AcroFields表单位于第二页,需要保留相同的模板

itext AcroFields表单位于第二页,需要保留相同的模板,itext,Itext,我有一个在MS Word中创建的表单,然后转换为PDF(表单),然后我使用PDF阅读器加载该表单,然后创建一个填充字段的压模,如果我想使用相同的模板(表单)添加第二个页面,我如何做到这一点并使用相同的信息填充某些字段 我已经设法用另一个阅读器获得了一个新页面,但是如何在该页面上标记信息,因为AcroFields将具有相同的名称# 我就是这样做到的: stamper.insertPage(1,PageSize.A4); PdfReader reader = new

我有一个在MS Word中创建的表单,然后转换为PDF(表单),然后我使用PDF阅读器加载该表单,然后创建一个填充字段的压模,如果我想使用相同的模板(表单)添加第二个页面,我如何做到这一点并使用相同的信息填充某些字段

我已经设法用另一个阅读器获得了一个新页面,但是如何在该页面上标记信息,因为AcroFields将具有相同的名称#

我就是这样做到的:

        stamper.insertPage(1,PageSize.A4);
        PdfReader reader = new PdfReader("/soaprintjobs/templates/STOTemplate.pdf"); //reads the original pdf
        PdfImportedPage page; //writes the new pdf to file 
        page = stamper.getImportedPage(reader,1); //retrieve the second page of the original pdf
        PdfContentByte newPageContent = stamper.getUnderContent(1); //get the over content of the first page of the new pdf
        newPageContent.addTemplate(page, 0,0); 

感谢

Acroform字段具有相同名称的字段被视为相同字段的属性。它们具有相同的价值。因此,如果在第1页和第2页上有一个名称相同的字段,它们将始终显示相同的值。如果更改第1页上的值,第2页上的值也会更改

在某些情况下,这是可取的。您可能有一个带有参考号的多页表单,并且希望在每页上重复该参考号。在这种情况下,可以使用同名字段

但是,如果您希望在一个文档中具有相同表单的多个副本以及不同的数据,则会遇到问题。您必须重命名表单字段,使其唯一

在iText中,不应使用
getImportedPage()
复制表单。从iText 5.4.4开始,您可以使用
PdfCopy
类。在早期版本中,应使用
PdfCopyFields

下面是一些复制Acroforms和重命名字段的示例代码。iText 5.4.4及以上版本的代码在注释中

public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {

    String[] inputs = { "form1.pdf", "form2.pdf" };

    PdfCopyFields pcf = new PdfCopyFields(new FileOutputStream("out.pdf"));

    // iText 5.4.4+
    // Document document = new Document();
    // PdfCopy pcf = new PdfCopy(document, new FileOutputStream("out.pdf"));
    // pcf.setMergeFields();
    // document.open();

    int documentnumber = 0;
    for (String input : inputs) {
        PdfReader reader = new PdfReader(input);
        documentnumber++;
        // add suffix to each field name, in order to make them unique.
        renameFields(reader, documentnumber);
        pcf.addDocument(reader);
    }
    pcf.close();

    // iText 5.4.4+
    // document.close();

}

public static void renameFields(PdfReader reader, int documentnumber) {
    Set<String> keys = new HashSet<String>(reader.getAcroFields()
            .getFields().keySet());
    for (String key : keys) {
        reader.getAcroFields().renameField(key, key + "_" + documentnumber);
    }
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、DocumentException、IOException{
字符串[]输入={“form1.pdf”,“form2.pdf”};
PdfCopyFields pcf=新的PdfCopyFields(新文件输出流(“out.pdf”));
//iText 5.4.4+
//文档=新文档();
//PdfCopy pcf=新的PdfCopy(文档,新文件输出流(“out.pdf”);
//pcf.setMergeFields();
//document.open();
int documentnumber=0;
for(字符串输入:输入){
PdfReader读取器=新PdfReader(输入);
documentnumber++;
//为每个字段名添加后缀,使其唯一。
重命名字段(读卡器、文档编号);
pcf.addDocument(读卡器);
}
pcf.close();
//iText 5.4.4+
//document.close();
}
公共静态无效重命名字段(PdfReader读取器,int documentnumber){
Set keys=newhashset(reader.getAcroFields()
.getFields().keySet());
用于(字符串键:键){
reader.getAcroFields().renameField(键,键+“\u”+文档编号);
}
}

我有一个包含多页的文档,每个页面中都有一个字段,所有页面中的键都相同。关键名称是“abc”。但是我无法在第二页和以后的页面中设置“abc”的值。请建议我更好的选择来实现这一点。