PDF管理控件Aspose.PDF for .Net使用教程(三十二):数字签名PDF文件

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

>>Aspose.PDF for .NET更新至最新版v20.4,欢迎下载体验。


数字签名PDF文件

用于.NET的Aspose.PDF支持使用SignatureField该类对PDF文件进行数字签名的功能。当然也可以使用PKCS12证书来认证PDF文件。

使用签名对PDF文档签名时,基本上可以“按原样”确认其内容。因此,之后进行的任何其他更改都会使签名无效,所以,将知道文档是否被更改。鉴于首先对文档进行认证,我们可以指定用户可以在不使认证无效的情况下对文档进行的更改。换句话说,仍将文档视为保留其完整性,并且收件人仍可以信任该文档。

为了满足上述要求,已对以下公共API进行了更改。

  • 添加DocMDPSignature类
  • 添加DocMDPAccessPermissions枚举
  • IsCertified属性添加到PdfFileSignature类
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();string pbxFile = "";string inFile = dataDir + @"DigitallySign.pdf";string outFile = dataDir + @"DigitallySign_out.pdf";using (Document document = new Document(inFile)){    using (PdfFileSignature signature = new PdfFileSignature(document))    {        PKCS7 pkcs = new PKCS7(pbxFile, "WebSales"); // Use PKCS7/PKCS7Detached objects        DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);        // Set signature appearance        signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";        // Create any of the three signature types        signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);        // Save output PDF file        signature.Save(outFile);    }}using (Document document = new Document(outFile)){    using (PdfFileSignature signature = new PdfFileSignature(document))    {        IList<string> sigNames = signature.GetSignNames();        if (sigNames.Count > 0) // Any signatures       {            if (signature.VerifySigned(sigNames[0] as string)) // Verify first one            {                if (signature.IsCertified) // Certified               {                    if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission                    {                        // Do something                    }                }            }        }    }}

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

用于.NET的Aspose.PDF支持使用时间戳服务器或Web服务对PDF进行数字签名。为了实现此要求,TimestampSettings已将该类添加到Aspose.PDF命名空间中。

请查看下面的代码片段,该代码片段获得了时间戳并将其添加到PDF文档中。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();string pfxFile = "";using (Document document = new Document(dataDir + @"DigitallySign.pdf")){    using (PdfFileSignature signature = new PdfFileSignature(document))    {        PKCS7 pkcs = new PKCS7(pfxFile, "pfx_password");        TimestampSettings timestampSettings = new TimestampSettings("https:\your_timestamp_settings", "user:password"); // User/Password can be omitted        pkcs.TimestampSettings = timestampSettings;        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);        // Create any of the three signature types        signature.Sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs);        // Save output PDF file        signature.Save(dataDir + "DigitallySignWithTimeStamp_out.pdf");    }}


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

标签:

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

上一篇 2020年3月5日
下一篇 2020年3月5日

相关推荐

发表回复

登录后才能评论