Sign PDF Programmatically in C# .NET 10: 數位簽章 Guide

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

C# .NET 中的數位簽章利用基於 X.509 憑證的加密技術對 PDF 文件進行驗證,提供篡改偵測、不可否認性及長期有效性,這些是單純的視覺簽名圖像無法提供的。 IronPDF 讓 .NET Framework 開發人員能夠輕鬆地透過程式碼對 PDF 進行簽署驗證保護,涵蓋範圍從基本的憑證簽署和視覺化簽名渲染,到多方核准工作流程、時間戳記伺服器整合,以及符合 eIDAS 和《電子簽名法》(《電子簽名法》(ESIGN Act))等法規框架的要求。

TL;DR:快速入門指南

本教學將介紹如何使用 C# 透過 X.509 憑證對 PDF 文件進行程式化簽署、驗證及安全保護,內容涵蓋從單一簽署到多方核准鏈的各種情境。

  • 適用對象:正在將文件簽署功能整合至合約管理、發票處理或合規系統中的 .NET 開發人員。
  • 您將開發的內容:基於憑證的 PDF 簽署 (.p12)、視覺化簽名疊加層、多方順序工作流程、簽名驗證、篡改偵測,以及基於權限控制的文件鎖定功能。
  • 支援平台:.NET 10、.NET 8 LTS、.NET Framework 4.6.2 以上版本,以及 .NET Standard 2.0。
  • 適用情境:當您需要以程式化方式大規模簽署 PDF 文件,且無需透過第三方簽署平台
  • 技術層面的重要性:加密簽名能提供篡改證明、不可否認性及長期有效性,這些是視覺簽名圖像無法提供的。

若要跟著本教學中的程式碼範例操作,請參閱快速安裝指南,將 IronPDF 設定到您的專案中。 只需幾行程式碼,即可為您的首份 PDF 簽名:

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 請複製並執行此程式碼片段。

    var signature = new IronPdf.Signing.PdfSignature("certificate.pfx", "password");
    IronPdf.PdfDocument.FromFile("document.pdf").Sign(signature).SaveAs("signed.pdf");
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronPDF

    arrow pointer

下載完整專案

若要快速上手,請從本教學中下載包含所有程式碼範例的完整運作專案。

下載內容包含一個已完全配置的 .NET 專案,內含範例憑證及所有簽名範例,可直接編譯並執行。

購買 IronPDF 或註冊 30 天試用版後,請在應用程式啟動時輸入您的授權金鑰。

IronPdf.License.LicenseKey = "KEY";
IronPdf.License.LicenseKey = "KEY";
Imports IronPdf

IronPdf.License.LicenseKey = "KEY"
$vbLabelText   $csharpLabel

立即透過免費試用,在您的專案中開始使用 IronPDF。

第一步:
green arrow pointer
NuGet 透過 NuGet 安裝

PM >  Install-Package IronPdf

請至 NuGet 查閱 https://www.nuget.org/packages/IronPdf 以快速安裝。該套件下載量已突破 1,000 萬次,正透過 C# 徹底改變 PDF 開發領域。 您亦可下載 DLL 檔案或 Windows 安裝程式

目錄


PDF 文件中的"數位簽名"與"視覺簽名"有何區別?

人們常將"數位簽名"與"電子簽名"混為一談,但在文件安全性方面,兩者的運作方式卻大不相同。 數位簽章利用加密技術,以私鑰對文件內容的雜湊值進行加密。 任何擁有對應公開金鑰的人,皆可驗證該文件自簽署以來未曾變更。

另一方面,視覺簽名僅是放置於 PDF 頁面上的圖片。 這可能是掃描的手寫簽名、公司標誌,或是名稱的藝術化文字版本。 雖然視覺簽名能讓文件在人類讀者眼中看起來像是"已簽署",但它們並無法提供防篡改的加密保護。

實際上,企業應用程式通常需要這兩種類型的工具協同運作。 法律合約可能需要透過基於憑證的數位簽章進行加密保護,以確保文件完整性,並需包含可見的簽名區塊,以便收件者能確認簽署者身分及簽署時間。

下表概述了這些簽名類型之間的關鍵差異:

特點 數位簽章 Visual Signature
加密保護 使用 PKI 和 X.509 憑證建立防篡改印章 無,圖片可移除或替換
不可否認性 簽署人無法合理地否認簽署 未提供技術身分證明
篡改偵測 任何修改將導致簽名失效 無法偵測文件是否遭竄改
法律地位 符合《電子簽名法》(《電子簽名法》(ESIGN Act))及《電子識別、認證與信任服務法》(eIDAS)等法規 僅可能滿足"簽署意圖"之要求
驗證 可透過程式碼或 PDF 閱讀器進行驗證 僅需進行目視檢查
需提供證書 是的,需要 .pfx 或 .p12 憑證檔案 不,任何圖像檔案皆可

對於大多數商業應用情境而言,結合視覺呈現的數位簽章,能提供安全性與易用性最完善的組合。 加密層確保文件完整性,而視覺層則為收件者提供熟悉的簽署體驗。


開發團隊該如何建立數位簽章基礎架構?

實作數位簽章需要兩項條件:用於簽名的 X.509 憑證,以及對生產環境中憑證管理運作方式的相關知識。 本節將逐一介紹各個元件。

IronPDF 支援哪些 PDF 簽署的憑證格式?

數位簽章仰賴 X.509 憑證,其中包含一組公鑰與私鑰,以及憑證持有人的身分資訊。 此類憑證的有效期通常為一至三年。 IronPDF 支援標準 PKCS#12 格式的憑證,通常儲存為 .pfx.p12 檔案。 這些檔案將私密金鑰(用於簽署)與公開憑證(用於驗證)整合至單一受密碼保護的容器中。

