1

我在将现有 acrofield 从 pdf 导入另一个 pdf 时遇到问题。这两个pdf是相似的。我尝试导入并保存文件(下面的代码)。如果我从文件系统打开它,我看不到更改,但如果我用 pdfbox 打开它,我会看到之前插入的 acrofile。我注意到文件大小增加了,但是当我打开它时,我看不到可填写的字段。

先感谢您

        PDDocument documentSrc = PDDocument.load(new File(SRC));
        PDAcroForm acroFormSrc = documentSrc.getDocumentCatalog().getAcroForm();

        PDDocument documentDest = PDDocument.load(new File(DEST));
        PDAcroForm acroFormDest = new PDAcroForm(documentDest);

        System.out.println("\n\n\n----------> FIELDS OF DOC SOURCE");
        for(PDField field : acroFormSrc.getFields()) {
            System.out.println(field);
        }

        acroFormDest.setCacheFields(true);
        acroFormDest.setFields(acroFormSrc.getFields());
        documentDest.getDocumentCatalog().setAcroForm(acroFormDest);

        documentDest.save(DEST_MERGED);
        documentDest.close();
        documentSrc.close();

        PDDocument documentMERGED = PDDocument.load(new File(DEST_MERGED));
        PDAcroForm acroFormMERGED = documentMERGED.getDocumentCatalog().getAcroForm();

        System.out.println("\n\n\n----------> FIELDS OF DOC MERGED");
        for(PDField field : acroFormMERGED.getFields()) {
            System.out.println(field);
        }

        documentMERGED.close();
4

2 回答 2

2

我这样解决了:

    try
    {

        PDDocument documentSrc = PDDocument.load(new File(SRC));
        PDAcroForm acroFormSrc = documentSrc.getDocumentCatalog().getAcroForm();

        PDDocument documentDest = PDDocument.load(new File(DEST));
        PDAcroForm acroFormDest = new PDAcroForm(documentDest);

        acroFormDest.setCacheFields(true);
        acroFormDest.setFields(acroFormSrc.getFields());
        documentDest.getDocumentCatalog().setAcroForm(acroFormDest);

        int pageIndex = 0;
        for(PDPage page: documentSrc.getPages()){
            documentDest.getPage(pageIndex).setAnnotations(page.getAnnotations());
            documentDest.getPage(pageIndex).setResources(page.getResources());
            pageIndex++;
        }

        documentDest.save(DEST_MERGED);
        documentDest.close();
        documentSrc.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

谢谢你的支持 :)

于 2018-01-31T09:19:06.060 回答
0

我已将您的代码更新为:

    public static void copyAcroForm(
            String acroFormPathfile,
            String inPathfile,
            String outPathfile) 
        throws IOException {

        try (
            PDDocument acroFormDocument = PDDocument.load(new File(acroFormPathfile));
            PDDocument outDocument = PDDocument.load(new File(inPathfile));) 
        {
            PDAcroForm templateAcroForm = acroFormDocument.getDocumentCatalog().getAcroForm();
            PDAcroForm outAcroForm = new PDAcroForm(outDocument);
            
            outAcroForm.setCacheFields(true);
            outAcroForm.setFields(templateAcroForm.getFields());
            outDocument.getDocumentCatalog().setAcroForm(outAcroForm);
            
            int pageIndex = 0;
            for (PDPage page: acroFormDocument.getPages()) {
                outDocument.getPage(pageIndex).setAnnotations(page.getAnnotations());
                outDocument.getPage(pageIndex).setResources(page.getResources());
                pageIndex++;
            }

            outDocument.save(outPathfile);
        }
    }

并在这里提出一个使用它的简单 Swing 应用程序 https://github.com/DavidRobertKeller/pdf-acroform-utils 屏幕截图

于 2021-01-07T15:02:58.617 回答