您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#如何實現PDF簽名時添加時間戳的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
編譯環境: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類。下面分別以兩個代碼示例展示如何簽名并添加時間戳。
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
簽名時間戳添加結果:
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簽名時添加時間戳”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。