PDFBox如何从另一个pdf导入acrofield

时间:2018-01-25 11:46:39

标签: pdf import pdfbox acrofields

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

提前谢谢

        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();

2 个答案:

答案 0 :(得分:1)

我解决了这个问题:

const handleKeyUp = () =>
  render(
    <div key="1">{Math.random()}</div>,
  document.getElementById('random') 
)

const input = () => 
  render(
    <input onKeyUp={handleKeyUp} placeholder="type somthing here"/>,         
    document.getElementById('input')
  )


input()

感谢您的支持:)

答案 1 :(得分: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 Screencap