文档半岛外围网上直营>>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),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线