Enterprise環境通常會從以下幾種來源之一取得憑證:

商業憑證授權機構(如 DigiCert、Sectigo 或 GlobalSign)所簽發的憑證,會自動獲得主要 PDF 閱讀器的信任。

內部 PKI 基礎架構允許組織自行簽發憑證,但接收者可能需要手動信任簽發的 CA。

自簽名憑證適用於原型開發,但在 PDF 閱讀器中會顯示警告,除非手動將其加入受信任清單。

載入 IronPDF 的憑證時,必須指定 X509KeyStorageFlags.Exportable 標誌。 這使加密子系統能夠存取用於簽名的私密金鑰。 以下是正確載入憑證的方法:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/load-certificate.cs
using System.Security.Cryptography.X509Certificates;

// Load the certificate from a PKCS#12 file (.pfx or .p12)
// The Exportable flag allows the private key to be used for signing
var certificate = new X509Certificate2(
    "company-signing-cert.pfx",
    "certificate-password",
    X509KeyStorageFlags.Exportable
);
Imports System.Security.Cryptography.X509Certificates

' Load the certificate from a PKCS#12 file (.pfx or .p12)
' The Exportable flag allows the private key to be used for signing
Dim certificate As New X509Certificate2(
    "company-signing-cert.pfx",
    "certificate-password",
    X509KeyStorageFlags.Exportable
)
$vbLabelText   $csharpLabel

翻譯範例:

憑證已成功載入
  主旨:CN=Test Signer, O=Test Organization, C=US
  簽發者:CN=Test Signer, O=Test Organization, C=US
  生效日期:2026-01-27 上午 7:50:04
  有效期至:2027-01-27 上午 8:00:03
  數位指紋:41355DE5ADD66CD64B2B99FF2CF87B9C87BD412F
  是否包含私密金鑰:是

在生產環境中,請從 Azure Key Vault、AWS Secrets Manager 或 HashiCorp Vault 等安全配置系統中提取憑證密碼,而非將其硬編碼。 請以適當的存取控制儲存憑證檔案,且切勿將其提交至版本控制系統。


開發人員如何透過程式設計方式在 PDF 文件中套用數位簽章?

IronPDF 的基本簽名工作流程包含:從憑證建立 PdfSignature 物件,並將其套用至 PDF 檔案。 本節涵蓋簽署流程,以及新增元資料與設定簽署行為的選項。

What does Basic Certificate Signing look like in C#?

最簡單的簽署情境是載入現有 PDF 檔案、透過憑證建立簽名,並儲存已簽署的結果。 IronPDF 會在後台處理所有加密運算:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/basic-signing.cs
using IronPdf;
using IronPdf.Signing;

// Load the PDF document that needs to be signed
PdfDocument pdf = PdfDocument.FromFile("contract.pdf");

// Create a signature object using the certificate file path and password
var signature = new PdfSignature("certificate.pfx", "password");

// Apply the cryptographic signature to the document
// This embeds an invisible digital signature in the PDF structure
pdf.Sign(signature);

// Save the signed document to a new file
pdf.SaveAs("contract-signed.pdf");
Imports IronPdf
Imports IronPdf.Signing

' Load the PDF document that needs to be signed
Dim pdf As PdfDocument = PdfDocument.FromFile("contract.pdf")

' Create a signature object using the certificate file path and password
Dim signature As New PdfSignature("certificate.pfx", "password")

' Apply the cryptographic signature to the document
' This embeds an invisible digital signature in the PDF structure
pdf.Sign(signature)

' Save the signed document to a new file
pdf.SaveAs("contract-signed.pdf")
$vbLabelText   $csharpLabel

輸入

contract.pdf - 簽署前的合約範本

contract.pdf - 簽署前的合約範本

輸出

這會產生一個內嵌了隱形數位簽名的 PDF 檔案。 當在 Adobe Acrobat 或其他支援簽名驗證的 PDF 閱讀器中開啟時,文件會顯示簽名驗證資訊,說明簽名是否有效,以及文件在簽署後是否曾被修改。

對於僅需簽署文件、無需將文件載入記憶體進行其他修改的情境,IronPDF 提供了一種簡化的單行解決方案:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/one-line-signing.cs
using IronPdf;
using IronPdf.Signing;

// One-line approach for signing PDFs
// Useful for batch processing where you don't need to manipulate the document
var signature = new PdfSignature("certificate.pfx", "password");
PdfDocument.FromFile("document.pdf").Sign(signature).SaveAs("document-signed.pdf");
Imports IronPdf
Imports IronPdf.Signing

' One-line approach for signing PDFs
' Useful for batch processing where you don't need to manipulate the document
Dim signature As New PdfSignature("certificate.pfx", "password")
PdfDocument.FromFile("document.pdf").Sign(signature).SaveAs("document-signed.pdf")
$vbLabelText   $csharpLabel

這對於批量處理特別方便,當需要簽署大量文件且無需進行其他變更時尤為實用。

應如何為稽核追蹤設定簽名元資料?

數位簽章可攜帶元資料,用以提供簽署事件的背景資訊。 此資訊會顯示於 PDF 閱讀器的簽名面板中,並納入文件的稽核追蹤紀錄。 IronPDF 支援多種標準元資料欄位:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/signature-metadata.cs
using IronPdf;
using IronPdf.Signing;
using System;

// Load the document to be signed
PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

// Create a signature with the company certificate
var signature = new PdfSignature("certificate.pfx", "password")
{
    // Add metadata to create an audit trail
    // This information appears in the signature panel of PDF readers
    SigningReason = "Invoice Approval",
    SigningLocation = "New York Office",
    SigningContact = "accounts@company.com",
    SignatureDate = DateTime.UtcNow
};

