使用文本或/和圖像對 PDF 進行數字簽名
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
數字簽名可確保已簽名的文件不會被作者以外的任何人篡改。添加簽名是確保文檔內容真實性的最常用方法。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 進行數字簽名
以下是為 PDF 文檔添加純文本簽名的步驟。
- 創建一個PdfDocument對象,并使用PdfDocument.LoadFromFile()方法加載一個PDF樣本文件。
- 在初始化PdfCertificate對象時加載一個.pfx證書文件。
- 創建一個 PdfSignature 對象,指定其在文檔中的位置和大小。
- 將簽名圖形模式設置為 "SignDetail",即用純文本顯示簽名細節。
- 通過 PdfSignature 對象下的屬性設置簽名細節,包括姓名、聯系信息、原因、日期等。
- 將認證文檔的權限設置為 ForbidChanges(禁止更改)和 AllowFormFill(允許填寫表格)。
- 使用PdfDocument.SaveToFile()方法將文檔保存到另一個文件中。
01	using Spire.Pdf;
02	using Spire.Pdf.Security;
03	using System;
04	using System.Drawing;
05	using Spire.Pdf.Graphics;
06	 
07	namespace AddTextSignature
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 - 340, 150, 290, 100);
25	            signature.Bounds = rectangleF;
26	            signature.Certificated = true;
27	 
28	            //Set the graphics mode to sign detail
29	            signature.GraphicsMode = GraphicMode.SignDetail;
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 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("TextSignature.pdf");
53	            doc.Close();
54	        }
55	    }
56	}
[VB.NET]
01	Imports Spire.Pdf
02	Imports Spire.Pdf.Security
03	Imports System
04	Imports System.Drawing
05	Imports Spire.Pdf.Graphics
06	  
07	Namespace AddTextSignature
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 - 340,150,290,100)
22	            signature.Bounds = rectangleF
23	            signature.Certificated = True
24	  
25	            'Set the graphics mode to sign detail
26	            signature.GraphicsMode = GraphicMode.SignDetail
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 font
43	            signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS",12f,FontStyle.Regular))
44	  
45	            'Set the document permission to forbid changes but allow form fill
46	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
47	  
48	            'Save to file
49	            doc.SaveToFile("TextSignature.pdf")
50	            doc.Close()
51	        End Sub
52	    End Class
53	End Namespace
  
 
	
	用圖像對 PDF 進行數字簽名
