using IronPdf;
using IronPdf.Signing;
// Cryptographically sign an existing PDF in 1 line of code!
new IronPdf.Signing.PdfSignature("Iron.p12", "123456").SignPdfFile("any.pdf");
/***** Advanced example for more control *****/
// Step 1. Create a PDF
var renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
// You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
// Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
var signature = new IronPdf.Signing.PdfSignature("Iron.pfx", "123456")
{
// Step 3. Optional signing options and a handwritten signature graphic
SigningContact = "support@ironsoftware.com",
SigningLocation = "Chicago, USA",
SigningReason = "To show how to sign a PDF"
};
//Step 3. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.Sign(signature);
//Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
' Cryptographically sign an existing PDF in 1 line of code!
Call (New IronPdf.Signing.PdfSignature("Iron.p12", "123456")).SignPdfFile("any.pdf")
'''*** Advanced example for more control ****
' Step 1. Create a PDF
Dim renderer = New ChromePdfRenderer()
Dim doc = renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>")
' Step 2. Create a Signature.
' You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
' Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
Dim signature = New IronPdf.Signing.PdfSignature("Iron.pfx", "123456") With {
.SigningContact = "support@ironsoftware.com",
.SigningLocation = "Chicago, USA",
.SigningReason = "To show how to sign a PDF"
}
'Step 3. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.Sign(signature)
'Step 4. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf")
對 PDF 文件進行數位簽章,可透過在 PDF 文件本身添加驗證機制,協助確保文件的完整性。 使用 IronPDF 時,您有數種方式可對新建立或現有的 PDF 檔案進行簽署。這些方式包括:使用憑證對 PDF 文件進行數位簽署、在 PDF 中加入手寫簽名的圖形版本、在 PDF 上加蓋憑證圖像,或是直接在 PDF 上建立簽名表單欄位以引導使用者簽署。
使用 IronPDF 對 PDF 進行數位簽名的步驟
此流程的第一步是載入或建立我們想要簽署的 PDF 檔案。 在此範例中,我們建立一個新的 ChromePdfRenderer 實例。 這是 IronPDF 強大的渲染引擎,用於將 HTML、CSS 和 JavaScript 渲染為 PDF,且不失真。 接著,我們使用 RenderHtmlAsPdf 方法,將 HTML 字串渲染成高品質的 PDF 文件,以便進行簽署。 生成的 PDF 檔案儲存於 doc 變數中。
接下來,我們需要建立我們的簽名。 在此範例中,我們將使用憑證對 PDF 文件進行簽名。 PdfSignature 代表用於簽署 PDF 的數位簽章物件,它需要我們欲用於簽章的 .pfx 檔案路徑,以及存取該檔案的密碼。 我們已包含三個可選屬性:SigningContact 用於在簽名元資料中添加電子郵件或聯絡資訊,SigningLocation 代表文件簽署的位置,而 SigningReason 則提供文件簽署的原因。
接著,我們使用先前建立的 PdfSignature 物件對 PDF 文件進行簽名。 透過呼叫 Sign 方法,我們只需一行簡單的程式碼即可將簽名套用至 PDF 文件。使用此方法,可將多個簽名憑證套用至 PDF 文件。
最後,我們使用 SaveAs 方法儲存已簽名的 PDF 文件,該方法會將 PDF 儲存至指定的檔案位置。