// Apply the signature with all metadata included
pdf.Sign(signature);
pdf.SaveAs("invoice-approved.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System

' Load the document to be signed
Dim pdf As PdfDocument = PdfDocument.FromFile("invoice.pdf")

' Create a signature with the company certificate
Dim signature As New PdfSignature("certificate.pfx", "password") With {
    ' Add metadata to create an audit trail
    ' This information appears in the signature panel of PDF readers
    .SigningReason = "Invoice Approval",
    .SigningLocation = "New York Office",
    .SigningContact = "accounts@company.com",
    .SignatureDate = DateTime.UtcNow
}

' Apply the signature with all metadata included
pdf.Sign(signature)
pdf.SaveAs("invoice-approved.pdf")
$vbLabelText   $csharpLabel

輸入

invoice.pdf - 簽署前的發票

invoice.pdf - 簽署前的發票

輸出

元資料欄位在文件工作流程中具有不同的用途:

元資料欄位 目的 範例值
簽署理由 說明為何簽署該文件 "合約核准"、"稽核認證"
簽署位置 發生簽署的記錄 "紐約辦公室"、"遠端居家辦公"
簽署聯絡人 提供聯絡資訊以供諮詢 "legal@company.com", "+1 555 0123"
簽署日期 簽署事件的時間戳記 DateTime.UtcNow

在Enterprise環境中,這些領域通常與營運資料息息相關。 發票核准系統可能會將簽署原因設為採購訂單編號,而合約管理系統則可能包含合約編號與核准階段。

時間戳記伺服器在簽名隨時間推移的有效性中扮演什麼角色?

數位簽章包含顯示文件簽署時間的時戳記,但此時戳記源自簽署電腦的本地時鐘。 對於未來數年後可能需要驗證的簽名,或當證明確切簽署時間在法律上至關重要時,來自外部時間戳記授權機構 (TSA) 的可信時間戳記能提供更強有力的證據。

時間戳記伺服器可提供加密證明,證明文件在特定時刻以當前形式存在。 即使簽名憑證日後過期或遭撤銷,時間戳記仍會顯示該簽名在套用時是有效的。 IronPDF 支援 RFC 3161 時間戳記伺服器:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/timestamp-server.cs
using IronPdf;
using IronPdf.Signing;
using System;

// Load the document to sign
PdfDocument pdf = PdfDocument.FromFile("agreement.pdf");
var signature = new PdfSignature("certificate.pfx", "password")
{
    SigningReason = "Agreement Execution",
    // Configure a trusted timestamp server (RFC 3161 compliant)
    // This provides cryptographic proof of when the document was signed
    TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256,
    TimeStampUrl = "http://timestamp.digicert.com"
};

// Apply the signature with the trusted timestamp
pdf.Sign(signature);
pdf.SaveAs("agreement-timestamped.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System

' Load the document to sign
Dim pdf As PdfDocument = PdfDocument.FromFile("agreement.pdf")
Dim signature As New PdfSignature("certificate.pfx", "password") With {
    .SigningReason = "Agreement Execution",
    ' Configure a trusted timestamp server (RFC 3161 compliant)
    ' This provides cryptographic proof of when the document was signed
    .TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256,
    .TimeStampUrl = "http://timestamp.digicert.com"
}

' Apply the signature with the trusted timestamp
pdf.Sign(signature)
pdf.SaveAs("agreement-timestamped.pdf")
$vbLabelText   $csharpLabel

輸入

agreement.pdf - 簽署前的服務協議

agreement.pdf - 簽署前的服務協議

輸出

目前有多個公開的時間戳記伺服器可供一般使用,其中包括由主要憑證授權機構營運的伺服器。 較大型的組織也可能運行遵循公司安全政策的內部時間戳記伺服器。

哈希演算法的選擇對延長有效期至關重要。 SHA 256 是當前的標準,但 IronPDF 亦支援 SHA 512 以滿足更嚴格的安全需求。 應避免使用 SHA-1 等舊式演算法,因其已不再被視為具備加密安全性。

在文件中,何時應將簽名設為隱藏,何時又應設為可見?

數位簽名可透過兩種模式應用:僅存在於 PDF 加密結構中的隱形簽名,或同時在指定頁面顯示圖形化呈現的可見簽名。 選擇取決於文件的用途及其受眾的期望。

隱形簽名適用於以下情境:無需人工審核的自動化文件處理、需維持原始文件版面配置不變的情況,以及多簽名工作流程中,若使用可見簽名會導致文件過於雜亂的情形。

當收件者期望看到簽名的視覺證據、工作流程要求將簽名放置於特定位置,或文件將被列印且簽名需顯示於紙本上時,建議採用可見簽名。

下一節將詳細介紹視覺簽名的實作方式。


如何將視覺簽名外觀新增至數位簽名的 PDF 檔案中?

許多商業流程已圍繞可見的簽名區塊發展而成,收件者通常期望能看到文件簽署的位置與時間。 IronPDF 讓您能在應用加密保護的同時,於頁面顯示可見的簽名,讓您兩全其美。

如何載入並定位簽名圖像?

視覺簽名通常是指放置在 PDF 頁面特定位置的圖像(例如掃描的手寫簽名、公司印章或樣式化的文字區塊)。 IronPDF 的 LoadSignatureImageFromFile 方法負責處理定位與渲染:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/visual-signature.cs
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;

// Load the document to sign
PdfDocument pdf = PdfDocument.FromFile("contract.pdf");
var signature = new PdfSignature("certificate.pfx", "password")
{
    SigningReason = "Contract Approval",
    SigningLocation = "Head Office"
};

// Define the position and size for the visible signature image
// Rectangle parameters: x position, y position, width, height (in points)
// Points are measured from the bottom-left corner of the page
var signatureArea = new Rectangle(150, 100, 200, 50);

// Load and attach the visual signature image
// The image will appear at the specified location on the document
signature.LoadSignatureImageFromFile(
    "signature-image.png",   // Path to the signature image file
    0,                       // Page index (0 = first page)
    signatureArea            // Position and dimensions
);

// Apply both the cryptographic signature and visual representation
pdf.Sign(signature);
pdf.SaveAs("contract-visually-signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' Load the document to sign
Dim pdf As PdfDocument = PdfDocument.FromFile("contract.pdf")
Dim signature As New PdfSignature("certificate.pfx", "password") With {
    .SigningReason = "Contract Approval",
    .SigningLocation = "Head Office"
}

' Define the position and size for the visible signature image
' Rectangle parameters: x position, y position, width, height (in points)
' Points are measured from the bottom-left corner of the page
Dim signatureArea As New Rectangle(150, 100, 200, 50)

' Load and attach the visual signature image
' The image will appear at the specified location on the document
signature.LoadSignatureImageFromFile(
    "signature-image.png",   ' Path to the signature image file
    0,                       ' Page index (0 = first page)
    signatureArea            ' Position and dimensions
)

' Apply both the cryptographic signature and visual representation
pdf.Sign(signature)
pdf.SaveAs("contract-visually-signed.pdf")
$vbLabelText   $csharpLabel

輸出

座標系統採用以頁面左下角為起點的點(1/72 英吋)進行測量。 對於標準的美國信紙尺寸頁面(612 x 792 點),若要在右下角附近放置簽名,需同時考量簽名大小與適當的邊距。

從不同來源載入簽名圖像尚有其他方法:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/load-signature-image.cs
using IronPdf.Signing;
using IronSoftware.Drawing;
using System.IO;

// Create signature object
var signature = new PdfSignature("certificate.pfx", "password");
var signatureArea = new Rectangle(400, 50, 150, 75);

// Method 1: Load signature image directly from a file path
signature.LoadSignatureImageFromFile("signature.png", 0, signatureArea);

// Method 2: Load from a stream (useful for database-stored images)
using (FileStream imageStream = File.OpenRead("signature.png"))
{
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureArea);
}

// Method 3: Load from AnyBitmap (IronSoftware's cross-platform image type)
AnyBitmap signatureBitmap = AnyBitmap.FromFile("signature.png");
using (var stream = signatureBitmap.ToStream())
{
    signature.LoadSignatureImageFromStream(stream, 0, signatureArea);
}
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports System.IO

' Create signature object
Dim signature As New PdfSignature("certificate.pfx", "password")
Dim signatureArea As New Rectangle(400, 50, 150, 75)

' Method 1: Load signature image directly from a file path
signature.LoadSignatureImageFromFile("signature.png", 0, signatureArea)

' Method 2: Load from a stream (useful for database-stored images)
Using imageStream As FileStream = File.OpenRead("signature.png")
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureArea)
End Using

' Method 3: Load from AnyBitmap (IronSoftware's cross-platform image type)
Dim signatureBitmap As AnyBitmap = AnyBitmap.FromFile("signature.png")
Using stream As Stream = signatureBitmap.ToStream()
    signature.LoadSignatureImageFromStream(stream, 0, signatureArea)
