使用文本或/和图像对 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交谈
在线咨询

渝公网安备
50010702500608号

客服热线