A Developer's Guide to Digitally Signing PDFs with C
這份全面指南向 C# 開發人員展示如何使用 IronPDF 對 PDF 進行數位簽名,內容涵蓋憑證式簽名、視覺化印章以及互動式表單欄位,以確保文件的真實性與安全性。
在 PDF 文件中添加簽名是許多應用程式的常見需求,但"簽名"一詞可能具有不同的含義。 對某些人而言,這涉及使用安全憑證來套用防篡改的數位簽章。 對其他人而言,這可能是將視覺化的手寫簽名圖像蓋印至文件上,或是新增互動式表單欄位,供使用者進行電子簽名。
本指南為 C# 開發人員提供詳盡的實作指引,說明如何使用 IronPDF for .NET 函式庫來完成所有這些任務。 我們將涵蓋從套用安全的 X509Certificate2 數位簽章,到蓋上圖形簽名及建立互動式簽名欄位等所有功能,確保您的 PDF 文件具備真實性、安全性與 Professional 性。
快速入門:使用 IronPDF 輕鬆為 PDF 文件進行數位簽名
立即開始使用 IronPDF,透過簡單直觀的流程為您的 PDF 文件進行數位簽名。 此範例展示如何使用 .pfx 憑證對 PDF 檔案進行驗證與簽署,以確保文件的完整性與真實性。 請按照以下步驟,將數位簽章無縫整合至您的應用程式中。
簡化工作流程(5 個步驟)
- 安裝適用於 .NET 的 IronPDF 函式庫。
- 使用數位簽名
X509Certificate2物件。 - 請添加一張圖像來代表數位簽章。
- 在 PDF 檔案上加蓋圖形或手寫簽名。
- 新增一個互動式簽名表單欄位,用於電子簽名。
如何使用憑證在 PDF 檔案上加簽數位簽章?
您可以使用數位憑證檔案(例如 .pfx 或 .p12)對 PDF 文件進行數位簽章,以確保文件的真實性與完整性。 此流程可確保文件自簽署以來未經任何變更。 如需了解數位簽章功能的完整概述,請參閱我們的全面數位簽章指南。
IronPDF 為此提供了一個直觀的 API,支援多種數位簽章的應用方式。 此功能的核心圍繞 PdfSignature 類別,該類別封裝了憑證以及簽名相關的所有元資料。
| 簽署方式 | 描述 |
|---|---|
簽署 |
使用您所建立並設定的 `PdfSignature` 物件對 PDF/A 進行簽名。 |
SignWithFile |
使用數位簽章憑證檔案簽署 PDF 檔案 (.pfx 或 .p12)進行簽署。 |
SignWithStore |
使用電腦憑證儲存庫中的數位簽章對 PDF 進行簽署,該簽章透過其拇指印 ID 進行識別。 |
使用 X509Certificate2 物件
若需最大程度的控制權,您可以從您的憑證檔案建立 X509Certificate2 物件。IronPDF 完全符合 X509Certificate2 標準,提供了一種強大且安全的數位簽章實作方法。 建立憑證物件時,請確保 X509KeyStorageFlags 已設定為 Exportable,因為這是底層加密 API 的必要條件。請參閱我們程式碼儲存庫中的數位簽章實例。
Install-Package IronPdf
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");
// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);
// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);
// Apply the signature to the PDF document.
pdf.Sign(signature);
// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");
// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);
// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);
// Apply the signature to the PDF document.
pdf.Sign(signature);
// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates
' Create a new PDF from an HTML string for demonstration.
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>")
' Load the certificate from a .pfx file with its password.
' The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
Private cert = New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)
' Create a PdfSignature object using the loaded certificate.
Private signature = New PdfSignature(cert)
' Apply the signature to the PDF document.
pdf.Sign(signature)
' Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf")
上述程式碼首先會產生一個簡單的 PDF 檔案。 接著,它會將 .pfx 憑證檔案載入至 X509Certificate2 物件中。 此物件代表數位身分,並被傳遞至 PdfSignature 建構函式。 最後,pdf.Sign 方法會在儲存文件前將此簽名套用至文件。 For more information on the X509Certificate2 class, you can refer to the official Microsoft documentation.
為數位簽章增添細部資訊
數位簽章所包含的內容不僅限於憑證; 您可以嵌入豐富的元資料,以提供關於該簽名的背景資訊。 這包括簽署位置、原因、聯絡資訊,以及來自可信機構的安全時間戳記。 您亦可設定及編輯元資料,以調整其他文件屬性。
加入這些細節有助於完善文件的稽核追蹤,並為審核人員提供有價值的資訊。 IronPDF 亦支援採用現代 SHA256 及 SHA512 雜湊演算法的時間戳記伺服器。
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;
// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");
// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");
// Add detailed metadata to the signature for a comprehensive audit trail.
// These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";
// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));
// Sign the PDF document with the configured signature object.
pdf.Sign(signature);
// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;
// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");
// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");
// Add detailed metadata to the signature for a comprehensive audit trail.
// These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";
// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));
// Sign the PDF document with the configured signature object.
pdf.Sign(signature);
// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports System
' Load an existing PDF document to be signed.
Dim pdf = PdfDocument.FromFile("invoice.pdf")
' Create a PdfSignature object directly from the certificate file and password.
Dim signature = New PdfSignature("IronSoftware.pfx", "123456")
' Add detailed metadata to the signature for a comprehensive audit trail.
' These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now
signature.SigningContact = "legal@ironsoftware.com"
signature.SigningLocation = "Chicago, USA"
signature.SigningReason = "Contractual Agreement"
' Add a secure timestamp from a trusted Time Stamp Authority (TSA).
' This provides cryptographic proof of the signing time.
signature.TimeStampUrl = New Uri("http://timestamp.digicert.com")
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256
' Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, New Rectangle(350, 750, 200, 100))
' Sign the PDF document with the configured signature object.
pdf.Sign(signature)
' Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf")
若簽名憑證未存於系統的受信任儲存庫中,您可能會在某些 PDF 檢視器中看到警告圖示。 若要獲得綠色勾選標記,必須將該憑證新增至檢視者的受信任憑證清單中。
如何為數位簽名添加視覺化呈現?
雖然數位簽章已透過加密方式嵌入 PDF 檔案中,但在頁面中提供視覺化的呈現通常會很有幫助。 此處可以是公司標誌、手寫簽名圖像或其他圖形。 IronPDF 讓您能輕鬆地將圖片新增至 PdfSignature 物件中。
您可以從檔案或串流載入圖片,並將其精確定位於 PDF 的任何頁面。 支援的圖像格式包括 PNG、JPEG、GIF、BMP、TIFF 及 WebP。此技術類似於在 PDF 文件上加蓋文字與圖像的操作方式。
using IronPdf.Signing;
using IronSoftware.Drawing;
// This example demonstrates various ways to add a visual image to a PDF signature.
// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");
// Define the position and size for the signature image on the first page (index 0).
// Rectangle parameters: x position, y position, width, height
var signatureRectangle = new Rectangle(350, 750, 200, 100);
// Option 1: Set the SignatureImage property directly.
// This is the most straightforward approach
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);
// Option 2: Use the LoadSignatureImageFromFile method.
// This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);
// Option 3: Load an image from a stream. This is useful for images generated in memory.
// Perfect for scenarios where images are retrieved from databases or web services
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}
// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
using IronPdf.Signing;
using IronSoftware.Drawing;
// This example demonstrates various ways to add a visual image to a PDF signature.
// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");
// Define the position and size for the signature image on the first page (index 0).
// Rectangle parameters: x position, y position, width, height
var signatureRectangle = new Rectangle(350, 750, 200, 100);
// Option 1: Set the SignatureImage property directly.
// This is the most straightforward approach
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);
// Option 2: Use the LoadSignatureImageFromFile method.
// This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);
// Option 3: Load an image from a stream. This is useful for images generated in memory.
// Perfect for scenarios where images are retrieved from databases or web services
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}
// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
Imports IronPdf.Signing
Imports IronSoftware.Drawing
' This example demonstrates various ways to add a visual image to a PDF signature.
' Create a PdfSignature object.
Dim signature As New PdfSignature("IronSoftware.pfx", "123456")
' Define the position and size for the signature image on the first page (index 0).
' Rectangle parameters: x position, y position, width, height
Dim signatureRectangle As New Rectangle(350, 750, 200, 100)
' Option 1: Set the SignatureImage property directly.
' This is the most straightforward approach
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle)
' Option 2: Use the LoadSignatureImageFromFile method.
' This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle)
' Option 3: Load an image from a stream. This is useful for images generated in memory.
' Perfect for scenarios where images are retrieved from databases or web services
Dim image As AnyBitmap = AnyBitmap.FromFile("assets/visual-signature.png")
Using imageStream = image.ToStream()
signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle)
End Using
' After configuring the signature image, apply it to a PDF.
Dim pdf As PdfDocument = PdfDocument.FromFile("invoice.pdf")
pdf.Sign(signature)
pdf.SaveAs("VisualSignature.pdf")
此程式碼展示了三種在數位簽名中加入視覺元件的等效方法。 無論您是將圖像儲存在磁碟中,還是保存在記憶體內,都可以在簽署過程中輕鬆將其烙印到 PDF 文件上。 這彌合了隱形加密安全與可見文件核准之間的鴻溝。
簽署後如何控制文件權限?
當您簽署文件時,您可能需要指定後續允許進行哪些變更(如有)。 例如,您可能希望完全鎖定文件,或僅允許使用者填寫表單欄位。 IronPDF 允許您使用 SignaturePermissions 枚舉來設定這些權限。 如需更進階的權限控制,請參閱我們關於設定 PDF 密碼與權限的指南。
設定簽名權限是管理文件生命週期的關鍵環節。 這可確保在您簽署後,文件能依照您的規則維持完整性。 若使用者執行未經允許的操作,簽名將被視為無效。
| `SignaturePermissions` 會員 | 定義 |
|---|---|
| `禁止修改` | 嚴禁進行任何形式的修改。本文件實質上已鎖定。 |
| `FormFillingAllowed` | 僅允許填寫現有表單欄位並簽名。 |
| `允許註解與表單填寫` | 支援表單填寫、簽署,以及建立或修改註解。 |
儲存並簽署特定的 PDF 修訂版本
PDF 檔案能儲存變更歷史紀錄,這與版本控制系統非常相似。 這稱為增量節省。 當您對 PDF 簽名時,該簽名僅適用於該文件的特定修訂版本。 這對於文件需經過多階段審核的工作流程至關重要。 請參閱我們的詳細指南,進一步了解如何管理 PDF 修訂歷史紀錄。
在以下範例中,我們載入 PDF 檔案、進行編輯,然後簽署當前版本,同時僅允許日後透過表單填寫進行變更。 我們使用 SaveAsRevision 在儲存檔案前,將當前狀態提交至文件歷史紀錄。
GetRevision
理解增量儲存是掌握進階 PDF 工作流程的關鍵。 雖然簡單的檢視器可能僅顯示最新版本,但像 Adobe Acrobat 這樣的工具則能呈現完整的修訂歷史紀錄,顯示誰簽署了哪個版本,以及各次簽署之間所做的變更。 IronPDF 讓您能完全透過程式化方式控制此流程。
對於需要高度安全性與合規性的複雜文件工作流程管理企業而言,可能需要一套全面的解決方案。 Iron Software 提供 Iron Suite 套件,其中包含用於簽署與文件處理的 IronPDF,以及其他適用於廣泛文件處理任務的函式庫,僅需一次性付款即可取得。
如何管理並驗證不同修訂版本之間的簽名?
一份 PDF 文件可在其各個修訂版本中套用多個簽名。 IronPDF 提供工具,可有效管理此歷史記錄。
- 還原至先前版本:您可以使用
RollBackToRevision方法,將文件還原至較早的狀態。 這將捨棄該修訂版本之後所做的所有變更與簽名。 - 驗證所有簽名:
VerifyAllSignatures方法會檢查文件所有修訂版本中 所有 簽名的有效性。 僅當所有簽名均有效且未經授權修改時,才會返回SignatureStatus.Valid。 - 移除簽名:
RemoveSignatures方法將從文件的每個版本中移除所有數位簽名,產生一個乾淨、未簽名的版本。
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");
// Verify all signatures across all revisions.
// This ensures document integrity throughout its entire history
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");
// Roll back to the first revision (index 0).
// Useful for reviewing the original document state
if (pdf.RevisionCount > 1)
{
PdfDocument firstRevision = pdf.GetRevision(0);
firstRevision.SaveAs("report_first_revision.pdf");
}
// Create a completely unsigned version of the document.
// This removes all digital signatures while preserving content
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");
// Verify all signatures across all revisions.
// This ensures document integrity throughout its entire history
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");
// Roll back to the first revision (index 0).
// Useful for reviewing the original document state
if (pdf.RevisionCount > 1)
{
PdfDocument firstRevision = pdf.GetRevision(0);
firstRevision.SaveAs("report_first_revision.pdf");
}
// Create a completely unsigned version of the document.
// This removes all digital signatures while preserving content
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
Imports System
' Load a PDF with a complex signature history.
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")
' Verify all signatures across all revisions.
' This ensures document integrity throughout its entire history
Dim allSignaturesValid As Boolean = pdf.VerifySignatures()
Console.WriteLine($"All signatures are valid: {allSignaturesValid}")
' Roll back to the first revision (index 0).
' Useful for reviewing the original document state
If pdf.RevisionCount > 1 Then
Dim firstRevision As PdfDocument = pdf.GetRevision(0)
firstRevision.SaveAs("report_first_revision.pdf")
End If
' Create a completely unsigned version of the document.
' This removes all digital signatures while preserving content
pdf.RemoveSignatures()
pdf.SaveAs("report_unsigned.pdf")
如何在 PDF 上加蓋手寫簽名?
有時,您不需要數位簽名的加密安全性,而只是想套用視覺化的電子簽名,例如掃描的手寫簽名。 這通常被稱為"蓋章"。 IronPDF 可透過其 Watermark 或 Stamp 功能實現此功能。 如需更進階的水印設定選項,請參閱我們的自訂水印指南。
讓我們從一份樣本發票 PDF 以及一張 .png 手寫簽名圖片開始。
VerifySignatures
簽名蓋章前的原始發票 PDF 檔。
以下是我們將套用的簽名圖像:
手寫簽名樣本圖片.
以下程式碼使用 SignatureImage 屬性,將此圖像烙印在 PDF 的右下角。
using IronPdf.Editing;
// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");
// Create an HtmlStamp containing our signature image.
// HtmlStamp allows us to position HTML content precisely on the page
var signatureStamp = new HtmlStamp("<img src='assets/signature.png'/>")
{
// Configure the stamp's position and appearance.
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = 10, // Add some space from the edge.
Opacity = 90 // Make it slightly transparent for a more authentic look.
};
// Apply the stamp to all pages of the PDF.
// You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp);
// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
using IronPdf.Editing;
// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");
// Create an HtmlStamp containing our signature image.
// HtmlStamp allows us to position HTML content precisely on the page
var signatureStamp = new HtmlStamp("<img src='assets/signature.png'/>")
{
// Configure the stamp's position and appearance.
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = 10, // Add some space from the edge.
Opacity = 90 // Make it slightly transparent for a more authentic look.
};
// Apply the stamp to all pages of the PDF.
// You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp);
// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
Imports IronPdf.Editing
' Load the existing PDF document.
Dim pdf = PdfDocument.FromFile("invoice.pdf")
' Create an HtmlStamp containing our signature image.
' HtmlStamp allows us to position HTML content precisely on the page
Dim signatureStamp = New HtmlStamp("<img src='assets/signature.png'/>") With {
' Configure the stamp's position and appearance.
.VerticalAlignment = VerticalAlignment.Bottom,
.HorizontalAlignment = HorizontalAlignment.Right,
.Margin = 10, ' Add some space from the edge.
.Opacity = 90 ' Make it slightly transparent for a more authentic look.
}
' Apply the stamp to all pages of the PDF.
' You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp)
' Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf")
加蓋水印後的 PDF 檔案會是什麼樣子?
執行程式碼後,簽名圖像將被烙印至文件上,生成一份視覺上已簽署的發票。
最終 PDF 檔的右下角將加蓋手寫簽名圖像印章。
如何在 PDF 中新增互動式簽名欄位?
對於需要在 Adobe Acrobat 等 PDF 檢視器中由最終使用者簽署的文件,您可以新增互動式簽名表單欄位。 這會建立一個可點擊的空白區域,提示使用者套用自己的數位簽章。 如需 PDF 表單的完整指南,請參閱我們的"建立 PDF 表單"教學。
您可以建立一個 SignatureFormField 並將其新增至 PDF 的表單集合中。 您可以精確控制其在頁面上的位置與大小。 這對於需要多方簽署的文件,或當您需要向外部方收集簽名時,特別有用。
true``RemoveSignatures
using IronPdf.Forms;
using IronSoftware.Drawing;
// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>");
// Define the properties for the signature form field.
string fieldName = "ClientSignature"; // Unique identifier for the field
int pageIndex = 0; // Add to the first page (zero-indexed)
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)
// Create the SignatureFormField object.
// This creates an interactive field that users can click to sign
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);
// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);
// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
using IronPdf.Forms;
using IronSoftware.Drawing;
// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>");
// Define the properties for the signature form field.
string fieldName = "ClientSignature"; // Unique identifier for the field
int pageIndex = 0; // Add to the first page (zero-indexed)
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)
// Create the SignatureFormField object.
// This creates an interactive field that users can click to sign
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);
// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);
// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
Imports IronPdf.Forms
Imports IronSoftware.Drawing
' Create a new PDF to add the signature field to.
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>")
' Define the properties for the signature form field.
Dim fieldName As String = "ClientSignature" ' Unique identifier for the field
Dim pageIndex As Integer = 0 ' Add to the first page (zero-indexed)
Dim fieldRect As New Rectangle(50, 200, 300, 100) ' Position: (x, y), Size: (width, height)
' Create the SignatureFormField object.
' This creates an interactive field that users can click to sign
Dim signatureField As New SignatureFormField(fieldName, pageIndex, fieldRect)
' Add the signature field to the PDF's form.
pdf.Form.Add(signatureField)
' Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf")
當使用者開啟此 PDF 時,將看到一個可點擊的欄位,讓他們能夠使用自己的數位身分證件完成簽署流程。 您可參閱我們的《PDF 表單製作教學指南》,進一步了解如何建立與管理互動式表單。
透過程式碼新增至 PDF 文件中的未簽署、互動式簽名欄位。
如何從已驗證的簽名中擷取簽署者名稱?
若要取得簽署簽名的憑證持有人的常用名稱,我們可以使用 VerifiedSignature 類別來存取 SignerName 屬性。 以下是一段展示如何實現此目標的程式碼片段。
:path=/static-assets/pdf/content-code-examples/how-to/signing-find-signer-name.cs
using IronPdf;
using System;
// Import the Signed PDF report
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");
// Using GetVerifiedSignatures() obtain a list of `VerifiedSignature` objects from the PDF
pdf.GetVerifiedSignatures().ForEach(signature =>
{
// Print out the SignerName of each `VerifiedSignature` object
Console.WriteLine($"SignatureName: {signature.SignerName}");
});
Imports IronPdf
Imports System
' Import the Signed PDF report
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")
' Using GetVerifiedSignatures() obtain a list of `VerifiedSignature` objects from the PDF
pdf.GetVerifiedSignatures().ForEach(Sub(signature)
' Print out the SignerName of each `VerifiedSignature` object
Console.WriteLine($"SignatureName: {signature.SignerName}")
End Sub)
匯入已簽署的 PDF 檔案後,我們會使用 GetSignatures(...) 方法來擷取報告中的 VerifiedSignature 物件清單,並針對每個簽名 PRINT SignerName。
GetVerifiedSignatures``SignerName
請注意,此值是從憑證的"主體識別名稱"(SubjectDN)中提取的,若未存在"CN"欄位,則會返回 null。
IronPDF 的 PDF 簽署功能接下來有哪些新進展?
本指南已展示了 IronPDF 強大且靈活的 PDF 簽名功能。 無論您需要套用附有詳細元資料的安全數位簽章、管理文件修訂版本、加蓋視覺化簽章,還是建立互動式表單,IronPDF 皆提供一套全面且對開發者友善的 API,助您輕鬆完成任務。
若要進一步探索,您可以下載 IronPDF for .NET 函式庫,並取得免費試用授權,在您的專案中測試其所有功能。 若需了解更進階的文件處理技巧,包括新增註解及處理表單欄位,請參閱我們的詳盡文件與教學指南。
準備好探索更多可能性了嗎? 請點此查看我們的教學頁面:簽署與保護 PDF 文件
常見問題
如何在 C# 中使用憑證對 PDF 進行數位簽章?
透過 IronPDF,您只需一行程式碼,即可使用 PdfSignature 類別對 PDF 進行數位簽名。只需使用您的憑證檔案(.pfx 或 .p12)和密碼建立一個新的 PdfSignature 物件,然後呼叫 SignPdfFile() 方法即可。 例如:new IronPdf.Signing.PdfSignature("certificate.pfx", "password").SignPdfFile("input.pdf")。此操作將使用您的 X509Certificate2 套用防篡改數位簽名,以確保文件真實性。
支援哪些類型的 PDF 簽名?
IronPDF 支援三種主要的 PDF 簽名類型:1) 使用 X509Certificate2 憑證進行驗證與防篡改的數位簽名;2) 將圖形或手寫簽名圖像添加至文件的視覺簽名章;以及 3) 允許使用者對 PDF 進行電子簽名的互動式簽名表單欄位。每種類型皆針對文件安全性與工作流程需求提供不同的解決方案。
數位簽章可使用哪些憑證格式?
IronPDF 支援常見的數位憑證格式,包括 .pfx(個人資訊交換)和 .p12 檔案。這些憑證檔案同時包含數位簽名所需的公開金鑰和私密金鑰。IronPDF 中的 PdfSignature 類別可與任何 X509Certificate2 物件配合使用,讓您在載入和管理簽名憑證時擁有高度的靈活性。
我可以為我的數位簽名添加視覺化圖示嗎?
是的,IronPDF 允許您在數位簽名中加入視覺元素。您可以在加密簽名旁加入圖形元素,例如手寫簽名圖像、公司標誌或自訂印章。此功能結合了數位憑證的安全性與視覺確認,使簽署後的文件既安全又具專業外觀。
如何建立互動式簽名欄位,供使用者進行電子簽名?
IronPDF 讓您能夠在 PDF 文件中加入互動式簽名表單欄位。透過這些欄位,使用者可透過點擊並繪製簽名,或上傳簽名圖片,以電子方式簽署文件。此功能非常適合用於製作需要收集簽名的文件,例如需由多方簽署的合約或表單。
簽署 PDF 能否確保文件完整性?
是的,當您使用 IronPDF 搭配 X509Certificate2 對 PDF 進行數位簽名時,系統會建立一個防篡改印章,以確保文件完整性。數位簽名可保證文件自簽署以來未曾遭竄改。若在簽署後對 PDF 進行任何修改,將導致簽名失效,並提醒收件者該文件可能已遭篡改。