End Using
$vbLabelText   $csharpLabel

支援的圖像格式包括 PNG、JPEG、GIF、BMP、TIFF 及 WebP。具透明背景的 PNG 檔案非常適合用作簽名圖像,因為透明背景能讓底層的文件內容透出。

跨頁面的簽名定位功能如何運作?

簽署多頁文件時,數位簽章會保護整份文件,無論頁數多少。 單一加密簽名涵蓋 PDF 中的所有頁面:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/multi-page-signing.cs
using IronPdf;
using IronPdf.Signing;

// Load a multi-page document
PdfDocument pdf = PdfDocument.FromFile("multi-page-contract.pdf");

// Create signature - digital signatures protect the entire document
// regardless of page count
var signature = new PdfSignature("certificate.pfx", "password");

// Sign and save the document
// The signature applies to all pages in the document
pdf.Sign(signature);
pdf.SaveAs("contract-signed-last-page.pdf");
Imports IronPdf
Imports IronPdf.Signing

' Load a multi-page document
Dim pdf As PdfDocument = PdfDocument.FromFile("multi-page-contract.pdf")

' Create signature - digital signatures protect the entire document
' regardless of page count
Dim signature As New PdfSignature("certificate.pfx", "password")

' Sign and save the document
' The signature applies to all pages in the document
pdf.Sign(signature)
pdf.SaveAs("contract-signed-last-page.pdf")
$vbLabelText   $csharpLabel

輸入

multi-page-contract.PDF(最後一頁) - 簽署前的簽名頁

multi-page-contract.PDF(最後一頁) - 簽署前的簽名頁

輸出

對於跨頁簽名(例如法律協議中每頁的簽名縮寫),開發人員可將圖像印章與加密簽名分開處理:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/initials-stamp.cs
using IronPdf;
using IronPdf.Editing;
using IronPdf.Signing;
using IronSoftware.Drawing;

// Load the document
PdfDocument pdf = PdfDocument.FromFile("agreement.pdf");

// Create an image stamp for initials that will appear on every page
var initialsStamp = new ImageStamper("initials.png")
{
    HorizontalAlignment = HorizontalAlignment.Right,
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalOffset = new Length(50, MeasurementUnit.Points),
    VerticalOffset = new Length(50, MeasurementUnit.Points)
};

// Apply initials stamp to all pages
pdf.ApplyStamp(initialsStamp);

// Now apply the cryptographic signature with a full signature image on the last page
var signature = new PdfSignature("certificate.pfx", "password");
var signatureArea = new Rectangle(100, 100, 200, 100);
signature.SignatureImage = new PdfSignatureImage(
    "full-signature.png",
    pdf.PageCount - 1,    // Last page only
    signatureArea
);

