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

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

一、程序运行环境

  • 编译环境:Visual Studio/.NetFramework 4.8
  • 所需测试文件:PDF、.pfx数字证书及秘钥、PDF类库(Spire.PDF for  .NET
  • 可信时间戳服务器: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签名添加时间戳的方法。

 

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

 

posted @ 2021-05-25 14:05  E-iceblue  阅读(1136)  评论(0编辑  收藏  举报