使用 C# .NET 10 以程式設計方式簽署 PDF:數位簽章指南

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

C# .NET中的數位簽章使用基於X.509 憑證的加密技術對 PDF 文件進行身份驗證,提供防篡改偵測、不可否認性和長期有效性,而這些是僅憑視覺簽章影像無法實現的。 IronPDF 使 .NET 開發人員能夠輕鬆地以程式設計方式對 PDF進行簽名驗證保護,涵蓋從基本證書簽名和視覺化簽名渲染到多方審批工作流程、時間戳伺服器整合以及與 eIDAS 和 ESIGN 法案等框架的監管合規性等所有內容。

TL;DR:快速入門指南

本教學涵蓋如何使用 C# 中的 X.509 憑證以程式設計方式對 PDF 文件進行簽署、驗證和保護,從單一簽章到多方審批鏈。

-適用對象:將文件簽署功能整合到合約管理、發票處理或合規系統中的 .NET 開發人員。 -你將建立的功能:基於憑證的PDF 簽名.pfx / .p12 )、視覺化簽名疊加、多方順序工作流程、簽名驗證、篡改偵測和權限控制的文件鎖定。 -運行環境: .NET 10、.NET 8 LTS、.NET 框架 4.6.2+ 和 .NET Standard 2.0。 -何時使用此方法:當您需要大規模地以程式設計方式簽署 PDF,而無需透過第三方簽署入口網站時。 -從技術角度來看,其重要性在於:加密簽名提供了視覺簽名圖像無法提供的防篡改證據、不可否認性和長期有效性。

若要跟隨本教學課程中的程式碼範例進行操作,請查看快速安裝指南,以便在您的專案中設定 IronPDF。 只需幾行程式碼即可簽署您的第一個 PDF 檔案:

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 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