// Sign the entire document cryptographically
pdf.Sign(signature);
pdf.SaveAs("agreement-initialed-and-signed.pdf");
Imports IronPdf
Imports IronPdf.Editing
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' Load the document
Dim pdf As PdfDocument = PdfDocument.FromFile("agreement.pdf")

' Create an image stamp for initials that will appear on every page
Dim initialsStamp As New ImageStamper("initials.png") With {
    .HorizontalAlignment = HorizontalAlignment.Right,
    .VerticalAlignment = VerticalAlignment.Bottom,
    .HorizontalOffset = New Length(50, MeasurementUnit.Points),
    .VerticalOffset = New Length(50, MeasurementUnit.Points)
}

' Apply initials stamp to all pages
pdf.ApplyStamp(initialsStamp)

' Now apply the cryptographic signature with a full signature image on the last page
Dim signature As New PdfSignature("certificate.pfx", "password")
Dim signatureArea As New Rectangle(100, 100, 200, 100)
signature.SignatureImage = New PdfSignatureImage(
    "full-signature.png",
    pdf.PageCount - 1,    ' Last page only
    signatureArea
)

' Sign the entire document cryptographically
pdf.Sign(signature)
pdf.SaveAs("agreement-initialed-and-signed.pdf")
$vbLabelText   $csharpLabel

此方法將視覺元素(可能出現在多個頁面)與加密簽名(用於保護整個文件)分開處理。


多方簽署工作流程在Enterprise應用程式中如何運作?

複雜的業務流程通常需要不同人員依特定順序簽署多份文件。 採購訂單可能需要先經部門經理批准,接著由財務部門審核,最後由高層簽署。 IronPDF 透過增量儲存與簽名權限功能,支援這些工作流程。

何謂順序簽署?增量儲存又是如何實現此功能的?

PDF 文件可在內部儲存多個修訂版本,類似於版本控制。 每次有人簽署時,該簽署即適用於該文件當下的狀態。 後續簽署者將其簽名添加至新修訂版本,形成一串可獨立驗證的簽署鏈。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/sequential-signing-first.cs
using IronPdf;
using IronPdf.Signing;

// Load the purchase order document
PdfDocument pdf = PdfDocument.FromFile("purchase-order.pdf");

// First signer: Department Manager approves the purchase order
var managerSignature = new PdfSignature("certificate.pfx", "password")
{
    SigningReason = "Manager Approval",
    SigningLocation = "Department A"
};

// Sign the document and save
// This preserves the original state while adding the signature
pdf.Sign(managerSignature);
pdf.SaveAs("po-manager-approved.pdf");
Imports IronPdf
Imports IronPdf.Signing

' Load the purchase order document
Dim pdf As PdfDocument = PdfDocument.FromFile("purchase-order.pdf")

' First signer: Department Manager approves the purchase order
Dim managerSignature As New PdfSignature("certificate.pfx", "password") With {
    .SigningReason = "Manager Approval",
    .SigningLocation = "Department A"
}

' Sign the document and save
' This preserves the original state while adding the signature
pdf.Sign(managerSignature)
pdf.SaveAs("po-manager-approved.pdf")
$vbLabelText   $csharpLabel

輸入

purchase-order.pdf - 待核准的採購訂單

purchase-order.pdf - 待核准的採購訂單

輸出

當文件轉交給下一位審核者時,該審核者會載入文件並添加自己的簽名:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/sequential-signing-second.cs
using IronPdf;
using IronPdf.Signing;

// Load the document that already has the manager's signature
PdfDocument pdf = PdfDocument.FromFile("po-manager-approved.pdf");

// Second signer: Finance department verifies budget availability
var financeSignature = new PdfSignature("certificate.pfx", "password")
{
    SigningReason = "Finance Approval",
    SigningLocation = "Finance Department"
};

// Add the second signature
// Both signatures remain independently verifiable
pdf.Sign(financeSignature);
pdf.SaveAs("po-finance-approved.pdf");
Imports IronPdf
Imports IronPdf.Signing

' Load the document that already has the manager's signature
Dim pdf As PdfDocument = PdfDocument.FromFile("po-manager-approved.pdf")

' Second signer: Finance department verifies budget availability
Dim financeSignature As New PdfSignature("certificate.pfx", "password") With {
    .SigningReason = "Finance Approval",
    .SigningLocation = "Finance Department"
}

' Add the second signature
' Both signatures remain independently verifiable
pdf.Sign(financeSignature)
pdf.SaveAs("po-finance-approved.pdf")
$vbLabelText   $csharpLabel

輸出

每個修訂版本皆保留其專屬簽名,PDF 閱讀器可顯示完整的簽署歷史紀錄,包括簽署者與簽署時間。

在新增簽名之前,該如何驗證現有的簽名?

在文件中新增簽名之前,您的程式碼應先驗證現有簽名是否仍然有效。 若文件在非正規工作流程外被修改,可能導致簽名失效,這可能表示文件遭竄改或違反流程規範。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/verify-signatures.cs
using IronPdf;
using System;

// Load a signed document
PdfDocument pdf = PdfDocument.FromFile("contract-signed.pdf");

// Verify all existing signatures in the document
// Returns true only if ALL signatures are valid and untampered
bool isValid = pdf.VerifyPdfSignatures();

Console.WriteLine($"Signatures Valid: {isValid}");

// Get signature details
var signatures = pdf.GetVerifiedSignatures();
Console.WriteLine($"Number of Signatures: {signatures.Count}");
Imports IronPdf
Imports System

' Load a signed document
Dim pdf As PdfDocument = PdfDocument.FromFile("contract-signed.pdf")

