C#和vb.net實現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 數字簽名內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C#實現PDF簽名時添加時間戳的2種方法(附VB.NET代碼)
- 利用C#/VB.NET實現將PDF轉為Word
- C#實現在PDF文檔中應用多種不同字體
- Java實現替換PDF中的字體功能
- C#實現從PDF中提取表格的方法詳解