这期内容当中小编将会给大家带来有关C#中怎么给pdf 添加可视化和不可见数字签名,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1. 在VS程序中通过Nuget安装 Spire.PDF for .net 版
这期内容当中小编将会给大家带来有关C#中怎么给pdf 添加可视化和不可见数字签名,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
鼠标右键点击“引用”,“管理Nuget包”
点击“浏览”,在搜索框中输入“Spire.PDF ”,点击选择搜索到的包,点击“安装”。
在弹出的窗口点击“OK”
等待安装完成。安装结果如图:
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)); } } }}
Imports Spire.PdfImports Spire.Pdf.GraphicsImports Spire.Pdf.Interactive.DigitalSignaturesImports System.DrawingImports System.Security.Cryptography.X509CertificatesNamespace 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 ClassEnd Namespace
可视化数字签名效果:
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 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 ClassEnd Namespace
不可见数字签名效果:
上述就是小编为大家分享的C#中怎么给PDF 添加可视化和不可见数字签名了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网精选频道。
--结束END--
本文标题: C#中怎么给PDF 添加可视化和不可见数字签名
本文链接: https://lsjlt.com/news/300102.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0