!{--01001100010010010100001001010010010000010101001001011001010111110101001101010100010001010101010 10100010111110101010001010010010010010100000101001100010111110100001001001100010011111010000100100110001001111010101 !{--010011000100100101000010010100100100000101010010010110010101111101001110010101010101010101010101010101010101010 0100010111110100100101001101010100010000010100110001001100010111110100001001001100010011110010101010

as-heading:2(目錄)


PDF文件中的數位簽章和視覺簽章有什麼不同?

人們經常將"數位簽章"和"電子簽章"互換使用,但它們在文件安全方面的作用卻截然不同。 數位簽章使用加密技術,以私鑰對文件內容的雜湊值進行加密。 任何擁有相應公鑰的人都可以驗證該文件自簽名以來是否已更改。

另一方面,視覺簽名只是放置在 PDF 頁面上的圖像。 它可以是掃描的手寫簽名、公司標誌或名稱的風格化文字版本。 雖然視覺簽名能讓文件看起來像是"簽署"過的,但它們並不能提供任何防止篡改的加密保護。

在實際應用中,商業應用通常需要這兩種類型協同工作。 法律合約可能需要基於憑證的數位簽章進行加密保護,以確保文件的完整性,還需要一個可見的簽章區塊,以便接收者可以看到是誰在何時簽署的。

下表總結了這些簽名類型之間的主要區別:

特徵 數位簽章 視覺簽名
加密保護 使用 PKI 和 X.509 憑證建立防篡改封條 無,圖片可以移除或替換
不可否認性 簽署人無法令人信服地否認簽署 不提供任何技術性的身份證明
防篡改檢測 任何修改都會使簽名失效。 無法偵測文件是否被竄改。
法律地位 獲得《電子簽名法案》(ESIGN 法) 和《電子身分識別和授權法案》(eIDAS) 等法規的認可 可能僅滿足"簽署意願"要求
確認 可以透過程式設計方式或在 PDF 閱讀器中進行驗證 僅需目視檢查
所需證書 是的,需要 .pfx 或 .p12 憑證文件 不,任何圖像檔案都可以。

對於大多數商業應用場景而言,具有視覺表現形式的數位簽名能夠提供安全性和易用性的最佳組合。 加密層確保文件完整性,而視覺層為接收者提供熟悉的簽名體驗。


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

實現數位簽章需要兩樣東西:用於簽署的 X.509 憑證和一些關於憑證管理在生產環境中如何運作的知識。 本節將逐一介紹每個組成部分。

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

數位簽章依賴 X.509 證書,其中包含公鑰和私鑰對以及證書持有者的識別資訊。 這些證書的有效期限通常為一至三年。 IronPDF 可處理標準 PKCS#12 格式的證書,通常以.pfx.p12文件形式儲存。 這些文件將私鑰(用於簽署)與公鑰憑證(用於驗證)捆綁到一個受密碼保護的容器中。

企業環境通常從以下幾個來源之一取得證書:

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=測試簽署人,O=測試組織,C=美國
  簽發方:CN=測試簽署者,O=測試組織,C=美國
  有效期限自:2026年1月27日 上午7:50:04
  有效期限至:2027年1月27日 上午8:00:03
  指紋:41355DE5ADD66CD64B2B99FF2CF87B9C87BD412F
  擁有私鑰:是

在生產環境中,應從 Azure Key Vault、AWS Secrets Manager 或 HashiCorp Vault 等安全設定係統中提取憑證密碼,而不是將其硬編碼到系統中。 證書文件應妥善保管,並設定適當的存取控制,切勿將其提交到版本控制系統中。


開發人員如何以程式設計方式將數位簽章應用於 PDF 文件?

IronPDF 的基本簽章工作流程包括從憑證建立PdfSignature物件並將其套用至 PDF。 本節介紹簽章過程,以及新增元資料和設定簽章行為的選項。

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

在企業環境中,這些欄位通常與營運資料相關聯。 發票審批系統可能會將簽署理由設定為採購訂單號,而合約管理系統可能會包含合約 ID 和核准階段。

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

數位簽章包含一個時間戳,顯示文件簽署的時間,但該時間戳記來自簽署電腦的本機時鐘。 對於可能需要在多年後進行驗證的簽名,或者在法律上證明確切的簽名時間至關重要的情況,來自外部時間戳機構 (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 = 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

輸入

多頁合約.pdf(最後一頁)- 簽署前的簽名頁

多頁合約.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

這種方法將視覺元素(可以出現在多個頁面上)與加密簽名(保護整個文件)分開。


企業應用中的多方簽章工作流程是如何運作的?

複雜的業務流程通常需要不同人員以特定順序進行多次簽名。 採購訂單可能需要部門經理批准,然後經過財務審核,最後由高階主管簽署。 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 法 美國 簽署意向、同意電子記錄、記錄保存
UETA 美國各州 與電子簽名類似,適用於州內交易。
eIDAS 歐洲聯盟 三種簽名等級(簡單、高級、合格);合格簽名需要QSCD。
21 CFR 第 11 部分 美國食品藥物管理局 電子簽名必須與電子記錄關聯,並需要審計追蹤。

這些框架通常認為,如果實施得當,數位簽名在法律上與手寫簽名具有同等效力。 數位簽章的證據價值通常取決於證明簽章在應用時是有效的,因此,可信的時間戳對於多年或數十年的文件保存至關重要。

: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` 只能填寫表單欄位;不能更改內容。
AdditionalSignaturesAndFormFillingAllowed 允許填寫表格和額外簽名
FormFillingAndAnnotationsAllowed 允許填寫表格並添加註釋。

適當的權限等級取決於文件的作用。 已完成的協議可能會使用NoChangesAllowed ,而仍在進行中的審批表可能會使用AdditionalSignaturesAndFormFillingAllowed ,以允許其他審批人填寫表格和新增簽名。


什麼時候建立自訂簽章比使用外部服務更有意義?

任何實作文件簽章的人都會面臨一個根本性的架構決策:是將簽章功能建置到自己的應用程式中,還是使用託管的簽章服務。 兩種方法各有其用武之地,正確的選擇取決於數量、客製化需求、合規性考量以及整體擁有成本。

哪些因素有利於建構內部簽署能力?

以下是一些將簽名功能直接整合到應用程式中比較合理的場景:

文件數量龐大時,按交易計費的成本會很高。 每月處理數千份文件的企業通常會發現,像 IronPDF 這樣的永久許可庫比按簽名收費的 API 更划算。 與持續的交易成本相比,一次性許可證投資很快就能收回成本。

當外部簽約服務機構有其自身的模式時,就會產生獨特的流程要求。 對於審批流程不尋常、文件處理特殊或整合需求不符合標準產品的團隊來說,他們通常會發現,與其嘗試適應服務的局限性,不如自己建立解決方案來得容易。

由於法規、安全政策或合約義務的限制,一些公司出於資料主權和安全策略的考慮,無法將文件發送給外部服務機構。 內部簽署流程可以確保文件始終在公司範圍內進行。

離線或實體隔離環境無法依賴需要網路連線的託管解決方案。 必須在斷網環境下運行的應用程式(現場服務應用程式、機密系統或網路不穩定的地方)需要本地實作簽章功能。

成本可預測性很重要,因為訂閱式定價會產生持續累積的費用。 永久圖書館許可證提供穩定的成本,財務部門可以據此進行規劃,而無需擔心訪問量波動或價格上漲。

哪些場景適合使用託管簽章解決方案?

託管解決方案在不同情況下都能很好地發揮作用:

當文件需要公司外部人員簽字,而這些人員又沒有自己的簽字證書時,就會出現簽字量少、簽字方多元化的情況。 提供身份驗證和證書頒發服務可以減少摩擦。 對於偶爾使用的情況來說,內部建設這種基礎設施很少能帶來利益。

當您需要快速啟動註冊和運行,但又沒有開發資源從頭開始建置時,快速部署就顯得至關重要。 這些平台配備了現成的使用者介面和電子郵件通知功能,提供開箱即用的功能。

當供應商專注於特定的監管框架,並能提供獨立取得成本高昂的認證或證明時,合規委託就變得有價值。

最終的決定往往取決於簽約是否是一項值得投資的核心競爭力,還是最好留給專業人士的商品職能。 將文件處理作為其營運核心部分的企業(律師事務所、金融機構、醫療保健提供者、政府機構)通常從擁有自己的簽署基礎設施中獲得更好的回報。 對於文件簽署並非首要考慮因素的使用者來說,外包方案的簡單性可能更受青睞。

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

在決定採用哪一種方法之前,請先考慮以下因素:

目前和預期的銷售決定了您的成本分析。 現在有多少份文件需要簽名?隨著時間推移,這個數字會發生什麼樣的變化?

整合方面的考慮會影響您的架構。 簽章功能將如何融入現有的應用程式和工作流程? 庫提供了最大的靈活性,而外部平台可能需要適應其 API 和使用者介面。

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

合規環境決定了技術要求。 有哪些法規適用,它們會如何影響您的實施方案選擇? 有些要求可以透過提供服務來更容易滿足; 其他一些則需要內部控制。

技術資源決定可行性。 團隊是否有足夠的資源來實現和維護簽名功能,還是說這會分散他們對核心開發重點的注意力?


後續步驟

將數位簽章整合到 .NET 應用程式中意味著既要了解加密基礎知識,又要了解驅動業務文件處理的實際需求。 IronPDF基於憑證的簽章視覺化簽章渲染、多方工作流程支援和簽章驗證提供了技術基礎,同時將何時以及如何使用這些功能的架構決策留給了了解其特定需求的開發人員。

本指南涵蓋的功能為可靠的簽署實施提供了基礎,無論您是自動化合約執行、實施批准鏈還是滿足合規性要求。 對於需要硬體支援的金鑰儲存的企業環境, 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 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 17,386,124 | 版本: 2026.2 剛剛發布