Sign PDF Programmatically in C# .NET 10: 數位簽章 Guide
數位簽章在C# .NET中使用X.509 憑證加密技術驗證 PDF 文件,提供篡改檢測、不可否認性以及長期有效性,而這是單純的視覺簽章圖像無法提供的。 IronPDF 使 .NET 開發人員能夠便捷地以程式方式簽署、驗證及保護 PDF,涵蓋從基本的憑證簽署及視覺簽章呈現到多方批准工作流、時間戳伺服器整合及合規管理的所有內容,符合 eIDAS 及 ESIGN 法案 等框架。
重點摘要:快速入門指南本教程涵蓋如何透過 X.509 憑證於 C# 中以程式方式簽署、驗證及保護 PDF 文件,從單一簽章到多方批准鏈。
- **適用物件:**將文件簽署功能內嵌於合約管理、發票處理或合規系統中的 .NET 開發人員。
- **您將學會的內容:**憑證基礎的PDF 簽署 (
.p12)、視覺簽章疊加、多方序列工作流、簽章驗證、篡改檢測及附有權限控制的文件鎖定。 - 運行環境:.NET 10, .NET 8 LTS, .NET Framework 4.6.2+ 和 .NET Standard 2.0。
- **何時使用此方法:**當您需要在大規模下以程式方式簽署 PDF而不需透過第三方簽署入口的時候。
- **技術上的重要性:**密碼學簽章提供篡改證據、不可否認性及長期有效性,而這是視覺簽章圖像無法提供的。
若要按照本教程中的程式碼範例進行操作,請查看快速安裝指南以將 IronPDF 設置於您的專案中。 僅需幾行程式碼,即可簽署您的第一份 PDF:
-
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
-
2Copy and run this code snippet.
var signature = new IronPdf.Signing.PdfSignature("certificate.pfx", "password"); IronPdf.PdfDocument.FromFile("document.pdf").Sign(signature).SaveAs("signed.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
下載完整專案
要快速開始,請下載本教程中所有程式碼範例的完整專案。
下載內容包括一個完全配置的 .NET 專案,附有範例憑證及所有簽章範例,準備好即可構建和運行。
在您購買或註冊IronPDF的30天試用後,請在應用程式的開頭新增您的授權金鑰。
IronPdf.License.LicenseKey = "KEY";Imports IronPdf
IronPdf.License.LicenseKey = "KEY"Start using IronPDF in your project today with a free trial.
PM > Install-Package IronPdf
- 總結:快速入門指南
- 數位簽章 vs 視覺簽章
- 設定數位簽章基礎設施
- 以程式方式應用數位簽章
- 新增視覺簽章外觀
- 多方簽署工作流程
- PDF 簽章的合規管理
- 建置 vs 購買:自訂簽署還是外部服務
數位簽章和視覺簽章在 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 標誌。 這讓加密子系統能夠存取私鑰以進行簽署。 以下是如何正確載入憑證:
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
)範例輸出:
憑證成功載入
主題:CN=測試簽署方, O=測試組織, C=US
發行者:CN=測試簽署方, O=測試組織, 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 處理所有背後的加密操作:
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")輸入
輸出
這產生一個嵌入了看不見的數位簽章的 PDF。 在 Adobe Acrobat 或其他支持簽章的 PDF 閱讀器中打開時,該文件顯示簽章驗證資訊,以顯示簽章是否合法以及文件自簽署以來是否被修改。
對於只需要對文件簽署而不需載入至記憶體進行其他更改的情境,IronPDF 提供了簡化的單行方法:
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")這特別適用於批量處理,當簽署大量文件而不進行其他更改時。
應如何配置簽章中繼資料以用於審核追踪?
數位簽章可以攜帶提供有關簽署事件背景資訊的中繼資料。 此資訊顯示在 PDF 閱讀器的簽章面板中,並增強文件的審核追踪。 IronPDF 支持多個標準中繼資料欄位:
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")輸入
輸出
中繼資料欄位在文件工作流程中具有不同的用途:
| 中繼資料欄位 | 用途 | 範例值 |
|---|---|---|
簽署原因 | 說明文件簽署的原因 | "合同批准"、"審計認證" |
簽署地點 | 記錄簽署發生的地點 | "紐約辦公室"、"遠程家庭辦公室" |
簽署聯絡人 | 提供查詢的聯絡資訊 | "legal@company.com"、"+1 555 0123" |
簽章日期 | 為簽署事件加上時間戳 | DateTime.UtcNow |
在企業設置中,這些欄位通常會連接到操作資料。 例如,發票批准系統可能將簽署原因設置為購買訂單號,而合同管理系統則可能包括合同 ID 和批准階段。
時間戳伺服器在長期上的簽章有效性中扮演什麼角色?
數位簽章包括一個顯示文件簽署時間的時間戳,但此時間戳來自簽署計算機的本地時鐘。 對於需要在幾年後驗證或法律上需要證明確切簽署時間的簽章,一個來自外部時間戳授權機構(TSA)的受信任時間戳能提供更強的證據。
時間戳伺服器提供加密證明,表明文件在具體時刻以其當前形式存在。 即便簽署憑證後來失效或被撤銷,時間戳顯示簽章在套用時的有效性。 IronPDF 支持 RFC 3161 時間戳伺服器:
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")輸入
輸出
有幾個公用的時間戳伺服器可供一般使用,其中包括由主要憑證授權機構運營的時間戳伺服器。 大型組織也可能運營自己的內部時間戳伺服器,以遵循公司安全政策。
哈希算法的選擇對於擴展有效性很重要。 SHA 256 是目前的標準,儘管 IronPDF 也支持更為嚴格的安全要求的 SHA 512。 應避免使用較舊的算法如 SHA 1,因為它們不再被認為是密碼學上安全的。
什麼時候簽章應在文件中不可見還是可見?
數位簽章可以在兩種模式下應用:不可見的簽章即只存在於 PDF 的加密結構中,或可見的簽章即在指定位頁上顯示圖形表示。 選擇取決於文件的目的和接收者的期望。
不可見的簽章適用於不預期人工審查的自動化文件處理、無需更改現有文件佈局的情境,以及可見簽章會使文件雜亂的多簽名工作流。
可見簽章為接收者期望看到視覺簽署證據的情境首選,如工作流程需要將簽章放置在特定位置或者文件會被列印且簽章應顯示在紙上。
下一節詳細討論了可視簽章的實作。
如何為數位簽署的 PDF 新增視覺簽章外觀?
許多業務過程圍繞著可視簽章塊展開,接收者通常期望看到文件的簽署位置和時間。 IronPDF 允許您在頁面上同時應用加密保護和顯示視覺簽章,讓您在兩者上均取得最佳效果。
如何載入及定位簽章圖像?
視覺簽章通常是一個圖像(如掃描的手寫簽名、公司印章或風格化文字塊),放置在 PDF 頁面上的特定位址。 IronPDF 的 LoadSignatureImageFromFile 方法處理定位與呈現:
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")輸出
座標系使用自頁面左下角量取的點(每英寸 1/72 點)。 對於標準美國信紙頁面(612 x 792 點),將簽名放在靠近右下方意味著考量簽名大小及適當的邊距。
存在從不同來源載入簽章圖像的替代方法:
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支援的圖像格式包括 PNG、JPEG、GIF、BMP、TIFF 和 WebP。支持透明的 PNG 檔案在簽名圖像上效果絕佳,因為透明背景語音下的文件內容可見。
簽名在多頁上的定位如何運作?
在簽署多頁面文件時,無論頁數多少,數位簽章都保護整個文件。 單一密碼學簽名保護 PDF 的所有頁面:
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")輸入
輸出
對於多頁面上的簽名(如每頁法律協議上的首字母縮寫),開發者可以獨立使用圖像蓋章,不需要同時套用密碼學簽名:
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")這種方法將視覺元素(可出現在多個頁面上)和整體文件的加密簽章分開來。
多方簽署工作流程在企業應用中的運作如同?
複雜的業務流程通常需要來自不同人的多個簽名,並以特定順序套用。 一張採購訂單可能需部門經理核准,然後財務審查,接著由高管簽署。 IronPDF 通過增量保存和簽名權限支持這些工作流程。
什麼是序列簽署,增量保存如何啟用它?
PDF 文件可以像版本管理一樣在內部保存多個修訂版本。 每次有人簽署,該簽章適用于當時的文件狀態。 後續簽署人將他們的簽章新增至新修訂版本,形成批准鍊,各簽章均可單獨驗證。
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")輸入
輸出
當文件移交至下一位批准者時,他們載入並新增自己的簽章:
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")輸出
每個修訂版本都保留其自身簽章,PDF 閱讀器可以顯示完整的簽章歷史,包括誰在什麼時候簽署。
如何在新增新簽章前驗證現有簽章?
在向文件新增新簽章之前,您的程式碼應驗證現有簽名是否仍然有效。 文件如果在正確的工作流程外被修改,其簽章可能會失效,這可能顯示篡改或程式違規。
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}")更詳細的檢查,您可以獲取有關每個經驗簽章的資訊:
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這種資訊讓您可以建構審計追踪,驗證批准鏈,並確保文件在移至下一階段前擁有所有必要簽名。
IronPDF 如何檢測被篡改的文件?
VerifyPdfSignatures() 方法如果文件中任何簽章無效,將返回 false。 這通常發生在文件簽署後被修改,或當簽章資料本身已損壞,或憑證被撤銷,或憑證在使用時尚未有效之時。
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對於需要移除簽章的應用(可能是為重新發行建立無簽署副本),IronPDF 提供 RemoveSignatures() 方法:
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")請注意,刪除簽章不會恢復任何先前的文件狀態。 此方法只是從當前文件版本中移除簽名資料。
哪些技術能力支持 PDF 簽章的合規管理?
各地區及行業的數位簽章法規不同,但它們特別注重憑證驗證、時間戳及簽章中繼資料等技術需求。 本節聚焦於合規框架普遍要求的技術能力,而不是試圖解釋具體的法律規範(您應與合格的法律顧問討論此事)
數位簽章的主要合規框架是什麼?
下表總結了常見的合規框架及其一般技術要求:
| 框架 | 管轄區域 | 主要技術要求 |
|---|---|---|
| ESIGN 法案 | 美國 | 簽署意圖、同意電子記錄、記錄保存 |
| UETA | 美國各州 | 類似於 ESIGN,適用於州內交易 |
| eIDAS | 歐洲聯盟 | 三種簽署等級(普通、高級、合格);合格需要 QSCD |
| 21 CFR 第 11 部分 | 美國食品藥品管理局(FDA) | 電子簽名必須與電子紀錄相連結,並要求審核追踪 |
這些框架普遍認為正確實施的數位簽章在法律上與手寫簽名等效。 數位簽章的舉證價值常依賴於證明簽章在套用時是有效的,而此時受信任的時間戳對於多年或十年後的文件保存至關重要。
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")輸入
輸出
合規行業適用哪些憑證要求?
不同的法規情境可能對用於簽署的憑證發出標準。 這些標準可能包括由認可的機構發佈的、最低密鑰長度(通常為 2048 位 RSA 或等效)等特定擴展密鑰用法屬性,或者硬體型密鑰儲存使用 HSM 或智能卡解決方案。
IronPDF 可以處理所有符合標準格式規格的 X.509 憑證,讓您可以使用由您的首選憑證授權機構發行的憑證。 對於需要遠端保護的環境,IronPDF 支持以 PKCS#11 基礎的 HSM 整合,其 HsmSigner 功能即是如此。
那些處於受監管行業的應當與他們的法律和合規部門合作確定需要的內容,然後設置符合的簽名基礎設施。
簽章許可控制如何支持文件生命週期管理?
在簽署後,文件有時需要保留可編輯狀態以便於特定用途,如讓額外簽署人新增他們的簽章或填寫表單字段。 IronPDF 的 SignaturePermissions 枚舉可控制簽署後允許的更改:
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")可用的許可級別有:
| 許可等級 | 簽署後允許的更改 |
|---|---|
NoChangesAllowed | 文件完全鎖定 |
FormFillingAllowed | 僅允許填寫表單字段;不允許內容更改 |
AdditionalSignaturesAndFormFillingAllowed | 允許填寫表單和新增簽章 |
FormFillingAndAnnotationsAllowed | 允許填寫表單和註解 |
適當的權限級別取決於文件的角色。 完成的協議可能使用 NoChangesAllowed,而正在進行的批准表單可能使用 AdditionalSignaturesAndFormFillingAllowed 以允許其他簽署者填表和新增簽章。
什麼情況下建置自訂簽名比使用外部服務更合適?
每個實施文件簽署的人面臨一個根本的架構決定:在他們自己的應用程式中整合簽署功能或使用託管的簽署服務。 這兩種方法各有其位置,正確的選擇取決於交易量、客製化需求、合規考慮及擁有權的總成本。
哪些因素促成內部簽署功能的建立?
以下是幾個情境,它們使在應用程式內直接建置簽名功能變得理所當然:
高文件量讓每次交易價格在規模上變得貴得可怕。 每月處理成千上萬文件的企業通常會發現像 IronPDF 這樣的永久授權函提供的財務效益比每次簽署的 API 收費更好。 一次性的許可投資相較於持續的交易費用來說,很快就能回本。
獨特的流程要求會隨著外部簽署服務帶來自己的範德雷恩。 對於具有不尋常批准流程、專門文档處理或難以適應標準方案整合需求的團隊而言,化繁為簡,建置自己的解決方案通常比試著適應服務的限制更容易。
資料主權和安全政策禁止某些公司將文件發送至外部服務,因為法律法規、安全政策、或合同義務的緣故。 保持簽署過程在內部以確保文件始終在公司邊界內。
離線或氣隙環境不能依賴於需要網路連接的託管解決方案。 符號必須在斷開連接的環境中運作(如外勤服務應用、保密系統,或網路不穩定的地方),就其而言,需要實做本地簽署功能。
成本預測很重要,因為基於訂閱的價格模型會創造持續不斷的開銷。 永久性程式庫許可提供穩定的成本,金融部門能夠按需計畫而毋須擔心交易量波動或價格上漲。
哪些情境下偏好使用託管簽署方案?
在以下不同情況下,託管的方案運作良好:
低交易量且多樣的簽署方發生在需要有公司外人士的文件簽署時。 可與身份驗證和證書發行共同使用的服務減少摩擦。 對於偶爾的使用來說,內部構建基礎設施幾乎毫無價值。
快速部署很重要,當您需要快速建立簽署而無法有開發資源從頭開始構建時。 這些平台附帶現成的使用者介面和電子郵件通知,提供即插即用的功能。
合規委託變得有價值,當供應商專注於特定的合規框架並能提供認證或報告獨自獲得時顯貴。
決定往往歸結到簽署是否是一個值得投資核心能力或留給專家的商品功能。 處理文件為其運作核心部份的企業(律所、金融機構、醫療提供者、政府機構)通常通過擁有自己的簽署基礎設施獲得更好的收益。 在文件簽署非核心項目的地方,可能偏好外包選擇的簡便性。
開發團隊如何評估其簽署基礎設施需的?
在承擔任何一種方式前,考慮以下要素:
當前和預期交易量驅動成本分析。 目前需要簽署多少文件,這在未來將如何變化?
整合考慮影響架構設計。 簽署將如何融入既有應用和工作流程中? 程式庫提供最大靈活性,而外部平台則可能需要適應其 API 和使用者介面。
簽署方型別影響複雜性。 文件會僅由內部使用者持有的管理憑證簽署,還是需要容納外部簽名者?
合規稧觀決定技術需求。 什麼法規適用,它們如何影響您的實施選擇? 有些標準更易用服務滿足; 其他則需內部控制。
技術資源決定實施的可行性。 團隊是否有足夠的寬裕來實施和維護簽署功能,還是會影響核心開發優先事項?
下一步
在 .NET 應用中構建數位簽章意味著理解密碼學基礎和驅動業務文件處理的實際需求。 IronPDF 提供了基於憑證的簽署、視覺簽章渲染、多方工作流程支持及 簽章驗證 的技術基礎,而將關於何時以及如何使用這些功能的架構決策留給了解具體需求的開發者。
本指南中涉及的功能提供了堅固的簽署實施基礎,無論是您要自動化合同執行、實施批准鏈還是滿足合規要求。 對於企業環境需要基於硬體的密鑰儲存,HSM 簽名指南將這些模式擴展至 PKCS#11 裝置。 當簽章是更大範圍文件安全策略的一部分時,將其與密碼保護及權限和PDF 加密結合,提供層次防護。
準備好開始構建了嗎? 下載 IronPDF並試用免費試用版。 在做出購買決策前,您可以使用實際文件評估基於憑證的簽署、簽章驗證和多方工作流。 如果您有關於簽署架構或合規整合的問題,請聯繫我們的工程支援團隊。
Frequently Asked Questions
PDFs的數位簽名是什麼?
數位簽名是一種用於驗證PDF文件真實性和完整性的加密機制。它確保文件未被修改,並確認簽署者的身份。
如何使用IronPDF在C#中實現數位簽名?
IronPDF提供了一個簡易的API來在PDFs中使用C#實現數位簽名。您可以使用證書簽署、新增視覺簽名並管理多方工作流程來程式簽署文件。
IronPDF支持哪些型別的數位簽名?
IronPDF支持各種型別的數位簽名,包括基於證書的簽名、視覺簽名以及需要時間戳伺服器進行額外驗證的簽名。
我可以使用IronPDF驗證PDF中的數位簽名嗎?
是的,IronPDF包含驗證PDF文件數位簽名的功能,確保文件真實且自簽署以來未被篡改。
時間戳伺服器在數位簽名中扮演什麼角色?
時間戳伺服器提供了一個可信的時間來源,以確認何時將數位簽名應用於文件。這通過驗證簽署的確切時間,增加了一層額外的安全性。
視覺簽名如何不同於數位證書?
視覺簽名在文件上顯示簽名的圖形表示,然而數位證書提供真實性和完整性的加密證明,未必顯示可視標記。
IronPDF能處理多方簽名工作流程嗎?
是的,IronPDF能夠管理多方簽名工作流程,允許多方協調安全地簽署文件。
在PDF文件中使用數位簽名有什麼好處?
數位簽名通過確保文件完整性和真實性來提高安全性。它們還透過啟用電子簽署來簡化流程,比傳統方法更快捷和有效。
是否可以在無需使用者交互的情況下程式簽署PDFs?
是的,使用IronPDF,您可以在C#中程式簽署PDFs,無需使用者交互,非常適合自動化工作流程和批量處理。

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.





