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

标签 itext

我有一个在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 字段具有相同名称的字段被视为同一字段的属性。它们具有相同的值(value)。因此,如果第 1 页和第 2 页上有一个具有相同名称的字段,它们将始终显示相同的值。如果您更改第 1 页上的值,第 2 页上的值也会更改。

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

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

在 iText 中,不应使用 getImportedPage() 复制 Acroforms。从 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);
    }
}

关于itext AcroFields 表单到第二页,需要保留相同的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613410/

相关文章:

asp.net - 如何在浏览器中打开PDF并修改并保存?

c# - 如何在 iTextSharp 中像 block 一样渲染 PdfContentByte

java - 无法将 ltv 添加到 PDF 文档。错误

java - 在 iTextSharp 中更改 PDF 文档的页面顺序

android - 如何在保持透明背景的同时转换图像中的 LinearLayout

java - iText - 无需 PDF 文件即可即时生成文件

java - 通过java创建带有超链接的pdf文件

java - Java打印后删除PDF文件