以下是在 PDF 文檔中添加圖像簽名的步驟。
- 創建一個PdfDocument對象,并使用PdfDocument.LoadFromFile()方法加載一個PDF樣本文件。
- 在初始化PdfCertificate對象時加載一個.pfx證書文件。
- 創建一個 PdfSignature 對象,指定其在文檔中的位置和大小。
- 將簽名圖形模式設置為SignImageOnly,即只顯示簽名圖像。
- 通過PdfSignature.SignImageSource屬性設置簽名圖像。
- 將認證文檔的權限設置為 "禁止更改 "和 "允許填寫表格"。
- 使用PdfDocument.SaveToFile()方法將文檔保存到另一個文件中。
[C#]
01	using Spire.Pdf;
02	using Spire.Pdf.Graphics;
03	using Spire.Pdf.Security;
04	using System.Drawing;
05	 
06	namespace AddImageSignature
07	{
08	    class Program
09	    {
10	        static void Main(string[] args)
11	        {
12	            //Create a PdfDocument object
13	            PdfDocument doc = new PdfDocument();
14	 
15	            //Load a sample PDF file
16	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
17	 
18	            //Load the certificate
19	            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
20	 
21	            //Create a PdfSignature object and specify its position and size
22	            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
23	            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 200, 150, 130, 130);
24	            signature.Bounds = rectangleF;
25	            signature.Certificated = true;
26	 
27	            //Set the graphics mode to image only
28	            signature.GraphicsMode = GraphicMode.SignImageOnly;
29	 
30	            //Set the sign image source
31	            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png");
32	 
33	            //Set the signature font
34	            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));
35	 
36	            //Set the document permission to forbid changes but allow form fill
37	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
38	 
39	            //Save to file
40	            doc.SaveToFile("ImageSignature.pdf");
41	            doc.Close();
42	        }
43	    }
44	}
[VB.NET]
01	Imports Spire.Pdf
02	Imports Spire.Pdf.Graphics
03	Imports Spire.Pdf.Security
04	Imports System.Drawing
05	  
06	Namespace AddImageSignature
07	    Class Program
08	        Shared  Sub Main(ByVal args() As String)
09	            'Create a PdfDocument object
10	            Dim doc As PdfDocument =  New PdfDocument()
11	  
12	            'Load a sample PDF file
13	            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
14	  
15	            'Load the certificate
16	            Dim cert As PdfCertificate =  New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
17	  
18	            'Create a PdfSignature object and specify its position and size
19	            Dim signature As PdfSignature =  New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
20	            Dim rectangleF As RectangleF =  New RectangleF(doc.Pages(0).ActualSize.Width - 200,150,130,130)
21	            signature.Bounds = rectangleF
22	            signature.Certificated = True
23	  
24	            'Set the graphics mode to image only
25	            signature.GraphicsMode = GraphicMode.SignImageOnly
26	  
27	            'Set the sign image source
28	            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png")
29	  
30	            'Set the signature font
31	            signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12f, FontStyle.Regular))
32	  
33	            'Set the document permission to forbid changes but allow form fill
34	            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
35	  
36	            'Save to file
37	            doc.SaveToFile("ImageSignature.pdf")
38	            doc.Close()
39	        End Sub
40	    End Class
41	End Namespace
	 
 
	使用文本和圖像對 PDF 進行數字簽名
以下是對 PDF 文檔進行文本和圖像數字簽名的步驟。
- 創建一個 PdfDocument 對象,并使用 PdfDocument.LoadFromFile() 方法加載一個示例 PDF 文件。
- 在初始化PdfCertificate對象時加載一個.pfx證書文件。
- 創建一個 PdfSignature 對象,指定其在文檔中的位置和大小。
- 將簽名圖形模式設置為SignImageAndSignDetail,即同時顯示簽名圖像和細節。
- 通過PdfSignature.SignImageSource屬性設置簽名圖像,并通過PdfSignature對象下的其他屬性設置簽名細節,包括姓名、聯系信息、原因、日期等。
- 將認證文檔的權限設置為 ForbidChanges 和 AllowFormFill。
- 使用PdfDocument.SaveToFile()方法將文檔保存到另一個文件中。
01	using Spire.Pdf;
02	using Spire.Pdf.Security;
03	using System;
04	using System.Drawing;
05	using Spire.Pdf.Graphics;
06	 
07	namespace AddTextAndImageSignature
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 - 320, 150, 260, 110);
25	            signature.Bounds = rectangleF;
26	            signature.Certificated = true;
27	 
28	            //Set the graphics mode to image and sign detail
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\\handSignature.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("TextAndImageSignature.pdf");
56	            doc.Close();
57	        }
58	    }
59	}
[VB.NET]
01	Imports Spire.Pdf
02	Imports Spire.Pdf.Security
03	Imports System
04	Imports System.Drawing
05	Imports Spire.Pdf.Graphics
06	  
07	Namespace AddTextAndImageSignature
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 - 320,150,260,110)
22	            signature.Bounds = rectangleF
23	            signature.Certificated = True
24	  
25	            'Set the graphics mode to image and sign detail
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\\handSignature.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("TextAndImageSignature.pdf")
53	            doc.Close()
54	        End Sub
55	    End Class
56	End Namespace
 
 
	
		申請臨時許可證
若想從生成的文檔中刪除評估信息,或解除功能限制,申請 30 天試用許可證。
	

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