本文介绍使用Spire.PDF for .NET在PDF文档中添加可视化数字签名和不可见数字签名的方法 。
1、添加可视化数字签名
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
namespace VisibleDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//加载PDF测试文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");
//获取PDF文档中的第二页
PdfPageBase page = pdf.Pages[1];
//在页面中的指定位置添加可视化签名
X509Certificate2 x509 = new X509Certificate2("cer.pfx", "e-iceblue");
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
IPdfSignatureAppearance signatureAppearance = new PdfCustomSignatureAppearance();
signatureMaker.MakeSignature("signName_" , page, page.ActualSize.Width-300,page.ActualSize.Height-300,150,150, signatureAppearance);
//保存文档
pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf");
pdf.Close();
}
public class PdfCustomSignatureAppearance : IPdfSignatureAppearance
{
public void Generate(PdfCanvas g)
{
float fontSize = 15;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", fontSize), true);
float lineHeight = fontSize; g.DrawString("Sign Here:", font, PdfBrushes.Black, new PointF(0, 0));
g.DrawImage(PdfImage.FromFile("sign.png"), new PointF(20, 20));
}
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Namespace VisibleDigitalSignature
Class Program
Private Shared Sub Main(args As String())
'加载PDF测试文档
Dim pdf As New PdfDocument()
pdf.LoadFromFile("sample.pdf")
'获取PDF文档中的第二页
Dim page As PdfPageBase = pdf.Pages(1)
'在页面中的指定位置添加可视化签名
Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
Dim signatureAppearance As IPdfSignatureAppearance = New PdfCustomSignatureAppearance()
signatureMaker.MakeSignature("signName_", page, page.ActualSize.Width - 300, page.ActualSize.Height - 300, 150, 150, _
signatureAppearance)
'保存文档
pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf")
pdf.Close()
End Sub
Public Class PdfCustomSignatureAppearance
Implements IPdfSignatureAppearance
Public Sub Generate(g As PdfCanvas)
Dim fontSize As Single = 15
Dim font As New PdfTrueTypeFont(New Font("Arial", fontSize), True)
Dim lineHeight As Single = fontSize
g.DrawString("Sign Here:", font, PdfBrushes.Black, New PointF(0, 0))
g.DrawImage(PdfImage.FromFile("sign.png"), New PointF(20, 20))
End Sub
End Class
End Class
End Namespace
可视化数字签名效果:
2、添加不可见数字签名
C#
using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;
namespace InvisibleDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//初始化一个PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.LoadFromFile("sample.pdf");
//添加签名
X509Certificate2 x509 = new X509Certificate2(@"cer.pfx", "e-iceblue");
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
signatureMaker.MakeSignature("signName");
signatureMaker.SetLocation("Chengdu");
signatureMaker.SetReason("Edit limited");
//保存文档
pdf.SaveToFile("result.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Security.Cryptography.X509Certificates
Namespace InvisibleDigitalSignature
Class Program
Private Shared Sub Main(args As String())
'初始化一个PdfDocument实例
Dim pdf As New PdfDocument()
'加载PDF文档
pdf.LoadFromFile("sample.pdf")
'添加签名
Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
signatureMaker.MakeSignature("signName")
signatureMaker.SetLocation("Chengdu")
signatureMaker.SetReason("Edit limited")
'保存文档
pdf.SaveToFile("result.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("result.pdf")
End Sub
End Class
End Namespace
数字签名效果: