本文通过C#程序代码展示如何给PDF文档添加可视化数字签名和不可见数字签名。文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文通过C#程序代码展示如何给PDF文档添加可视化数字签名和不可见数字签名。可视化数字签名,即在PDF文档中的指定页面位置添加签名,包含相关文字信息和签名图片等;不可见数字签名,即添加签名时不在文档中指定签名位置,在文档正文中不显示相关数字签名信息。
这里签名是通过PdfOrdinarySignatureMaker类提供的的接口来实现签名,在添加可视化签名时,通过IPdfSignatureAppearance类提供的接口来设置签名的具体位置,签名图片等。
下面是具体实现方法和步骤,供参考。
1. 在VS程序中通过Nuget安装 Spire.PDF for .NET 版本7.8.2,安装步骤如下:
鼠标右键点击“引用”,“管理Nuget包”
点击“浏览”,在搜索框中输入“Spire.PDF ”,点击选择搜索到的包,点击“安装”。
在弹出的窗口点击“OK”
等待安装完成。安装结果如图:
2.在VS程序中添加如下代码添加数字签名
2.1 添加可视化数字签名
C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
namespace VisibleDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//加载PDF测试文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("test.pdf");
//获取PDF文档中的第5页
PdfPageBase page = pdf.Pages[4];
//在页面中的指定位置添加可视化签名
X509Certificate2 x509 = new X509Certificate2("cer.pfx", "e-iceblue");
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
IPdfSignatureAppearance signatureAppearance = new PdfCustomSignatureAppearance();
signatureMaker.MakeSignature("signName_" , page, page.ActualSize.Width-240,page.ActualSize.Height-200,150,150, signatureAppearance);
//保存文档
pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf");
pdf.Close();
}
public class PdfCustomSignatureAppearance : IPdfSignatureAppearance
{
public void Generate(PdfCanvas g)
{
float fontSize = 15;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", fontSize), true);
float lineHeight = fontSize; g.DrawString("Sign Here:", font, PdfBrushes.Black, new PointF(0, 0));
g.DrawImage(PdfImage.FromFile("sign.png"), new PointF(20, 20));
}
}
}
}
Vb.net
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates
Namespace VisibleDigitalSignature
Class Program
Private Shared Sub Main(args As String())
'加载PDF测试文档
Dim pdf As New PdfDocument()
pdf.LoadFromFile("test.pdf")
'获取PDF文档中的第5页
Dim page As PdfPageBase = pdf.Pages(4)
'在页面中的指定位置添加可视化签名
Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
Dim signatureAppearance As IPdfSignatureAppearance = New PdfCustomSignatureAppearance()
signatureMaker.MakeSignature("signName_", page, page.ActualSize.Width - 240, page.ActualSize.Height - 200, 150, 150, _
signatureAppearance)
'保存文档
pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf")
pdf.Close()
End Sub
Public Class PdfCustomSignatureAppearance
Implements IPdfSignatureAppearance
Public Sub Generate(g As PdfCanvas)
Dim fontSize As Single = 15
Dim font As New PdfTrueTypeFont(New Font("Arial", fontSize), True)
Dim lineHeight As Single = fontSize
g.DrawString("Sign Here:", font, PdfBrushes.Black, New PointF(0, 0))
g.DrawImage(PdfImage.FromFile("sign.png"), New PointF(20, 20))
End Sub
End Class
End Class
End Namespace
可视化数字签名效果:
2.2 添加不可见数字签名
C#
using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;
namespace InvisibleDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//初始化一个PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.LoadFromFile("test.pdf");
//添加签名
X509Certificate2 x509 = new X509Certificate2("cer.pfx", "e-iceblue");
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
signatureMaker.MakeSignature("signName");
signatureMaker.SetLocation("Chengdu");
signatureMaker.SetReason("Edit limited");
//保存文档
pdf.SaveToFile("result.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");
}
}
}
Vb.net
Imports Spire.Pdf
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Security.Cryptography.X509Certificates
Namespace InvisibleDigitalSignature
Class Program
Private Shared Sub Main(args As String())
'初始化一个PdfDocument实例
Dim pdf As New PdfDocument()
'加载PDF文档
pdf.LoadFromFile("test.pdf")
'添加签名
Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
signatureMaker.MakeSignature("signName")
signatureMaker.SetLocation("Chengdu")
signatureMaker.SetReason("Edit limited")
'保存文档
pdf.SaveToFile("result.pdf", FileFormat.PDF)
System.Diagnostics.Process.Start("result.pdf")
End Sub
End Class
End Namespace
不可见数字签名效果:
到此这篇关于C#和vb.net实现PDF 添加可视化和不可见数字签名的文章就介绍到这了,更多相关C# PDF 数字签名内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
本文标题为:C#和vb.net实现PDF 添加可视化和不可见数字签名
基础教程推荐
- C#控制台实现飞行棋小游戏 2023-04-22
- winform把Office转成PDF文件 2023-06-14
- C# 调用WebService的方法 2023-03-09
- C# List实现行转列的通用方案 2022-11-02
- C#类和结构详解 2023-05-30
- ZooKeeper的安装及部署教程 2023-01-22
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# windows语音识别与朗读实例 2023-04-27