我對 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);我的結果如下所示:結果
1 回答

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