數字簽名廣泛用於保護PDF文檔,可見數字簽名在日常生活中是相當重要的。在這篇文章中我將與大家分享如何給PDF文件添加可見的數字簽名。
首先我下載了一個由E-iceblue公司開發的免費版的PDF組件-Free Spire.PDF,控件安裝好后,再通過下面所提及的路徑把Bin文件夾里的Spire.PDF.dll添加為引用。
路徑:"...\Spire.pdf-fe\Bin\NET4.0\ Spire.PDF.dll"
接下來我將提供一些代碼片段來向大家展示如何添加可見的數字簽名:
步驟1:新建一個PDF文檔並加載一個PDF證書
//新建一個PDF文檔對象,再添加一個新頁面。 PdfDocument doc = new PdfDocument(); doc.Pages.Add(); //加載一個PDF證書 PdfCertificate cert = new PdfCertificate(@"C:\Users\Administrator\Desktop\gary.pfx", "e-iceblue");
步驟2:添加數字簽名並設置數字簽名的位置
//添加數字簽名 var signature = new PdfSignature(doc, doc.Pages[0], cert, "Requestd1"); //設置數字簽名的位置 signature.Bounds = new RectangleF(new PointF(280, 600), new SizeF(260, 90));
步驟3:設置顯示文本屬性
signature.IsTag = true;
步驟4:填充數字簽名的內容
signature.DigitalSignerLable = "Digitally signed by"; signature.DigitalSigner = "Gary for Test"; signature.DistinguishedName = "DN:"; signature.LocationInfoLabel = "Location:"; signature.LocationInfo = "London"; signature.ReasonLabel = "Reason: "; signature.Reason = "Le document est certifie"; signature.DateLabel = "Date: "; signature.Date = DateTime.Now; signature.ContactInfoLabel = "Contact: "; signature.ContactInfo = "123456789"; signature.Certificated = false; signature.ConfigGraphicType = ConfiguerGraphicType.TextSignInformation;
步驟5:設置數字簽名的文檔權限
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;
步驟6:將文檔以PDF格式保存到文件夾中
//保存文檔 doc.SaveToFile("sample.pdf"); //打開文檔 System.Diagnostics.Process.Start("sample.pdf");
效果圖:
感謝您的瀏覽,希望本文能帶給您一定的幫助。