继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

C# / VB如何获取PDF文档的数字签名信息

Eiceblue
关注TA
已关注
手记 107
粉丝 9
获赞 48

文档中的数字签名具有不可否认性,可有效防伪防篡改。对文档中已有的数字签名信息,可通过一定方法获取,下面通过程序代码介绍如何来实现。程序中,使用了Spire.PDF.dll,版本:6.11.6,可自行在官网下载包,或者通过Nuget下载。下载后,在程序中添加引用dll文件。如下添加引用效果:

http://img1.sycdn.imooc.com/5fc9ee02000183f203190605.jpg

完成dll文件添加引用后,可参考如下代码来操作:

[C#]

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;
using System;
using System.Drawing;

 namespace GetSignature{
     class Program 
        {     
           static void Main(string[] args)    
               {          
                 //创建PdfDocument实例            
                 PdfDocument pdf = new PdfDocument();            
                 //加载含有签名的PDF文件            
                 pdf.LoadFromFile("数字签名.pdf");  
                            
                 //获取域集合            
                 PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;            
                 PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;             
                 
                 //遍历域          
                 for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)         
                    {               
                     //判定是否为签名域               
                      if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)               
                       {                    
                        //获取签名域                  
                        PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];                    
                        //获取签名                    
                        PdfSignature signature = signatureFieldWidget.Signature;                    
                        String location = signature.LocationInfo;                    
                        String reason = signature.Reason;                    
                        String data = signature.Date.ToString();                     
                        Console.WriteLine("签名位置信息:" + location + "\n" +                            
                        "签名原因:" + reason + "\n" +                            
                        "签名日期:" + data + "\n" +                            
                        "文档中的签名坐标:X = " + signatureFieldWidget.Location.X + "  Y = " + signatureFieldWidget.Location.Y);                    
                        Console.ReadLine();               
                   }            
               }        
          }    
     }
}

[VB.NET]

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

Imports Spire.Pdf.Security

Imports Spire.Pdf.Widget

Imports System.Drawing

 

Namespace GetSignature

         Class Program

                   Private Shared Sub Main(args As String())

 

                            '创建PdfDocument实例

                            Dim pdf As New PdfDocument()

                            '加载含有签名的PDF文件

                            pdf.LoadFromFile("数字签名.pdf")

 

                            '获取域集合

                            Dim pdfFormWidget As PdfFormWidget = DirectCast(pdf.Form, PdfFormWidget)

                            Dim pdfFormFieldWidgetCollection As PdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget

 

                            '遍历域

                            For i As Integer = 0 To pdfFormFieldWidgetCollection.Count - 1

                                     '判定是否为签名域

                                     If TypeOf pdfFormFieldWidgetCollection(i) Is PdfSignatureFieldWidget Then

                                               '获取签名域

                                               Dim signatureFieldWidget As PdfSignatureFieldWidget = DirectCast(pdfFormFieldWidgetCollection(i), PdfSignatureFieldWidget)

                                               '获取签名

                                               Dim signature As PdfSignature = signatureFieldWidget.Signature

                                               Dim location As [String] = signature.LocationInfo

                                               Dim reason As [String] = signature.Reason

                                               Dim data As [String] = signature.[Date].ToString()

 

                                               Console.WriteLine("签名位置信息:" + location + vbLf + "签名原因:" + reason + vbLf + "签名日期:" + data + vbLf + "文档中的签名坐标:X = " + signatureFieldWidget.Location.X + "  Y = " + signatureFieldWidget.Location.Y)

                                               Console.ReadLine()

                                     End If

                            Next

 

                   End Sub

         End Class

End Namespace


数字签名信息获取结果:

http://img1.sycdn.imooc.com/5fc9f1ad000157f906770442.jpg



打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP