世俱杯 2025

logo E-iceblue中文文档
文档世俱杯 2025>>E-iceblue中文文档>>添加或删除 PDF 中的数字签名

添加或删除 PDF 中的数字签名


Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。

行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。

Spire.PDF for.NET 最新下载

欢迎加入spire技术交流群:767755948

随着 PDF 文档在企业中越来越受欢迎,确保其真实性已成为一个关键问题。使用基于证书的签名来签署 PDF 可以保护内容,还能让其他人知道谁签署或批准了该文档。在本文中,您将了解如何使用不可见或可见签名对 PDF 进行数字签名,以及如何使用 Spire.PDF for .NET 从 PDF 中移除数字签名。

  • 为 PDF 添加不可见数字签名
  • 为 PDF 添加可见数字签名
  • 删除 PDF 中的数字签名

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 软件包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。这些 DLL 文件既可以从这个链接下载,也可以通过 NuGet 安装。

1  PM> Install-Package Spire.PDF

为 PDF 添加隐形数字签名

以下是使用 Spire.PDF for .NET 为 PDF 添加隐形数字签名的步骤。

  • 创建一个 PdfDocument 对象。
  • 使用 PdfDocument.LoadFromFile() 方法加载示例 PDF 文件。
  • 在初始化 PdfCertificate 对象时加载 pfx 证书文件。
  • 根据证书创建 PdfSignature 对象。
  • 通过 PdfSignature 对象设置文档权限。
  • 使用 PdfDocument.SaveToFile() 方法将文档保存到另一个 PDF 文件中。

[C#]

01	using Spire.Pdf;
02	using Spire.Pdf.Security;
03	 
04	namespace AddInvisibleSignature
05	{
06	    class Program
07	    {
08	        static void Main(string[] args)
09	        {
10	            //Create a PdfDocument object
11	            PdfDocument doc = new PdfDocument();
12	 
13	            //Load a sample PDF file
14	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
15	 
16	            //Load the certificate
17	            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
18	 
19	            //Create a PdfSignature object
20	            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
21	           
22	            //Set the document permission to forbid changes but allow form fill
23	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
24	 
25	            //Save to another PDF file
26	            doc.SaveToFile("InvisibleSignature.pdf");
27	            doc.Close();
28	        }
29	    }
30	}

[VB.NET]

01	Imports Spire.Pdf
02	Imports Spire.Pdf.Security
03	  
04	Namespace AddInvisibleSignature
05	    Class Program
06	        Shared  Sub Main(ByVal args() As String)
07	            'Create a PdfDocument object
08	            Dim doc As PdfDocument =  New PdfDocument()
09	  
10	            'Load a sample PDF file
11	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
12	  
13	            'Load the certificate
14	            Dim cert As PdfCertificate =  New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
15	  
16	            'Create a PdfSignature object
17	            Dim signature As PdfSignature =  New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
18	  
19	            'Set the document permission to forbid changes but allow form fill
20	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
21	  
22	            'Save to another PDF file
23	            doc.SaveToFile("InvisibleSignature.pdf")
24	            doc.Close()
25	        End Sub
26	    End Class
27	End Namespace

为 PDF 添加可见数字签名
以下是使用 Spire.PDF for .NET 为 PDF 添加可见数字签名的步骤。

  • 创建一个 PdfDocument 对象。
  • 使用 PdfDocument.LoadFromFile() 方法加载一个示例 PDF 文件。
  • 在初始化 PdfCertificate 对象时加载 pfx 证书文件。
  • 创建一个 PdfSignature 对象,并指定其在文档中的位置和大小。
  • 设置签名细节,包括日期、姓名、位置、原因、手写签名图像和文档权限。
  • 使用 PdfDocument.SaveToFile() 方法将文档保存到另一个 PDF 文件中。
[C#]

01	using System;
02	using System.Drawing;
03	using Spire.Pdf;
04	using Spire.Pdf.Security;
05	using Spire.Pdf.Graphics;
06	 
07	namespace AddVisibleSignature
08	{
09	    class Program
10	    {
11	        static void Main(string[] args)
12	        {
13	            //Create a PdfDocument object
14	            PdfDocument doc = new PdfDocument();
15	 
16	            //Load a sample PDF file
17	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
18	 
19	            //Load the certificate
20	            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
21	 
22	            //Create a PdfSignature object and specify its position and size
23	            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
24	            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 260 - 54 , 200, 260, 110);
25	            signature.Bounds = rectangleF;
26	            signature.Certificated = true;
27	 
28	            //Set the graphics mode to ImageAndSignDetail
29	            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
30	 
31	            //Set the signature content
32	            signature.NameLabel = "Signer:";
33	            signature.Name = "Gary";
34	            signature.ContactInfoLabel = "Phone:";
35	            signature.ContactInfo = "0123456";
36	            signature.DateLabel = "Date:";
37	            signature.Date = DateTime.Now;
38	            signature.LocationInfoLabel = "Location:";
39	            signature.LocationInfo = "USA";
40	            signature.ReasonLabel = "Reason:";
41	            signature.Reason = "I am the author";
42	            signature.DistinguishedNameLabel = "DN:";
43	            signature.DistinguishedName = signature.Certificate.IssuerName.Name;
44	 
45	            //Set the signature image source
46	            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png");
47	 
48	            //Set the signature font
49	            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));
50	 
51	            //Set the document permission to forbid changes but allow form fill
52	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
53	 
54	            //Save to file
55	            doc.SaveToFile("VisiableSignature.pdf");
56	            doc.Close();
57	        }
58	    }
59	}
[VB.NET]

01	Imports System
02	Imports System.Drawing
03	Imports Spire.Pdf
04	Imports Spire.Pdf.Security
05	Imports Spire.Pdf.Graphics
06	  
07	Namespace AddVisibleSignature
08	    Class Program
09	        Shared  Sub Main(ByVal args() As String)
10	            'Create a PdfDocument object
11	            Dim doc As PdfDocument =  New PdfDocument()
12	  
13	            'Load a sample PDF file
14	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
15	  
16	            'Load the certificate
17	            Dim cert As PdfCertificate =  New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
18	  
19	            'Create a PdfSignature object and specify its position and size
20	            Dim signature As PdfSignature =  New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
21	            Dim rectangleF As RectangleF =  New RectangleF(doc.Pages(0).ActualSize.Width - 260 - 54,200,260,110)
22	            signature.Bounds = rectangleF
23	            signature.Certificated = True
24	  
25	            'Set the graphics mode to ImageAndSignDetail
26	            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail
27	  
28	            'Set the signature content
29	            signature.NameLabel = "Signer:"
30	            signature.Name = "Gary"
31	            signature.ContactInfoLabel = "Phone:"
32	            signature.ContactInfo = "0123456"
33	            signature.DateLabel = "Date:"
34	            signature.Date = DateTime.Now
35	            signature.LocationInfoLabel = "Location:"
36	            signature.LocationInfo = "USA"
37	            signature.ReasonLabel = "Reason:"
38	            signature.Reason = "I am the author"
39	            signature.DistinguishedNameLabel = "DN:"
40	            signature.DistinguishedName = signature.Certificate.IssuerName.Name
41	  
42	            'Set the signature image source
43	            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png")
44	  
45	            'Set the signature font
46	            signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12f, FontStyle.Regular))
47	  
48	            'Set the document permission to forbid changes but allow form fill
49	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
50	  
51	            'Save to file
52	            doc.SaveToFile("VisiableSignature.pdf")
53	            doc.Close()
54	        End Sub
55	    End Class
56	End Namespace

从 PDF 中移除数字签名
以下是使用 Spire.PDF for .NET 从 PDF 中移除数字签名的步骤。

  • 创建一个 PdfDocument 对象。
  • 通过 PdfDocument.Form 属性从文档中获取表单部件。
  • 循环查看窗口部件,并确定特定窗口部件是否为 PdfSignatureFieldWidget。
  • 使用PdfFieldCollection.RemoveAt()方法移除签名部件。
  • 使用PdfDocument.SaveToFile()方法将文档保存到另一个PDF文件中。
[C#]
01	using Spire.Pdf;
02	using Spire.Pdf.Widget;
03	 
04	namespace RemoveSignature
05	{
06	    class Program
07	    {
08	        static void Main(string[] args)
09	        {
10	            //Create a PdfDocument object
11	            PdfDocument doc = new PdfDocument("C:\\Users\\Administrator\\Desktop\\VisiableSignature.pdf");
12	 
13	            //Get form widgets from the document
14	            PdfFormWidget widgets = doc.Form as PdfFormWidget;
15	 
16	            //Loop through the widgets
17	            for (int i = 0; i < widgets.FieldsWidget.List.Count; i++)
18	            {
19	                //Get the specific widget
20	                PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget;
21	 
22	                //Determine if the widget is a PdfSignatureFieldWidget
23	                if (widget is PdfSignatureFieldWidget)
24	                {
25	                    //Remove the widget
26	                    widgets.FieldsWidget.RemoveAt(i);
27	                }
28	            }
29	 
30	            //Save the document to another PDF file
31	            doc.SaveToFile("RemoveSignatures.pdf");
32	        }
33	    }
34	}

[VB.NET]

01Imports Spire.Pdf
02	Imports Spire.Pdf.Widget
03	  
04	Namespace RemoveSignature
05	    Class Program
06	        Shared  Sub Main(ByVal args() As String)
07	            'Create a PdfDocument object
08	            Dim doc As PdfDocument =  New PdfDocument("C:\\Users\\Administrator\\Desktop\\VisiableSignature.pdf")
09	  
10	            'Get form widgets from the document
11	            Dim widgets As PdfFormWidget =  doc.Form as PdfFormWidget
12	  
13	            'Loop through the widgets
14	            Dim i As Integer
15	            For  i = 0 To  widgets.FieldsWidget.List.Count- 1  Step  i + 1
16	                'Get the specific widget
17	                Dim widget As PdfFieldWidget =  widgets.FieldsWidget.List(i) as PdfFieldWidget
18	  
19	                'Determine if the widget is a PdfSignatureFieldWidget
20	                If TypeOf widget Is PdfSignatureFieldWidget Then
21	                    'Remove the widget
22	                    widgets.FieldsWidget.RemoveAt(i)
23	                End If
24	            Next
25	  
26	            'Save the document to another PDF file
27	            doc.SaveToFile("RemoveSignatures.pdf")
28	        End Sub
29	    End Class
30	End Namespace

申请临时许可证
若想从生成的文档中删除评估信息,或解除功能限制,申请 30 天试用许可证。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP