翻譯|使用教程|編輯:李顯亮|2020-04-08 10:00:17.110|閱讀 902 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何對PDF文件進行數字簽名。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何對PDF文件進行數字簽名。
>>Aspose.PDF for .NET更新至最新版v20.4,歡迎下載體驗。
用于.NET的Aspose.PDF支持使用SignatureField該類對PDF文件進行數字簽名的功能。當然也可以使用PKCS12證書來認證PDF文件。
使用簽名對PDF文檔簽名時,基本上可以“按原樣”確認其內容。因此,之后進行的任何其他更改都會使簽名無效,所以,將知道文檔是否被更改。鑒于首先對文檔進行認證,我們可以指定用戶可以在不使認證無效的情況下對文檔進行的更改。換句話說,仍將文檔視為保留其完整性,并且收件人仍可以信任該文檔。
為了滿足上述要求,已對以下公共API進行了更改。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); string pbxFile = ""; string inFile = dataDir + @"DigitallySign.pdf"; string outFile = dataDir + @"DigitallySign_out.pdf"; using (Document document = new Document(inFile)) { using (PdfFileSignature signature = new PdfFileSignature(document)) { PKCS7 pkcs = new PKCS7(pbxFile, "WebSales"); // Use PKCS7/PKCS7Detached objects DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100); // Set signature appearance signature.SignatureAppearance = dataDir + @"aspose-logo.jpg"; // Create any of the three signature types signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature); // Save output PDF file signature.Save(outFile); } } using (Document document = new Document(outFile)) { using (PdfFileSignature signature = new PdfFileSignature(document)) { IList<string> sigNames = signature.GetSignNames(); if (sigNames.Count > 0) // Any signatures? { if (signature.VerifySigned(sigNames[0] as string)) // Verify first one { if (signature.IsCertified) // Certified? { if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission { // Do something } } } } } }
用于.NET的Aspose.PDF支持使用時間戳服務器或Web服務對PDF進行數字簽名。為了實現此要求,TimestampSettings已將該類添加到Aspose.PDF命名空間中。
請查看下面的代碼片段,該代碼片段獲得了時間戳并將其添加到PDF文檔中。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); string pfxFile = ""; using (Document document = new Document(dataDir + @"DigitallySign.pdf")) { using (PdfFileSignature signature = new PdfFileSignature(document)) { PKCS7 pkcs = new PKCS7(pfxFile, "pfx_password"); TimestampSettings timestampSettings = new TimestampSettings("https:\\your_timestamp_settings", "user:password"); // User/Password can be omitted pkcs.TimestampSettings = timestampSettings; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100); // Create any of the three signature types signature.Sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs); // Save output PDF file signature.Save(dataDir + "DigitallySignWithTimeStamp_out.pdf"); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn