在PDF新增簽名時,支援新增可信時間戳來保證文件的法律效應。本文,將通過C#程式程式碼介紹如何新增可信時間戳,可通過2種方法來實現。文中附上VB.NET程式碼,有需可供參考。
一、程式執行環境
- 編譯環境:Visual Studio/.NetFramework 4.8
- 所需測試檔案:PDF、.pfx數字證書及祕鑰、PDF類庫(spire.pdf.dll 7.5.7)
- 可信時間戳伺服器:URL地址
注:VS程式中,在“解決方案資源管理器中”先引用spire.pdf.dll檔案,如下圖,
這裡可以通過Nuget下載該檔案。或者在手動下載引用,dll檔案在下載後的檔案包BIN資料夾下,如本次測試使用的是.NET4.0下面的dll,即spire.pdf_7.5.7\BIN\NET4.0\Spire.Pdf.dll,如圖:
二、程式程式碼
新增簽名時,這裡可分別通過兩個類來實現,即PdfSignature 和 PdfOrdinarySignatureMaker類。下面分別以兩個程式碼示例展示如何簽名並新增時間戳。
1. 通過PdfSignature類來簽名,新增時間戳
C#
using Spire.Pdf; using Spire.Pdf.Security; using System.Drawing; namespace TimeStamp { class Program { static void Main(string[] args) { //載入PDF文件 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("sample.pdf"); //獲取第2頁 PdfPageBase page = pdf.Pages[1]; //載入數字證書(.pfx檔案及祕鑰) PdfCertificate cert = new PdfCertificate("test.pfx", "123456"); //載入簽名圖片 Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromFile("sign.png"); //新增與圖片大小相同的簽名到第2頁指定座標位置 PdfSignature signature = new PdfSignature(page.Document, page, cert, "sign1");//新增數字簽名 signature.Bounds = new RectangleF(new PointF(400,700), new SizeF(image.Width, image.Height));//設定簽名的頁面座標位置、大小與圖片相同 signature.SignImageSource = image; signature.GraphicsMode = Spire.Pdf.Security.GraphicMode.SignImageAndSignDetail;//設定簽名為圖片模式 signature.SignImageLayout = Spire.Pdf.Security.SignImageLayout.Stretch; signature.DocumentPermissions = Spire.Pdf.Security.PdfCertificationFlags.AllowFormFill | Spire.Pdf.Security.PdfCertificationFlags.AllowComments;//文件許可權 //配置時間戳伺服器,新增時間戳 string url = "Your TimeStamp Server URL"; signature.ConfigureTimestamp(url); //signature.ConfigureTimestamp(url, "Author", "123654");//新增時間戳時設定使用者名稱和密碼 //儲存文件 pdf.SaveToFile("TimeStamp1.pdf",FileFormat.PDF); System.Diagnostics.Process.Start("TimeStamp1.pdf"); } } }
VB.NET
Imports Spire.Pdf Imports Spire.Pdf.Security Imports System.Drawing Namespace TimeStamp Class Program Private Shared Sub Main(args As String()) '載入PDF文件 Dim pdf As New PdfDocument() pdf.LoadFromFile("sample.pdf") '獲取第2頁 Dim page As PdfPageBase = pdf.Pages(1) '載入數字證書(.pfx檔案及祕鑰) Dim cert As New PdfCertificate("test.pfx", "123456") '載入簽名圖片 Dim image As Spire.Pdf.Graphics.PdfImage = Spire.Pdf.Graphics.PdfImage.FromFile("sign.png") '新增與圖片大小相同的簽名到第2頁指定座標位置 Dim signature As New PdfSignature(page.Document, page, cert, "sign1") '新增數字簽名 signature.Bounds = New RectangleF(New PointF(400, 700), New SizeF(image.Width, image.Height)) '設定簽名的頁面座標位置、大小與圖片相同 signature.SignImageSource = image signature.GraphicsMode = Spire.Pdf.Security.GraphicMode.SignImageAndSignDetail '設定簽名為圖片模式 signature.SignImageLayout = Spire.Pdf.Security.SignImageLayout.Stretch signature.DocumentPermissions = Spire.Pdf.Security.PdfCertificationFlags.AllowFormFill Or Spire.Pdf.Security.PdfCertificationFlags.AllowComments'文件許可權 '配置時間戳伺服器,新增時間戳 Dim url As String = "Your TimeStamp Server URL" signature.ConfigureTimestamp(url) 'signature.ConfigureTimestamp(url, "Author", "123654");//新增時間戳時設定使用者名稱和密碼 '儲存文件 pdf.SaveToFile("TimeStamp1.pdf", FileFormat.PDF) System.Diagnostics.Process.Start("TimeStamp1.pdf") End Sub End Class End Namespace
簽名時間戳新增結果:
2. 通過PdfOrdinarySignatureMaker類來簽名,新增時間戳
C#
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Interactive.DigitalSignatures; using System.Drawing; using System.Security.Cryptography.X509Certificates; namespace TimeStamp2 { class Program { static void Main(string[] args) { //載入PDF文件 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //載入pfx數字證書及祕鑰 X509Certificate2 x509 = new X509Certificate2("test.pfx", "123456"); //例項化PdfPKCS7Formatter類的物件 PdfPKCS7Formatter formatter = new PdfPKCS7Formatter(x509, false); formatter.TimestampService = new TSAHttpService("Your TimeStamp Server URL");//配置時間戳伺服器 //新增簽名 PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, formatter); IPdfSignatureAppearance signatureAppearance = new PdfCustomSignatureAppearance(); signatureMaker.MakeSignature("sign", doc.Pages[1], 400, 650, 150, 80, signatureAppearance); //儲存簽名後的PDF文件 doc.SaveToFile("TimeStamp2.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("TimeStamp2.pdf"); } public class PdfCustomSignatureAppearance : IPdfSignatureAppearance { public void Generate(PdfCanvas canvas) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12), true); canvas.DrawString("SIGNED! A CLASS.\n By Tom Smith", font, PdfBrushes.Red, new PointF(0, 0)); } } } }
VB.NET
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports Spire.Pdf.Interactive.DigitalSignatures Imports System.Drawing Imports System.Security.Cryptography.X509Certificates Namespace TimeStamp2 Class Program Private Shared Sub Main(args As String()) '載入PDF文件 Dim doc As New PdfDocument() doc.LoadFromFile("sample.pdf") '載入pfx數字證書及祕鑰 Dim x509 As New X509Certificate2("test.pfx", "123456") '例項化PdfPKCS7Formatter類的物件 Dim formatter As New PdfPKCS7Formatter(x509, False) formatter.TimestampService = New TSAHttpService("Your TimeStamp Server URL") '配置時間戳伺服器 '新增簽名 Dim signatureMaker As New PdfOrdinarySignatureMaker(doc, formatter) Dim signatureAppearance As IPdfSignatureAppearance = New PdfCustomSignatureAppearance() signatureMaker.MakeSignature("sign", doc.Pages(1), 400, 650, 150, 80, _ signatureAppearance) '儲存簽名後的PDF文件 doc.SaveToFile("TimeStamp2.pdf", FileFormat.PDF) System.Diagnostics.Process.Start("TimeStamp2.pdf") End Sub Public Class PdfCustomSignatureAppearance Implements IPdfSignatureAppearance Public Sub Generate(canvas As PdfCanvas) Dim font As New PdfTrueTypeFont(New Font("Arial", 12), True) canvas.DrawString("SIGNED! A CLASS." & vbLf & " By Tom Smith", font, PdfBrushes.Red, New PointF(0, 0)) End Sub End Class End Class End Namespace
簽名時間戳新增結果:
以上是本文關於如何在C#程式中給PDF簽名新增時間戳的方法。
內容原創,如需轉載請務必註明出處!