如何使用 IronPDF 在 C# 中對 PDF 文件進行簽名 | IronPDF 教學課程

A Developer's Guide to Digitally Signing PDFs with C#

This article was translated from English: Does it need improvement?
Translated
View the article in English

在許多應用程式中,為 PDF 文件添加簽名是一個常見需求,但「簽名」可能代表不同的意義。 對某些人來說,這是關於使用安全證書應用防篡改的數位簽名。 對其他人來說,這可能是將手寫簽名圖像印於文件上或添加互動式表單欄位以供用戶電子簽名。

這份指南為 C# 開發人員提供了使用IronPDF for .NET 庫完成這些任務的全面說明。 我們將涵蓋從應用安全 X509Certificate2 數位簽名,到圖形簽名蓋印以及創建互動式簽名欄位的所有內容,以確保您的 PDF 文件是真實、安全和專業的。

快速入門:使用 IronPDF 輕鬆進行 PDF 文件的數位簽名

使用 IronPDF 來數位簽名您的 PDF 文件,以簡單明瞭的流程迅速開始。 這個範例展示了如何使用 .pfx 證書來驗證並簽名 PDF 文件,以確保文件的完整性和真實性。 遵循這些步驟將數位簽名無縫整合到您的應用中,賦予用戶安全可靠的 PDF 功能。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.Signing.PdfSignature("certificate.pfx", "password").SignPdfFile("input.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步驟)

展示 C# 開發者如何數位簽名 PDF 文件的插圖。
  1. 安裝 IronPDF for .NET 庫。
  2. 使用 X509Certificate2 物件應用數位簽名。
  3. 添加可視圖像以代表數位簽名。
  4. 將圖形或手寫簽名印於 PDF 文件上。
  5. 添加互動簽名表單欄位以供電子簽名。

如何使用證書在 PDF 文件中應用數位簽名?

您可以使用數位證書文件(如 .pfx.p12)對 PDF 文件應用數位簽名,以保證文件的真實性和完整性。 這個過程確保了自簽名後文件沒有被更改。

IronPDF 提供了一個簡單直觀的 API,用於此目的,支持多種方式應用數位簽名。 此功能的核心圍繞著 PdfSignature 類,該類封裝了用於簽名的證書及所有相關元數據。

簽名方法 描述
Sign 使用您創建並配置的PdfSignature物件簽名 PDF。
SignWithFile 使用位於磁碟上的數位簽章證書文件(.pfx.p12)簽名 PDF。
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")
$vbLabelText   $csharpLabel

上面的代碼首先生成一個簡單的 PDF。 然後它將 .pfx 證書文件加載到 X509Certificate2 物件中。 這個代表數字身份的物件會被傳到 PdfSignature 構造函數。 最後,pdf.Sign 方法將此簽名應用於文件,然後保存。 For more information on the X509Certificate2 class, you can refer to the official Microsoft documentation.

向數位簽名添加詳細信息

數位簽名可以不僅僅包含證書; 您可以嵌入豐富的元數據以提供有關簽名的背景資訊。 這包括簽名地點、原因、聯系資訊和來自信任機構的安全時間戳。

添加這些詳細信息改善了文件的審計跟蹤並為驗證者提供了有價值的信息。 IronPDF 也支持使用現代 SHA256SHA512 雜湊算法的時間戳伺服器。

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.
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.
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.
Private pdf = PdfDocument.FromFile("invoice.pdf")

' Create a PdfSignature object directly from the certificate file and password.
Private signature = New PdfSignature("IronSoftware.pfx", "123456")

' Add detailed metadata to the signature for a comprehensive audit trail.
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")
$vbLabelText   $csharpLabel

如果簽名證書不在系統的受信任存儲中,您可能在某些 PDF 檢視器中會看到警告圖標。 要獲得綠色勾選,證書必須添加到檢視器的受信任身份中。

如何為數位簽名添加視覺表現?

雖然數位簽名被加密地嵌入在 PDF 中,但通常在頁面上擁有視覺表現是有用的。 這可以是公司標誌、手寫簽名圖像或其他圖形。 IronPDF 讓您很容易將圖像添加到 PdfSignature 物件。

您可以從文件或資料流中加載圖像,並精確地定位在 PDF 的任意頁面上。 支援的圖像格式包括 PNG、JPEG、GIF、BMP、TIFF 及 WebP。

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).
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
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");
</span><br class="ProseMirror-trailingBreak">
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).
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
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");
</span><br class="ProseMirror-trailingBreak">
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' This example demonstrates various ways to add a visual image to a PDF signature.

