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 文件。 在這個例子中,我們將根據 HTML 內容建立一個新的 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 儲存到指定的文件位置。