C#和vb.net实现PDF|C#和vb.net实现PDF 添加可视化和不可见数字签名
目录
- 1. 在VS程序中通过Nuget安装 Spire.PDF for .NET 版本7.8.2,安装步骤如下:
- 2.在VS程序中添加如下代码添加数字签名
- 2.1 添加可视化数字签名
- 2.2 添加不可见数字签名
这里签名是通过PdfOrdinarySignatureMaker类提供的的接口来实现签名,在添加可视化签名时,通过IPdfSignatureAppearance类提供的接口来设置签名的具体位置,签名图片等。
下面是具体实现方法和步骤,供参考。
【C#和vb.net实现PDF|C#和vb.net实现PDF 添加可视化和不可见数字签名】
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.PdfImports Spire.Pdf.GraphicsImports Spire.Pdf.Interactive.DigitalSignaturesImports System.DrawingImports System.Security.Cryptography.X509CertificatesNamespace VisibleDigitalSignatureClass ProgramPrivate 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 SubPublic Class PdfCustomSignatureAppearanceImplements IPdfSignatureAppearancePublic Sub Generate(g As PdfCanvas)Dim fontSize As Single = 15Dim font As New PdfTrueTypeFont(New Font("Arial", fontSize), True)Dim lineHeight As Single = fontSizeg.DrawString("Sign Here:", font, PdfBrushes.Black, New PointF(0, 0))g.DrawImage(PdfImage.FromFile("sign.png"), New PointF(20, 20))End SubEnd ClassEnd ClassEnd 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.PdfImports Spire.Pdf.Interactive.DigitalSignaturesImports System.Security.Cryptography.X509CertificatesNamespace InvisibleDigitalSignatureClass ProgramPrivate 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 SubEnd ClassEnd Namespace
不可见数字签名效果:
文章图片
到此这篇关于C#和vb.net实现PDF 添加可视化和不可见数字签名的文章就介绍到这了,更多相关C# PDF 数字签名内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 急于表达——往往欲速则不达
- 第三节|第三节 快乐和幸福(12)
- 20170612时间和注意力开销记录
- 2.6|2.6 Photoshop操作步骤的撤消和重做 [Ps教程]
- 对称加密和非对称加密的区别
- 眼光要放高远
- 樱花雨
- 前任
- 2020-04-07vue中Axios的封装和API接口的管理
- 烦恼和幸福