这篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。