翻譯|使用教程|編輯:李顯亮|2020-04-08 10:00:17.110|閱讀 915 次
概述:在本系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何對(duì)PDF文件進(jìn)行數(shù)字簽名。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何對(duì)PDF文件進(jìn)行數(shù)字簽名。
>>Aspose.PDF for .NET更新至最新版v20.4,歡迎下載體驗(yàn)。
用于.NET的Aspose.PDF支持使用SignatureField該類對(duì)PDF文件進(jìn)行數(shù)字簽名的功能。當(dāng)然也可以使用PKCS12證書來認(rèn)證PDF文件。
使用簽名對(duì)PDF文檔簽名時(shí),基本上可以“按原樣”確認(rèn)其內(nèi)容。因此,之后進(jìn)行的任何其他更改都會(huì)使簽名無效,所以,將知道文檔是否被更改。鑒于首先對(duì)文檔進(jìn)行認(rèn)證,我們可以指定用戶可以在不使認(rèn)證無效的情況下對(duì)文檔進(jìn)行的更改。換句話說,仍將文檔視為保留其完整性,并且收件人仍可以信任該文檔。
為了滿足上述要求,已對(duì)以下公共API進(jìn)行了更改。
// 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支持使用時(shí)間戳服務(wù)器或Web服務(wù)對(duì)PDF進(jìn)行數(shù)字簽名。為了實(shí)現(xiàn)此要求,TimestampSettings已將該類添加到Aspose.PDF命名空間中。
請(qǐng)查看下面的代碼片段,該代碼片段獲得了時(shí)間戳并將其添加到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");
}
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn