.NET版PDF处理控件Aspose.PDF功能演示:在C#中对PDF文档进行数字签名并验证数字签名

数字签名用于保护PDF文档,然后再与第三方共享。对PDF文档进行数字签名可以通过使用数字签名验证文档来检测篡改。

Aspose.PDF for .NET是一种PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务,可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。

  • 使用C#对PDF文档进行数字签名。
  • 使用C#中的时间戳服务器对PDF文档进行数字签名。
  • 使用C#验证PDF中的数字签名。

近日,.NET版Aspose.PDF升级到v20.2版,解决VerticalAlignment对TextBoxField不起作用的问题,修复PDF到HTML之间转换等多项Bug,感兴趣的朋友可点击下方按钮下载最新版。

用C#对PDF文档进行数字签名

以下是使用Aspose.PDF for .NET签署PDF文档的步骤。

  • 创建Document类的对象,并使用PDF文档的路径对其进行初始化。
  • 创建一个PdfFileSignature类的对象,并使用Document类的对象对其进行初始化。
  • 创建PKCS7类的对象,并使用证书路径和密码对其进行初始化。
  • 为MDP签名类型创建并初始化DocMDPSignature类的对象。
  • 创建一个矩形用于签名放置。
  • 使用PdfFileSignature.Certify()方法对PDF文档进行数字签名。
  • 使用PdfFileSignature.Save()方法保存文档。

以下代码示例显示了如何在C#中向PDF文档添加数字签名。

// Load PDF documentusing (Document pdfDocument = new Document("Document.pdf")){using (PdfFileSignature signature = new PdfFileSignature(pdfDocument)){PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objectsDocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);// Set signature appearancesignature.SignatureAppearance = @"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");}}

使用C#中的时间戳服务器对PDF文档进行数字签名

使用TimestampSettings类提供详细信息,从而使用TimeStamp服务器对PDF文档进行签名。以下代码示例演示如何使用C#中的TimeStamp服务器对PDF文档进行数字签名。

// Load PDF documentusing (Document pdfDocument = new Document("Document.pdf")){using (PdfFileSignature signature = new PdfFileSignature(pdfDocument)){PKCS7 pkcs = new PKCS7("certificate.pfx", "WebSales"); // Use PKCS7/PKCS7Detached objectsTimestampSettings timestampSettings = new TimestampSettings("https:\your_timestamp_settings", "user:password"); // User/Password can be omittedpkcs.TimestampSettings = timestampSettings;System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);// Create any of the three signature typessignature.Sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs);// Save output PDF filesignature.Save("Output.pdf");}}

使用C#验证PDF中的数字签名

使用以下步骤验证经过数字签名的PDF文档:

  • 创建Document类的对象,并使用PDF文档的路径对其进行初始化。
  • 创建一个PdfFileSignature类的对象,并使用Document类的对象对其进行初始化。
  • 访问PDF文档的所有签名。
  • 使用PdfFileSignature.VerifySigned()方法验证签名的有效性。

下面的代码示例演示如何使用C#验证PDF中的数字签名。

// Load PDF documentusing (Document pdfDocument = new Document("Document.pdf")){using (PdfFileSignature signature = new PdfFileSignature(pdfDocument)){IListsigNames = signature.GetSignNames();if (sigNames.Count > 0) // Any signaturesif (signature.VerifySigned(sigNames[0] as string)) // Verify first one{if (signature.IsCertified) // Certifiedif (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission{// Do something}}}}}}

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

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月23日
下一篇 2020年1月23日

相关推荐

发表回复

登录后才能评论