PDFBox如何从另一个pdf导入acrofield [英] PDFBox how to import acrofield from another pdf
本文介绍了PDFBox如何从另一个pdf导入acrofield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将导入acrofield从一个pdf导入另一个pdf时遇到问题. 这两个pdf相似.我试图导入并保存文件(下面的代码).如果从文件系统打开它,则看不到更改,但是如果使用pdfbox打开它,则会看到前面插入的acrofile. 我注意到文件大小增加了,但是当我打开它时,我看不到可填充的字段.
I have a problem with import exist acrofield from a pdf into another pdf. The two pdf are similar. I tried to import and save the file (code below). if I open it from the file system I do not see the changes, but if I open it with pdfbox I see the acrofiles inserted earlier. I notice that the file size has increased, but when I open it I do not see the fields fillable.
提前谢谢
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();
推荐答案
我这样解决:
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();
}
}
感谢您的支持:)
这篇关于PDFBox如何从另一个pdf导入acrofield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文