' Verify all existing signatures in the document
' Returns true only if ALL signatures are valid and untampered
Dim isValid As Boolean = pdf.VerifyPdfSignatures()

Console.WriteLine($"Signatures Valid: {isValid}")

' Get signature details
Dim signatures = pdf.GetVerifiedSignatures()
Console.WriteLine($"Number of Signatures: {signatures.Count}")
$vbLabelText   $csharpLabel

如需更詳細的檢視,您可以檢索每個已驗證簽名的相關資訊:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/get-verified-signatures.cs
using IronPdf;
using System;

// Load a document with multiple signatures
PdfDocument pdf = PdfDocument.FromFile("multi-signed-document.pdf");

// Retrieve detailed information about each verified signature
var verifiedSignatures = pdf.GetVerifiedSignatures();

// Iterate through all signatures to build an audit trail
foreach (var sig in verifiedSignatures)
{
    Console.WriteLine($"Signer: {sig.SignerName}");
    Console.WriteLine($"Reason: {sig.SigningReason}");
    Console.WriteLine($"Location: {sig.SigningLocation}");
    Console.WriteLine($"Date: {sig.SigningDate}");
    Console.WriteLine($"Contact: {sig.SigningContact}");
    Console.WriteLine("---");
}
Imports IronPdf
Imports System

' Load a document with multiple signatures
Dim pdf As PdfDocument = PdfDocument.FromFile("multi-signed-document.pdf")

' Retrieve detailed information about each verified signature
Dim verifiedSignatures = pdf.GetVerifiedSignatures()

' Iterate through all signatures to build an audit trail
For Each sig In verifiedSignatures
    Console.WriteLine($"Signer: {sig.SignerName}")
    Console.WriteLine($"Reason: {sig.SigningReason}")
    Console.WriteLine($"Location: {sig.SigningLocation}")
    Console.WriteLine($"Date: {sig.SigningDate}")
    Console.WriteLine($"Contact: {sig.SigningContact}")
    Console.WriteLine("---")
Next
$vbLabelText   $csharpLabel

透過這些資訊,您可建立稽核追蹤紀錄、驗證核准流程,並確保文件在進入下一階段前已具備所有必要的簽名。

IronPDF 如何偵測遭篡改的文件?

若文件中的任何簽名無效,VerifyPdfSignatures() 方法將傳回 false。 這種情況通常發生在文件簽署後內容遭到修改、簽名資料本身損毀、憑證已被撤銷,或憑證在使用時尚未生效等情況下。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/detect-tampering.cs
using IronPdf;
using System;

// Load a signed document and verify
PdfDocument pdf = PdfDocument.FromFile("contract-signed.pdf");

// Check if all signatures are still valid
bool isValid = pdf.VerifyPdfSignatures();

if (isValid)
{
    Console.WriteLine("Document has not been tampered with");
    Console.WriteLine("All signatures are valid");
}
else
{
    Console.WriteLine("WARNING: Document may have been tampered with!");
    Console.WriteLine("One or more signatures are invalid");
}
Imports IronPdf
Imports System

' Load a signed document and verify
Dim pdf As PdfDocument = PdfDocument.FromFile("contract-signed.pdf")

' Check if all signatures are still valid
Dim isValid As Boolean = pdf.VerifyPdfSignatures()

If isValid Then
    Console.WriteLine("Document has not been tampered with")
    Console.WriteLine("All signatures are valid")
Else
    Console.WriteLine("WARNING: Document may have been tampered with!")
    Console.WriteLine("One or more signatures are invalid")
End If
$vbLabelText   $csharpLabel

對於需要移除簽名的應用(例如為了建立未簽名的副本以供重新分發),IronPDF 提供了 RemoveSignatures() 方法:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/remove-signatures.cs
using IronPdf;

// Load a signed document
PdfDocument pdf = PdfDocument.FromFile("signed-template.pdf");

// Remove all digital signatures from the document
// This strips signature data but does not restore previous document state
pdf.RemoveSignatures();

// Save as an unsigned version
pdf.SaveAs("unsigned-template.pdf");
Imports IronPdf

' Load a signed document
Dim pdf As PdfDocument = PdfDocument.FromFile("signed-template.pdf")

' Remove all digital signatures from the document
' This strips signature data but does not restore previous document state
pdf.RemoveSignatures()

' Save as an unsigned version
pdf.SaveAs("unsigned-template.pdf")
$vbLabelText   $csharpLabel

請注意,移除簽名並不會恢復任何先前的文件狀態。 此方法僅會從當前文件版本中移除簽名資料。


哪些技術功能可支援 PDF 簽名的法規遵循?

各司法管轄區與產業的數位簽章法規雖不盡相同,但在憑證驗證、時間戳記及簽章元資料方面,仍存在共通的技術需求。 本節不試圖詮釋具體的法律規定(相關事宜應諮詢合格的法律顧問),而是著重於合規框架通常所要求的技術能力。

數位簽名的關鍵監管框架有哪些?

下表概述了常見的法規框架及其一般技術要求:

框架 管轄權 關鍵技術要求
《電子簽名法》(ESIGN Act) 美國 簽署意圖、同意電子紀錄、紀錄保存
UETA 美國各州 與 ESIGN 類似,適用於州內交易
eIDAS 歐洲聯盟 三種簽名等級(基礎、進階、合格);合格等級需具備 QSCD 認證
21 CFR 第 11 部分 美國食品藥物管理局 (US FDA) 電子簽名必須與電子紀錄相關聯,且須具備稽核追蹤功能

這些框架通常認為,若能正確實施,數位簽名在法律上等同於手寫簽名。 數位簽名的證據效力,往往取決於能否證明該簽名在簽署時有效;正因如此,在文件需保存數年甚至數十年的情況下,可信時間戳記便顯得至關重要。

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/compliance-signing.cs
using IronPdf;
using IronPdf.Signing;
using System;

