我的pdfBox API有问题。我正在尝试使用以下代码加密合并的pdf文档:
这是合并/创建文档的功能
public static void fillMultipleReportsInOne(List<report> reports) throws IOException {
PDFMergerUtility PDFmerger = new PDFMergerUtility();
PDDocument resultPDF = new PDDocument();
for (report report : reports) {
try
{
PDDocument pdfDocument = PDDocument.load(new File(SRC + "test.pdf"));
// get the document catalog
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
// as there might not be an AcroForm entry a null check is necessary
setFormFields(acroForm, report.getName(), report.getArea(), report.getOperatingActivities(), report.getVocationlaSchool(), report.getWeeklyTopics());
// Save and close the filled out form.
PDFmerger.appendDocument(resultPDF, pdfDocument);
} catch (Exception e) {
e.printStackTrace();
}
}
encryptPDF(resultPDF, SRC + "merged.pdf");
}
这是加密功能:
public static PDDocument encryptPDF(PDDocument pddocumentToEncrypt, String SRC) {
// Define the length of the encryption key.
// Possible values are 40 or 128 (256 will be available in PDFBox 2.0).
int keyLength = 128;
AccessPermission ap = new AccessPermission();
// Disable printing, everything else is allowed
ap.setCanModifyAnnotations(false);
ap.setCanFillInForm(false);
ap.setCanModify(false);
// Owner password (to open the file with all permissions) is "12345"
// User password (to open the file but with restricted permissions, is empty here)
StandardProtectionPolicy spp = new StandardProtectionPolicy("12334", "", ap);
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
try {
pddocumentToEncrypt.protect(spp);
pddocumentToEncrypt.save(SRC);
pddocumentToEncrypt.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pddocumentToEncrypt;
}
最后使用所有示例数据调用函数
report report1 = new report("TestUser1", "P&T", "operatingActivities", "weeklyTopics","vocationalSchool");
report report2 = new report("TestUser2", "P&T2", "operatingActivities2", "weeklyTopics2","vocationalSchool2");
report report3 = new report("TestUser3", "P&T3", "operatingActivities3", "weeklyTopics3","vocationalSchool3");
report report4 = new report("TestUser4", "P&T4", "operatingActivities4", "weeklyTopics4","vocationalSchool4");
List<report> reports = new ArrayList<>();
reports.add(report1);
reports.add(report2);
reports.add(report3);
reports.add(report4);
fillMultipleReportsInOne(reports);
我的结果如下:
结果
当所有字段都应包含数据时,只有第一个字段充满数据 这绝对是一个加密问题,因为当我删除document.protect()行时,数据已正确填充。我还尝试了acroForm.flatten()函数->不成功...
也许有人遇到相同的问题,并愿意提供帮助:) 提前致谢 -亚历克斯
这是粘贴到pastebin中的整个文件: https://pastebin.com/L9auaTGH
使用代码行
pddocumentToEncrypt.getDocumentCatalog().getAcroForm().refreshAppearances();
在我的加密功能中,它有效
答案 0 :(得分:0)
通话
pddocumentToEncrypt.getDocumentCatalog().getAcroForm().refreshAppearances();
针对2.0版本修复了此问题。设置/ NeedAppearances时,该版本不会在setValue()
调用中设置外观(即表单值的视觉表示)(请参见PDTerminalField.applyChange()
)。 / NeedAppearances设置(为true时)表示查看应用程序应创建外观,以便中间的处理应用程序不需要这样做;我怀疑一个或多个权限设置会阻止Adobe Reader在显示时对其进行更改。
另一种解决方法是致电
pdfDocument.getDocumentCatalog().getAcroForm().setNeedAppearances(false);
在设置表单值之前。
唯一未解之谜是为什么第一个值在合并文件中可见。