• <menu id="w2i4a"></menu>
  • logo Aspose.PDF使用教程

    文檔首頁(yè)>>Aspose.PDF使用教程>>.NET版PDF處理控件Aspose.PDF功能演示:在C#中對(duì)PDF文檔進(jìn)行數(shù)字簽名并驗(yàn)證數(shù)字簽名

    .NET版PDF處理控件Aspose.PDF功能演示:在C#中對(duì)PDF文檔進(jìn)行數(shù)字簽名并驗(yàn)證數(shù)字簽名


    數(shù)字簽名用于保護(hù)PDF文檔,然后再與第三方共享。對(duì)PDF文檔進(jìn)行數(shù)字簽名可以通過(guò)使用數(shù)字簽名驗(yàn)證文檔來(lái)檢測(cè)篡改。

    Aspose.PDF for .NET是一種PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù),可以輕松用于生成,修改,轉(zhuǎn)換,渲染,保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。

    為了使用數(shù)字簽名以編程方式保護(hù)PDF文檔,本文將展示如何在C#中對(duì)PDF文檔進(jìn)行數(shù)字簽名和驗(yàn)證。最終,將學(xué)習(xí)如何:

    • 使用C#對(duì)PDF文檔進(jìn)行數(shù)字簽名。
    • 使用C#中的時(shí)間戳服務(wù)器對(duì)PDF文檔進(jìn)行數(shù)字簽名。
    • 使用C#驗(yàn)證PDF中的數(shù)字簽名。

    近日,.NET版Aspose.PDF升級(jí)到v20.2版,解決VerticalAlignment對(duì)TextBoxField不起作用的問(wèn)題,修復(fù)PDF到HTML之間轉(zhuǎn)換等多項(xiàng)Bug,感興趣的朋友可點(diǎn)擊下方按鈕下載最新版。

    點(diǎn)擊下載最新版Aspose.PDF for .NET

    用C#對(duì)PDF文檔進(jìn)行數(shù)字簽名

    以下是使用Aspose.PDF for .NET簽署PDF文檔的步驟。

    • 創(chuàng)建Document類的對(duì)象,并使用PDF文檔的路徑對(duì)其進(jìn)行初始化。
    • 創(chuàng)建一個(gè)PdfFileSignature類的對(duì)象,并使用Document類的對(duì)象對(duì)其進(jìn)行初始化。
    • 創(chuàng)建PKCS7類的對(duì)象,并使用證書(shū)路徑和密碼對(duì)其進(jìn)行初始化。
    • 為MDP簽名類型創(chuàng)建并初始化DocMDPSignature類的對(duì)象。
    • 創(chuàng)建一個(gè)矩形用于簽名放置。
    • 使用PdfFileSignature.Certify()方法對(duì)PDF文檔進(jìn)行數(shù)字簽名。
    • 使用PdfFileSignature.Save()方法保存文檔。

    以下代碼示例顯示了如何在C#中向PDF文檔添加數(shù)字簽名。

    // Load PDF document
    using (Document pdfDocument = new Document("Document.pdf"))
    {
    	using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
    	{
    		PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // 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 = @"aspose-logo.png";
    		// Create any of the three signature types
    		signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
    		// Save digitally signed PDF file
    		signature.Save("Digitally Signed PDF.pdf");
    	}
    }

    使用C#中的時(shí)間戳服務(wù)器對(duì)PDF文檔進(jìn)行數(shù)字簽名

    使用TimestampSettings類提供詳細(xì)信息,從而使用TimeStamp服務(wù)器對(duì)PDF文檔進(jìn)行簽名。以下代碼示例演示如何使用C#中的TimeStamp服務(wù)器對(duì)PDF文檔進(jìn)行數(shù)字簽名。

    // Load PDF document
    using (Document pdfDocument = new Document("Document.pdf"))
    {
    	using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
    	{
    		PKCS7 pkcs = new PKCS7("certificate.pfx", "WebSales"); // Use PKCS7/PKCS7Detached objects
    		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("Output.pdf");
    	}
    }

    使用C#驗(yàn)證PDF中的數(shù)字簽名

    使用以下步驟驗(yàn)證經(jīng)過(guò)數(shù)字簽名的PDF文檔:

    • 創(chuàng)建Document類的對(duì)象,并使用PDF文檔的路徑對(duì)其進(jìn)行初始化。
    • 創(chuàng)建一個(gè)PdfFileSignature類的對(duì)象,并使用Document類的對(duì)象對(duì)其進(jìn)行初始化。
    • 訪問(wèn)PDF文檔的所有簽名。
    • 使用PdfFileSignature.VerifySigned()方法驗(yàn)證簽名的有效性。

    下面的代碼示例演示如何使用C#驗(yàn)證PDF中的數(shù)字簽名。

    // Load PDF document
    using (Document pdfDocument = new Document("Document.pdf"))
    {
    	using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
    	{
    		IListsigNames = 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
    					}
    				}
    			}
    		}
    	}
    }
    還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();