文檔金喜正規買球>>Aspose.Words使用教程>>Java版Word開發工具Aspose.Words功能解析:添加和驗證PDF文檔中的數字簽名
Java版Word開發工具Aspose.Words功能解析:添加和驗證PDF文檔中的數字簽名
PDF中的數字簽名使您可以在與利益相關者共享文檔之前保護其安全。可以輕松地檢測和驗證數字簽名的PDF文檔內容的偽造。
為了以編程方式對PDF文檔進行數字簽名,在本文中,將展示如何使用Java在PDF中添加和驗證數字簽名。以下是我們將在此處探討的一些方案:
>>如果想要測試這項新功能,可點擊這里下載最新版試用。
- 使用Java將數字簽名添加到PDF
- 使用Java使用時間戳服務器對PDF進行數字簽名
- 使用Java驗證PDF中的數字簽名
①使用Java將數字簽名添加到PDF
以下是使用Aspose.PDF for Java將數字簽名添加到PDF文檔的步驟。
- 創建Document 類的實例, 并使用PDF文檔的路徑對其進行初始化。
- 初始化PdfFileSignature類,并將Document對象傳遞給它。
- 創建PKCS7類的實例,并使用證書的路徑和密碼對其進行初始化。
- 為MDP簽名類型初始化DocMDPSignature類。
- 定義一個 Rectangle 以將簽名放置在文檔頁面上。
- 使用PdfFileSignature.Certify()方法對PDF文檔進行數字簽名。
- 使用PdfFileSignature.Save() 方法保存簽名的PDF。
以下代碼示例顯示了如何使用Java對PDF進行數字簽名。
// Create a Document object Document doc = new Document("input.pdf"); PdfFileSignature signature = new PdfFileSignature(doc); PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objects DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms); Rectangle rect = new Rectangle(100, 600, 400, 100); // Set signature appearance signature.setSignatureAppearance("aspose-logo.png"); // Create any of the three signature types signature.certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature); // Save digitally signed PDF file signature.save("Digitally Signed PDF.pdf");
②使用Java使用時間戳服務器對PDF進行數字簽名
Aspose.PDF for Java還允許您使用TimeStamp服務器將數字簽名添加到PDF。該 TimestampSettings 類是用于這一目的。以下代碼示例顯示了如何使用Java使用TimeStamp服務器將數字簽名添加到PDF。
// Create a Document object Document doc = new Document("input.pdf"); PdfFileSignature signature = new PdfFileSignature(doc); PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objects TimestampSettings timestampSettings = new TimestampSettings("https:\\your_timestamp_settings", "user:password"); // User/Password can be omitted pkcs.setTimestampSettings(timestampSettings); Rectangle rect = new Rectangle(100, 600, 400, 100); // Set signature appearance signature.setSignatureAppearance("aspose-logo.png"); // Create any of the three signature types signature.sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs); // Save digitally signed PDF file signature.save("Digitally Signed PDF.pdf");
③使用Java驗證PDF中的數字簽名
收到經過數字簽名的PDF時,您可以非常輕松地驗證其簽名。以下是驗證經過數字簽名的PDF的步驟。
- 創建PdfFileSignature 類的實例。
- 使用PdfFileSigntature.bindPdf(string)方法綁定PDF文件。
- 使用PdfFileSignature.verifySigned() 方法驗證簽名的有效性 。
下面的代碼示例演示如何使用Java驗證經過數字簽名的PDF文檔。
// Create PDF File Signature PdfFileSignature pdfSign = new PdfFileSignature(); // Bind PDF pdfSign.bindPdf("Digitally Signed PDF.pdf"); // Verify signature using signature name if (pdfSign.verifySigned("Signature1")) { if (pdfSign.isCertified()) // Certified? { if (pdfSign.getAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission { // Do something } } }
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。