我尝试使用PDFBox和Groovy创建多页文档。 我有一个模板文档,其中包含一些表单文本字段,每次创建新文档时,程序都会使用此模板。
我的问题是,每当我尝试创建新文档时,新文档中的某些表单字段都会丢失。我和Foxit PhantomPDF合作,在视觉上,我看不到丢失的字段。我看到的其他人都很好。
这是我的代码:
static void initiatePdf() {
// Initiate a new PDF Box object and get the acro form from it
File file = new File(Constants.EMPTY_DOC)
PDDocument tempDoc
Evaluator evaluator = new Evaluator()
int numPages = evaluator.getNumOfPagesRequired(objects)
for(int i = 0; i < numPages; i++) {
tempDoc = new PDDocument().load(file)
PDDocumentCatalog docCatalog = tempDoc.getDocumentCatalog()
PDAcroForm acroForm = docCatalog.acroForm
PDPage page = (PDPage) docCatalog.getPages().get(0)
document.addPage(page)
}
document.save(Constants.RESULT_FILE)
document.close()
}
这是一张有助于描绘我的问题的图片。这是模板:
这是新的pdf文件
答案 0 :(得分:0)
我设法在Tilman Hausherr的帮助下解决了这个问题。这是位于for循环之后的代码。
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
acroForm.setFields(fields)
acroForm.setDefaultResources(res);
PDDocumentCatalog catalog = document.getDocumentCatalog();
catalog.setAcroForm(acroForm);