// Load the compliance document
PdfDocument pdf = PdfDocument.FromFile("compliance-document.pdf");
var signature = new PdfSignature("certificate.pfx", "password")
{
    // Add comprehensive metadata for audit trail requirements
    SigningReason = "21 CFR Part 11 Compliance Certification",
    SigningLocation = "Quality Assurance Department",
    SigningContact = "compliance@company.com",
    SignatureDate = DateTime.UtcNow,
    // Configure a trusted timestamp for regulatory compliance
    // The timestamp proves when the signature was applied
    TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256,
    TimeStampUrl = "http://timestamp.digicert.com"
};

// Apply the signature with timestamp and full metadata
pdf.Sign(signature);
pdf.SaveAs("compliance-document-certified.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System

' Load the compliance document
Dim pdf As PdfDocument = PdfDocument.FromFile("compliance-document.pdf")
Dim signature As New PdfSignature("certificate.pfx", "password") With {
    ' Add comprehensive metadata for audit trail requirements
    .SigningReason = "21 CFR Part 11 Compliance Certification",
    .SigningLocation = "Quality Assurance Department",
    .SigningContact = "compliance@company.com",
    .SignatureDate = DateTime.UtcNow,
    ' Configure a trusted timestamp for regulatory compliance
    ' The timestamp proves when the signature was applied
    .TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256,
    .TimeStampUrl = "http://timestamp.digicert.com"
}

' Apply the signature with timestamp and full metadata
pdf.Sign(signature)
pdf.SaveAs("compliance-document-certified.pdf")
$vbLabelText   $csharpLabel

輸入

compliance-document.pdf - 待認證之合規文件

compliance-document.pdf - 待認證之合規文件

輸出

受監管行業適用哪些證書要求?

不同的法規環境可能會針對簽名所使用的憑證訂定特定標準。 這些標準可能包括由認可機構簽發、最低金鑰長度(通常為 2048 位元 RSA 或同等規格)、特定的擴展金鑰使用屬性,或是使用 HSM 或智慧卡解決方案進行的硬體式金鑰儲存。

IronPDF 支援任何符合標準格式規範的 X.509 憑證,讓您能夠使用來自您偏好的憑證授權機構所發行的憑證。 對於需要硬體保護金鑰的環境,IronPDF 透過其 HsmSigner 功能,支援基於 PKCS#11 的 HSM 整合。

受監管產業的從業者應與法務及合規部門合作,釐清具體需求,進而建立相應的簽署基礎架構。

"簽名權限控制"如何支援文件生命週期管理?

簽署後,文件有時需保持可編輯狀態以滿足特定需求,例如讓其他簽署人添加簽名,或允許填寫表單欄位。 IronPDF 的 SignaturePermissions 枚舉控制簽署後允許進行哪些變更:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-digital-signatures-csharp-guide/signature-permissions.cs
using IronPdf;
using IronPdf.Signing;

// Load a form document that needs signing
PdfDocument pdf = PdfDocument.FromFile("approval-form.pdf");

// Sign with specific permissions for document lifecycle management
// AdditionalSignaturesAndFormFillingAllowed permits form completion and additional signatures after signing
pdf.SignWithFile(
    "approver-cert.pfx",
    "password",
    null,
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed
);

// Save the signed document with form-filling permissions enabled
pdf.SaveAs("approval-form-signed.pdf");
Imports IronPdf
Imports IronPdf.Signing

' Load a form document that needs signing
Dim pdf As PdfDocument = PdfDocument.FromFile("approval-form.pdf")

' Sign with specific permissions for document lifecycle management
' AdditionalSignaturesAndFormFillingAllowed permits form completion and additional signatures after signing
pdf.SignWithFile(
    "approver-cert.pfx",
    "password",
    Nothing,
    SignaturePermissions.AdditionalSignaturesAndFormFillingAllowed
)

' Save the signed document with form-filling permissions enabled
pdf.SaveAs("approval-form-signed.pdf")
$vbLabelText   $csharpLabel

可用的權限等級包括:

權限等級 簽約後允許的變更
禁止修改 文件已完全鎖定
FormFillingAllowed 僅可填入表單欄位;內容不得變更
允許附加簽名與表單填寫 允許填寫表單及追加簽名
允許填寫表單與註解 允許填寫表單並添加註解

適當的權限等級取決於文件的角色。 已完成的協議可能使用 NoChangesAllowed,而仍在進行中的核准表單則可能使用 AdditionalSignaturesAndFormFillingAllowed,以便進行表單填寫及取得其他核准人的簽名。


在何種情況下,建立自訂簽署機制比使用外部服務更為合適?

任何需要實作文件簽署功能的人,都面臨一個根本性的架構決策:是將簽署功能整合到自己的應用程式中,還是使用託管式簽署服務。 兩種方法各有其適用情境,正確的選擇取決於翻譯量、客製化需求、合規考量以及總持有成本。

哪些因素有利於建立內部簽名能力?

以下是一些將簽名功能直接整合至應用程式中具有實用價值的場景:

由於文件數量龐大,按交易計費的模式在規模擴大時成本會相當高昂。 每月處理數千份文件的企业通常发现,像 IronPDF 这样的永久授權库在财务上比按次计费的 API 费用更具成本效益。 相較於持續的交易成本,一次性的授權投資能迅速回本。

當外部簽名服務帶有其自身的運作模式時,便會產生獨特的流程需求。 對於那些擁有特殊審核流程、特殊文件處理需求,或整合需求不符合標準服務的團隊而言,自行建置解決方案往往比試圖適應服務的限制來得容易。

基於資料主權與安全政策,部分企業因法規、安全政策或合約義務的限制,無法將文件傳送至外部服務。 將簽署流程保留在內部,可確保文件在整個過程中始終處於企業管轄範圍內。

離線或氣隙環境無法依賴需要網路連線的託管解決方案。 必須在離線環境下運作的應用程式(例如現場服務應用程式、機密系統或網路訊號不穩定的場所),需要具備本地實作的簽名功能。

成本可預測性至關重要,因為訂閱制定價會產生持續累積的開支。 永久授權的函式庫能提供穩定的成本,讓財務部門得以據此規劃預算,無需擔心使用量波動或價格上漲的問題。

哪些情境適合採用託管簽名解決方案?

託管解決方案在各種情況下都能發揮良好效能:

當文件需要公司外部人員簽署,而這些人員沒有自己的憑證時,就會發生簽署方眾多但簽署量較少的情況。 處理身分驗證與憑證簽發的服務能降低使用門檻。 若僅是偶爾使用,內部建置這類基礎架構通常得不償失。

當您需要快速上線運作,卻沒有開發資源從頭開始建置時,快速部署至關重要。 這些平台配備現成的使用者介面與電子郵件通知功能,提供開箱即用的解決方案。

當供應商專精於特定監管框架,並能提供若自行取得將耗費高昂成本的認證或證明時,合規委託便顯得極具價值。

決策往往取決於:簽名功能究竟是值得投資的核心能力,還是應交由專家處理的常規功能。 對於將文件處理作為營運核心的企業(如律師事務所、金融機構、醫療服務提供者、政府機關),擁有專屬的簽署基礎架構通常能帶來更高的投資回報。 對於那些將文件簽署視為次要需求的用戶,可能更傾向於選擇外包方案的簡便性。

開發團隊該如何評估其簽名基礎架構的需求?

在決定採用哪種翻譯方式之前,請考慮以下因素:

您可依據當前及預估的翻譯量進行成本分析。 目前有多少文件需要簽名,而這個數字未來會如何變化?

整合考量會影響您的架構設計。 簽名功能將如何融入現有的應用程式和工作流程? 函式庫能提供最大的靈活性,而外部平台則可能需要配合其 API 和使用者介面進行調整。

簽署方類型會影響複雜度。 文件是否僅由持有受管憑證的內部使用者簽署,還是也需要支援外部簽署者?

合規環境決定了技術要求。 適用哪些法規,它們又如何影響您的實作選擇? 某些要求透過服務來達成會比較容易; 其他情況則需內部管控。

技術資源將決定可行性。 團隊是否有足夠的資源來實作並維護簽名功能,還是這會分散對核心開發優先事項的注意力?


後續步驟

在 .NET 應用程式中整合數位簽章,意味著必須同時理解加密技術的基礎原理,以及驅動商業文件處理的實際需求。 IronPDF憑證式簽署視覺化簽名渲染、多方工作流程支援及簽名驗證提供了技術基礎,同時將何時以及如何使用這些功能的架構決策,交由了解自身特定需求的開發人員自行決定。

本指南所涵蓋的功能,無論您是自動化合約簽署、實作核准流程,還是滿足合規要求,皆能為穩健的簽署實作提供基礎建構模組。 對於需要硬體支援的金鑰儲存的 Enterprise 環境,HSM 簽名指南將這些模式延伸至 PKCS#11 裝置。 當數位簽章作為更廣泛的文件安全策略之一環時,請結合密碼保護、權限設定PDF 加密功能,以建立多層次防護機制。

準備開始建置了嗎? 下載 IronPDF 並透過免費試用版體驗其功能。 在做出任何購買決策之前,您可以使用實際文件來評估基於憑證的簽署、簽名驗證以及多方工作流程。 若您對簽名架構或合規整合有任何疑問,請聯繫我們的工程支援團隊

常見問題

在 PDF 的語境中,何謂數位簽章?

數位簽章是一種用於驗證 PDF 文件真實性與完整性的加密機制。它能確保文件未遭竄改,並確認簽署者的身分。

如何使用 IronPDF 在 C# 中實作數位簽章?

IronPDF 提供直觀的 API,讓您能使用 C# 在 PDF 中實作數位簽名。您可以透過憑證簽名、新增視覺化簽名以及管理多方工作流程,以程式化方式簽署文件。

IronPDF 支援哪些類型的數位簽章?

IronPDF 支援多種數位簽章類型,包括憑證式簽章、視覺化簽章,以及需透過時間戳記伺服器進行額外驗證的簽章。

我可以使用 IronPDF 驗證 PDF 中的數位簽章嗎?

是的,IronPDF 包含驗證 PDF 文件中數位簽章的功能,以確保文件真實無誤,且自簽署以來未遭竄改。

時間戳記伺服器在數位簽章中扮演什麼角色?

時間戳記伺服器提供可信賴的時間來源,用以確認數位簽章何時被應用於文件上。透過驗證簽署的確切時間,這為系統增添了額外的安全層級。

視覺簽名與數位憑證有何不同?

視覺簽名會在文件上顯示簽名的圖形化呈現,而數位憑證則提供關於真實性與完整性的加密證明,但不一定會顯示視覺標記。

IronPDF 能否處理多方簽名工作流程?

是的,IronPDF 具備管理多方簽名工作流程的能力,允許多個參與方以協調且安全的方式簽署文件。

在 PDF 文件中使用數位簽章有哪些好處?

數位簽章透過確保文件的完整性與真實性來提升安全性。此外,透過啟用電子簽署功能,數位簽章還能簡化工作流程,其速度與效率均優於傳統方法。

是否可以透過程式設計在無需使用者互動的情況下簽署 PDF 文件?

是的,透過 IronPDF,您可以在 C# 中以程式化方式簽署 PDF 文件,無需使用者介入,非常適合自動化工作流程和批次處理。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 18,918,602 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。