本文通過C#程式程式碼展示如何給PDF文件新增視覺化數字簽名和不可見數字簽名。視覺化數字簽名,即在PDF文件中的指定頁面位置新增簽名,包含相關文字資訊和簽名圖片等;不可見數字簽名,即新增簽名時不在文件中指定簽名位置,在文件正文中不顯示相關數字簽名資訊。
這裡簽名是通過PdfOrdinarySignatureMaker類提供的的介面來實現簽名,在新增視覺化簽名時,通過IPdfSignatureAppearance類提供的介面來設定簽名的具體位置,簽名圖片等。
下面是具體實現方法和步驟,供參考。
1. 在VS程式中通過Nuget安裝 Spire.PDF for .NET 版本7.8.2,安裝步驟如下:
滑鼠右鍵點選“引用”,“管理Nuget包”
點選“瀏覽”,在搜尋框中輸入“Spire.PDF ”,點選選擇搜尋到的包,點選“安裝”。
在彈出的視窗點選“OK”
等待安裝完成。安裝結果如圖:
2.在VS程式中新增如下程式碼新增數字簽名
2.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("test.pdf"); //獲取PDF文件中的第5頁 PdfPageBase page = pdf.Pages[4]; //在頁面中的指定位置新增視覺化簽名 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-240,page.ActualSize.Height-200,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("test.pdf") '獲取PDF文件中的第5頁 Dim page As PdfPageBase = pdf.Pages(4) '在頁面中的指定位置新增視覺化簽名 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 - 240, page.ActualSize.Height - 200, 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.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("test.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("test.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
不可見數字簽名效果:
—End—