' Create a PdfSignature object.
Private signature = New PdfSignature("IronSoftware.pfx", "123456")

' Define the position and size for the signature image on the first page (index 0).
Private signatureRectangle = New Rectangle(350, 750, 200, 100)

' Option 1: Set the SignatureImage property directly.
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle)

' Option 2: Use the LoadSignatureImageFromFile method.
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle)

' Option 3: Load an image from a stream. This is useful for images generated in memory.
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 = PdfDocument.FromFile("invoice.pdf")
pdf.Sign(signature)
pdf.SaveAs("VisualSignature.pdf")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'</span><br class="ProseMirror-trailingBreak">
$vbLabelText   $csharpLabel

這段代碼展示了三種等效的方法來為數位簽名添加視覺組件。 無論您是在磁碟上還是在記憶體中有圖像,您都可以輕鬆地作為簽名過程的一部分將其印於 PDF。 這把不可見的加密安全性和可見的文件驗證結合起來。

如何在簽名後控制文件權限?

當您簽署文件時,您可能想要指定之後允許的更改(如果有的話)。 例如,您可能想完全鎖定文件或僅允許用戶填寫表單欄位。 IronPDF 允許您使用 SignaturePermissions 枚舉來設置這些權限。

設置簽名權限是管理文件生命周期的關鍵部分。 它確保在您的簽名應用後文件的完整性會根據您的規則保持。 如果用戶執行不允許的操作,簽名將失效。

SignaturePermissions成員 定義
NoChangesAllowed 不允許任何形式的更改。文件變為有效鎖定狀態。
FormFillingAllowed 僅允許填寫現有的表單欄位和簽名。
AnnotationsAndFormFillingAllowed 允許表單填寫、簽名以及創建或修改註釋。

保存和簽署特定的 PDF 修訂

PDF 能夠儲存更改歷史,與版本控制系統非常相似。 這被稱為增量保存。 當您簽署 PDF 時,簽名會應用於文件的特定修訂。 這對於文件經歷多個審批階段的工作流程來說至關重要。

在以下示例中,我們載入一個 PDF,進行編輯,然後簽署當前修訂,同時僅允許未來改變為表單填寫。 我們使用SaveAsRevision將當前狀態提交到文件的歷史紀錄中,然後保存文件。

using IronPdf.Signing;

// Load a PDF file with change tracking enabled.
var pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking);

// Placeholder for edits: You might add text, fill forms, or add annotations here.
// For example: pdf.Annotations.Add(new TextAnnotation(...));

// Sign the current state of the document using SignWithFile for convenience.
// We set permissions to allow further signatures and form filling.
pdf.SignWithFile(
    "assets/IronSignature.p12", 
    "password", 
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed);

// Save the current state as a distinct revision within the PDF's history.
PdfDocument pdfWithRevision = pdf.SaveAsRevision();

// Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf");
using IronPdf.Signing;

// Load a PDF file with change tracking enabled.
var pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking);

// Placeholder for edits: You might add text, fill forms, or add annotations here.
// For example: pdf.Annotations.Add(new TextAnnotation(...));

// Sign the current state of the document using SignWithFile for convenience.
// We set permissions to allow further signatures and form filling.
pdf.SignWithFile(
    "assets/IronSignature.p12", 
    "password", 
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed);

// Save the current state as a distinct revision within the PDF's history.
PdfDocument pdfWithRevision = pdf.SaveAsRevision();

// Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf");
Imports IronPdf.Signing

' Load a PDF file with change tracking enabled.
Private pdf = PdfDocument.FromFile("annual_census.pdf", ChangeTrackingModes.EnableChangeTracking)

' Placeholder for edits: You might add text, fill forms, or add annotations here.
' For example: pdf.Annotations.Add(new TextAnnotation(...));

' Sign the current state of the document using SignWithFile for convenience.
' We set permissions to allow further signatures and form filling.
pdf.SignWithFile("assets/IronSignature.p12", "password", SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed)

