翻譯|使用教程|編輯:李顯亮|2020-07-20 10:56:00.320|閱讀 676 次
概述:為了以編程方式對PDF文檔進行數字簽名,在本文中,將展示如何使用Java在PDF中添加和驗證數字簽名。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
PDF中的數字簽名使您可以在與利益相關者共享文檔之前保護其安全。可以輕松地檢測和驗證數字簽名的PDF文檔內容的偽造。
為了以編程方式對PDF文檔進行數字簽名,在本文中,將展示如何使用Java在PDF中添加和驗證數字簽名。以下是我們將在此處探討的一些方案:
>>如果想要測試這項新功能,可點擊這里下載最新版試用。
以下是使用Aspose.PDF for Java將數字簽名添加到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");
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");
收到經過數字簽名的PDF時,您可以非常輕松地驗證其簽名。以下是驗證經過數字簽名的PDF的步驟。
下面的代碼示例演示如何使用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 } } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn