添加或刪除 PDF 中的數字簽名
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入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 文件中。
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文件中。
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 天試用許可證。
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線