' Save the current state as a distinct revision within the PDF's history.
Dim pdfWithRevision As PdfDocument = pdf.SaveAsRevision()

' Save the final PDF with its full revision history to a new file.
pdfWithRevision.SaveAs("annual_census_signed.pdf")
$vbLabelText   $csharpLabel

理解增量保存是進階 PDF 工作流的關鍵。 雖然簡單的檢視器可能只顯示最新版本,但像 Adobe Acrobat 這樣的工具可以揭示整個修訂歷史,顯示誰簽署了哪個版本以及各簽名間所進行的更改。 IronPDF 讓您對該過程擁有全面的程序控制。

對於需要高安全性及合規性管理複雜文件工作流程的企業,可能需要一套全面的解決方案。 Iron Software 提供Iron Suite,其中包括用于簽名和操作的 IronPDF,再加上其他廣泛文件處理任務的庫,只需一次性付款即可獲得。

如何管理和驗證跨修訂的簽名?

一個 PDF 文件可以有多個簽名應用於其各修訂中。 IronPDF 提供了有效管理此歷史的工具。

  • 回滾至先前修訂:您可以使用GetRevision方法將文件恢復至較早的狀態。 這會捨棄在該修訂後的所有更改和簽名。
  • 驗證所有簽名VerifySignatures方法檢查文件所有修訂內所有簽名的有效性。 它僅在每個簽名均有效且沒有未經授權的更改時才返回true
  • 移除簽名RemoveSignatures方法會剝除文件每個修訂的所有數位簽名,創建一個乾淨的未簽名版本。
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Verify all signatures across all revisions.
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
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.
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
' Load a PDF with a complex signature history.
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")

' Verify all signatures across all revisions.
Dim allSignaturesValid As Boolean = pdf.VerifySignatures()
Console.WriteLine($"All signatures are valid: {allSignaturesValid}")

' Roll back to the first revision (index 0).
If pdf.RevisionCount And gt; 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.
pdf.RemoveSignatures()
pdf.SaveAs("report_unsigned.pdf")
$vbLabelText   $csharpLabel

如何在 PDF 上蓋印手寫簽名?

有時,您不需要數位簽名的加密安全性,僅僅想要應用視覺電子簽名,比如掃描的手寫簽名。 這通常稱為蓋印。 IronPDF 可以通過其WatermarkStamp功能來實現。

讓我們從一個範例發票 PDF 和一個.png格式的手寫簽名圖像開始。

在蓋印簽名前的原始發票 PDF

這是我們將應用的簽名圖像:

手寫簽名的圖像 範例手寫簽名圖像

以下代碼使用Watermark屬性將此圖像蓋印在 PDF 的右下角。

using IronPdf.Editing;

// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create an HtmlStamp containing our signature image.
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
{
    // 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.
};

// Apply the stamp to all pages of the PDF.
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.
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
{
    // 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.
};

// Apply the stamp to all pages of the PDF.
pdf.ApplyStamp(signatureStamp);

// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
Imports IronPdf.Editing

' Load the existing PDF document.
Private pdf = PdfDocument.FromFile("invoice.pdf")

' Create an HtmlStamp containing our signature image.
Dim signatureStamp = New HtmlStamp("&lt;img src='assets/signature.png'/&gt;")
If True Then
	' Configure the stamp's position and appearance.
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = 10, Opacity = 90
	10, Opacity = 90 ' Make it slightly transparent.
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = 10, Opacity
	HorizontalAlignment.Right, Margin = 10, Opacity
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin
	VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin
	VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment
End If

' Apply the stamp to all pages of the PDF.
pdf.ApplyStamp(signatureStamp)

' Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf")
$vbLabelText   $csharpLabel

蓋印 PDF 結果

代碼執行後,簽名圖像將蓋印於文件上,創建一個視覺簽署的發票。

最終 PDF 圖像蓋印手寫簽名於右下角

如何為 PDF 添加互動式簽名欄位?

對於需要用戶在 PDF 檢視器如 Adobe Acrobat 中簽名的文件,您可以添加互動式簽名表單欄位。 這創建了一個空的、可點擊的區域,提示用戶施加自己的數位簽名。

您可以創建SignatureFormField並將其添加到 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("&lt;h1&gt;Please Sign Below&lt;/h1&gt;");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";
int pageIndex = 0; // Add to the first page.
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
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("&lt;h1&gt;Please Sign Below&lt;/h1&gt;");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";
int pageIndex = 0; // Add to the first page.
var fieldRect = new Rectangle(50, 200, 300, 100); // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
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.
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("&lt;h1&gt;Please Sign Below&lt;/h1&gt;")

' Define the properties for the signature form field.
Private fieldName As String = "ClientSignature"
Private pageIndex As Integer = 0 ' Add to the first page.
Private fieldRect = New Rectangle(50, 200, 300, 100) ' Position: (x, y), Size: (width, height)

' Create the SignatureFormField object.
Private 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")
$vbLabelText   $csharpLabel

當用戶打開此 PDF 時,他們將看到一個可點擊欄位,允許他們使用自己的數位 ID 完成簽名過程。 您可以在我們的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}");
});
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

在導入簽名的 PDF 文件後,我們利用GetVerifiedSignatures方法來擷取報告中的VerifiedSignature物件列表並打印每個簽名的SignerName

請注意請注意,該值是從證書的主體可辨名稱 (SubjectDN) 中提取的,如果 CN 欄位不存在,將返回 null

下一步

本指南展示了 IronPDF 強大而靈活的 PDF 簽名功能。 無論您需要應用具有詳細元數據的安全數位簽名,管理文件修訂,蓋印視覺簽名,還是創建交互式表單,IronPDF 提供了一個全面且對開發者友好的 API 來完成任務。

To continue exploring, you can download the IronPDF library for .NET and get a free trial license to test out all its features in your projects.

準備看看您還能做哪些其他事情嗎? 查看我們的教程頁面:簽名和保護 PDF

常見問題解答

如何使用 C# 中的憑證檔案為 PDF 應用數位簽章?

若要在 C# 中使用憑證檔案為 PDF 套用數位簽名,可以使用 IronPDF。將憑證載入到X509Certificate2物件中,然後使用 IronPDF 的PdfSignature類,透過Sign方法將其套用到文件中。

使用數位簽章對PDF文件有什麼好處?

數位簽章可為 PDF 文件提供身分驗證和完整性保障。使用 IronPDF,您可以確保文件由經過驗證的來源簽名,並且自簽名後未被篡改。

如何在C#中為PDF新增手寫簽名?

IronPDF 讓您透過將手寫簽名影像蓋章到 PDF 上來新增簽名。您可以使用ApplyStamp方法和HtmlStamp對象,並指定圖像檔案來實現此功能。

如何在 PDF 檔案中新增互動式簽章網域?

若要在 PDF 中新增互動式簽章網域,請建立一個SignatureFormField對象,並指定所需的名稱、頁碼和位置。在儲存文件之前,使用 IronPDF 將此欄位新增至PdfDocument.Form集合中。

我可以控制已簽署PDF的權限嗎?

是的,使用 IronPDF 簽署 PDF 時,您可以指定SignaturePermissions來控製文件簽署後允許進行的變更。選項包括NoChangesAllowedFormFillingAllowedAnnotationsAndFormFillingAllowed

如何驗證PDF文件數位簽章的真實性?

IronPDF 提供了一個VerifySignatures方法,用於檢查 PDF 中所有簽名的有效性。它會傳回一個布林值,指示所有簽章是否真實有效且未被竄改。

PDF 中的增量保存是什麼?它有什麼用?

PDF 的增量保存功能允許在單一文件中儲存多個文件版本。此功能有助於維護變更歷史記錄,並確保每個數位簽章都與簽章時對應的文件版本一致。

如何在PDF文件中為數位簽章新增元資料?

使用 IronPDF,您可以為數位簽名添加元數據,例如簽名原因、地點和時間戳記。這些資訊可以在建立PdfSignature物件時設置,從而可以詳細記錄簽名過程。

我可以在 .NET 10 應用程式中使用 IronPDF 的數位簽章功能嗎?

是的。 IronPDF 完全支援 .NET 10,因此本文中展示的基於 X509Certificate2 的數位簽章工作流程同樣適用於 .NET 10 主機應用程式、Web 應用程式和服務應用程式。只需建立一個 .NET 10 項目,從 NuGet 安裝最新的 IronPDF 包,即可重複使用簽章程式碼來套用不可見或可見的簽章、簽章影像和簽章表單欄位。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布