我对 pdfBox API 有疑问。我正在尝试使用以下代码加密合并的 pdfdocument:
这是合并/创建文档的功能
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);
我的结果如下所示:
结果
FFIVE
相关分类