为了账号安全,请及时绑定邮箱和手机立即绑定

C# 给PDF签名时添加时间戳的2种方法(附VB.NET代码)

标签:
C# .NET

在PDF添加签名时,支持添加可信时间戳来保证文档的法律效应。本文,将通过C#程序代码介绍如何添加可信时间戳,可通过2种方法来实现。文中附上VB.NET代码,有需可供参考。

一、程序运行环境

编译环境:Visual Studio/.NetFramework 4.8

所需测试文件:PDF、.pfx数字证书及秘钥、PDF类库(spire.pdf.dll 7.5.7)

可信时间戳服务器:URL地址

:VS程序中,在“解决方案资源管理器”中先引用spire.pdf.dll文件,如下图,

https://img1.sycdn.imooc.com/60c178320001f22004010609.jpg

这里可以通过Nuget下载该文件。或者在手动下载引用,dll文件在下载后的文件包BIN文件夹下,如本次测试使用的是.NET4.0下面的dll,即spire.pdf_7.5.7\BIN\NET4.0\Spire.Pdf.dll,如图:

https://img3.sycdn.imooc.com/60c1784d0001cddf08740424.jpg

二、程序代码

添加签名时,这里可分别通过两个类来实现,即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

签名时间戳添加结果:

https://img1.sycdn.imooc.com/60c17871000140a811240681.jpg

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

签名时间戳添加结果:

https://img2.sycdn.imooc.com/60c178a60001651011320674.jpg

以上是本文关于如何在C#程序中给PDF签名添加时间戳的方法。


内容原创,如需转载请务必注明出处!


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消