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

PDF中的数字签名使您可以在与利益相关者共享文档之前保护其安全。可以轻松地检测和验证数字签名的PDF文档内容的伪造。

为了以编程方式对PDF文档进行数字签名,在本文中,将展示如何使用Java在PDF中添加和验证数字签名。以下是我们将在此处探讨的一些方案:

aspose.words下载icon-default.png?t=M85Bhttps://www.evget.com/product/4116/download

  • 使用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
		}
	} 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值