C# / VB.NET 获取PDF文档的数字签名信息
文档中的数字签名具有不可否认性,可有效防伪防篡改。对文档中已有的数字签名信息,可通过一定方法获取,下面通过程序代码介绍如何来实现。程序中,使用了Spire.PDF.dll,版本:6.11.6,可自行在官网下载包,或者通过Nuget下载。下载后,在程序中添加引用dll文件。如下添加引用效果:
完成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
数字签名信息获取结果:
相关阅读推荐: C# 给PDF文档添加数字签名