赞
踩
数字签名确保签名的文档不能被除其作者之外的任何人更改。添加签名是确保文档内容真实性的最常见方法。PDF 文档中的可视数字签名可以显示文本或图像(例如手写签名)。本文从以下三个方面介绍如何使用Spire.PDF for .NET对PDF 进行数字签名。
Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下载 Spire.PDF for java下载
首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.PDF
以下是将纯文本签名添加到 PDF 文档的步骤。
【C#】
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;namespace AddTextSignature
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();//Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");//Load the certificate
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");//Create a PdfSignature object and specify its position and size
PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count-1], cert, "MySignature");
RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 340, 150, 290, 100);
signature.Bounds = rectangleF;
signature.Certificated = true;//Set the graphics mode to sign detail
signature.GraphicsMode = GraphicMode.SignDetail;//Set the signature content
signature.NameLabel = "Signer:";
signature.Name = "Gary";
signature.ContactInfoLabel = "Phone:";
signature.ContactInfo = "0123456";
signature.DateLabel = "Date:";
signature.Date = DateTime.Now;
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "USA";
signature.ReasonLabel = "Reason:";
signature.Reason = "I am the author";
signature.DistinguishedNameLabel = "DN:";
signature.DistinguishedName = signature.Certificate.IssuerName.Name;//Set the signature font
signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",12f,FontStyle.Regular));//Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;//Save to file
doc.SaveToFile("TextSignature.pdf");
doc.Close();
}
}
}
【VB.NET】
Imports Spire.Pdf
Imports Spire.Pdf.Security
Imports System
Imports System.Drawing
Imports Spire.Pdf.Graphics
Namespace AddTextSignature
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
'Load the certificate
Dim cert As PdfCertificate = New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
'Create a PdfSignature object and specify its position and size
Dim signature As PdfSignature = New PdfSignature(doc,doc.Pages(doc.Pages.Count-1),cert,"MySignature")
Dim rectangleF As RectangleF = New RectangleF(doc.Pages(0).ActualSize.Width - 340,150,290,100)
signature.Bounds = rectangleF
signature.Certificated = True
'Set the graphics mode to sign detail
signature.GraphicsMode = GraphicMode.SignDetail
'Set the signature content
signature.NameLabel = "Signer:"
signature.Name = "Gary"
signature.ContactInfoLabel = "Phone:"
signature.ContactInfo = "0123456"
signature.DateLabel = "Date:"
signature.Date = DateTime.Now
signature.LocationInfoLabel = "Location:"
signature.LocationInfo = "USA"
signature.ReasonLabel = "Reason:"
signature.Reason = "I am the author"
signature.DistinguishedNameLabel = "DN:"
signature.DistinguishedName = signature.Certificate.IssuerName.Name
'Set the signature font
signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS",12f,FontStyle.Regular))
'Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
'Save to file
doc.SaveToFile("TextSignature.pdf")
doc.Close()
End Sub
End Class
End Namespace
以下是将图像签名添加到 PDF 文档的步骤。
【C#】
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
using System.Drawing;namespace AddImageSignature
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();//Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");//Load the certificate
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");//Create a PdfSignature object and specify its position and size
PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 200, 150, 130, 130);
signature.Bounds = rectangleF;
signature.Certificated = true;//Set the graphics mode to image only
signature.GraphicsMode = GraphicMode.SignImageOnly;//Set the sign image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png");//Set the signature font
signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));//Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;//Save to file
doc.SaveToFile("ImageSignature.pdf");
doc.Close();
}
}
}
【VB.NET】
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Security
Imports System.Drawing
Namespace AddImageSignature
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
'Load the certificate
Dim cert As PdfCertificate = New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
'Create a PdfSignature object and specify its position and size
Dim signature As PdfSignature = New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
Dim rectangleF As RectangleF = New RectangleF(doc.Pages(0).ActualSize.Width - 200,150,130,130)
signature.Bounds = rectangleF
signature.Certificated = True
'Set the graphics mode to image only
signature.GraphicsMode = GraphicMode.SignImageOnly
'Set the sign image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png")
'Set the signature font
signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12f, FontStyle.Regular))
'Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
'Save to file
doc.SaveToFile("ImageSignature.pdf")
doc.Close()
End Sub
End Class
End Namespace
以下是使用文本和图像对 PDF 文档进行数字签名的步骤。
【C#】
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;namespace AddTextAndImageSignature
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();//Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");//Load the certificate
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");//Create a PdfSignature object and specify its position and size
PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 320, 150, 260, 110);
signature.Bounds = rectangleF;
signature.Certificated = true;//Set the graphics mode to image and sign detail
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;//Set the signature content
signature.NameLabel = "Signer:";
signature.Name = "Gary";
signature.ContactInfoLabel = "Phone:";
signature.ContactInfo = "0123456";
signature.DateLabel = "Date:";
signature.Date = DateTime.Now;
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "USA";
signature.ReasonLabel = "Reason:";
signature.Reason = "I am the author";
signature.DistinguishedNameLabel = "DN:";
signature.DistinguishedName = signature.Certificate.IssuerName.Name;//Set the signature image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handSignature.png");//Set the signature font
signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));//Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;//Save to file
doc.SaveToFile("TextAndImageSignature.pdf");
doc.Close();
}
}
}
【VB.NET】
Imports Spire.Pdf
Imports Spire.Pdf.Security
Imports System
Imports System.Drawing
Imports Spire.Pdf.Graphics
Namespace AddTextAndImageSignature
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")
'Load the certificate
Dim cert As PdfCertificate = New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")
'Create a PdfSignature object and specify its position and size
Dim signature As PdfSignature = New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
Dim rectangleF As RectangleF = New RectangleF(doc.Pages(0).ActualSize.Width - 320,150,260,110)
signature.Bounds = rectangleF
signature.Certificated = True
'Set the graphics mode to image and sign detail
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail
'Set the signature content
signature.NameLabel = "Signer:"
signature.Name = "Gary"
signature.ContactInfoLabel = "Phone:"
signature.ContactInfo = "0123456"
signature.DateLabel = "Date:"
signature.Date = DateTime.Now
signature.LocationInfoLabel = "Location:"
signature.LocationInfo = "USA"
signature.ReasonLabel = "Reason:"
signature.Reason = "I am the author"
signature.DistinguishedNameLabel = "DN:"
signature.DistinguishedName = signature.Certificate.IssuerName.Name
'Set the signature image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handSignature.png")
'Set the signature font
signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12f, FontStyle.Regular))
'Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill
'Save to file
doc.SaveToFile("TextAndImageSignature.pdf")
doc.Close()
End Sub
End Class
End Namespace
以上便是如何使用文本或/和图像对 PDF 进行数字签名,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。