動(dòng)漫人物
2022-07-06 19:17:28
我對(duì) pdfBox API 有疑問。我正在嘗試使用以下代碼加密合并的 pdfdocument:這是合并/創(chuàng)建文檔的功能 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);我的結(jié)果如下所示:結(jié)果
1 回答

FFIVE
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
打電話
pddocumentToEncrypt.getDocumentCatalog().getAcroForm().refreshAppearances();
為 2.0 版本修復(fù)了這個(gè)問題。setValue()
當(dāng)設(shè)置 /NeedAppearances 時(shí),該版本不會(huì)在調(diào)用中設(shè)置外觀(即表單值的視覺表示) (請(qǐng)參閱 參考資料PDTerminalField.applyChange()
)。/NeedAppearances 設(shè)置(如果為真)意味著查看應(yīng)用程序應(yīng)該創(chuàng)建外觀,以便中間的處理應(yīng)用程序不需要這樣做;我懷疑其中一項(xiàng)或多項(xiàng)權(quán)限設(shè)置會(huì)阻止 Adobe Reader 在顯示時(shí)對(duì)其進(jìn)行更改。
另一種解決方案是致電
pdfDocument.getDocumentCatalog().getAcroForm().setNeedAppearances(false);
在設(shè)置表單值之前。
唯一未解之謎是為什么第一個(gè)值在合并文件中可見。
添加回答
舉報(bào)
0/150
提交
取消