數字簽名普遍用於保護PDF文檔,可見數字簽名在平常生活中是至關重要的。在這篇文章中我將與你們分享如何給PDF文件添加可見的數字簽名。git
首先我下載了一個由E-iceblue公司開發的免費版的PDF組件-Free Spire.PDF,控件安裝好後,再經過下面所說起的路徑把Bin文件夾裏的Spire.PDF.dll添加爲引用。ui
路徑:"...\Spire.pdf-fe\Bin\NET4.0\ Spire.PDF.dll"spa
接下來我將提供一些代碼片斷來向你們展現如何添加可見的數字簽名:code
步驟1:新建一個PDF文檔並加載一個PDF證書orm
//新建一個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:設置顯示文本屬性blog
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格式保存到文件夾中it
//保存文檔 doc.SaveToFile("sample.pdf"); //打開文檔 System.Diagnostics.Process.Start("sample.pdf");
效果圖:
感謝您的瀏覽,但願本文能帶給您必定的幫助。