Java版Word开发工具Aspose.Words功能解析:添加和验证PDF文档中的数字签名

为了以编程方式对PDF文档进行数字签名,在本文中,将展示如何使用Java在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 objectDocument doc = new Document("input.pdf");PdfFileSignature signature = new PdfFileSignature(doc);PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objectsDocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);Rectangle rect = new Rectangle(100, 600, 400, 100);// Set signature appearancesignature.setSignatureAppearance("aspose-logo.png");// Create any of the three signature typessignature.certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);// Save digitally signed PDF filesignature.save("Digitally Signed PDF.pdf");

②使用Java使用时间戳服务器对PDF进行数字签名

Aspose.PDF for Java还允许您使用TimeStamp服务器将数字签名添加到PDF。该 TimestampSettings 类是用于这一目的。以下代码示例显示了如何使用Java使用TimeStamp服务器将数字签名添加到PDF。

// Create a Document objectDocument doc = new Document("input.pdf");PdfFileSignature signature = new PdfFileSignature(doc);PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objectsTimestampSettings timestampSettings = new TimestampSettings("https:\your_timestamp_settings", "user:password"); // User/Password can be omittedpkcs.setTimestampSettings(timestampSettings);Rectangle rect = new Rectangle(100, 600, 400, 100);// Set signature appearancesignature.setSignatureAppearance("aspose-logo.png");// Create any of the three signature typessignature.sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs);// Save digitally signed PDF filesignature.save("Digitally Signed PDF.pdf");

③使用Java验证PDF中的数字签名

收到经过数字签名的PDF时,您可以非常轻松地验证其签名。以下是验证经过数字签名的PDF的步骤。

  • 创建PdfFileSignature 类的实例。
  • 使用PdfFileSigntature.bindPdf(string)方法绑定PDF文件。
  • 使用PdfFileSignature.verifySigned() 方法验证签名的有效性 。

下面的代码示例演示如何使用Java验证经过数字签名的PDF文档。

// Create PDF File SignaturePdfFileSignature pdfSign = new PdfFileSignature();// Bind PDFpdfSign.bindPdf("Digitally Signed PDF.pdf");// Verify signature using signature nameif (pdfSign.verifySigned("Signature1")){if (pdfSign.isCertified()) // Certifiedif (pdfSign.getAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission{// Do something}}}

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年6月17日
下一篇 2020年6月17日

相关推荐

发表回复

登录后才能评论