產品比較 使用Itextsharp在C#中向PDF添加數字簽名 Curtis Chau 更新:2026年2月1日 下載 IronPDF NuGet 下載 DLL 下載 Windows Installer 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 Full Comparison Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing. View Full Comparison 在我們快速變化的世界中,實體文件正迅速被電子文件取代。 無論是簽署合同、批准發票,還是提交政府表格,電子文件已成為標準。 然而,這種便利帶來了一個新的挑戰:如何確保電子文件的真實性和完整性。 電子簽名提供了解決方案。 不僅僅是在觸控屏幕上草草簽字,數位簽名使用密碼技術來驗證簽署人的身份並確保文件內容未被更改。 對於C#開發人員來說,將這種安全性集成到PDF工作流程中變得更加容易,特別是使用像IronPDF和iTextSharp這樣的工具。 在本文中,我們將指導您完成對PDF進行數位簽名的過程,對比程式庫,提供最佳實踐,並幫助您為您的下一個項目選擇合適的解決方案。 了解數位簽名 數位簽名是一種使用密碼技術來驗證數位訊息或文件的真實性和完整性的方法。 與簡單的圖像簽名或鍵入的名稱不同,數位簽名使用私鑰來加密文件的雜湊值。 然後,任何人都可以使用簽署者的公鑰來驗證這個加密的雜湊值。 為什麼這很重要? 因為它能確保兩件事: 身份驗證 – 簽名驗證來自聲明中的發送者的PDF文件。 完整性 – 文件自簽署以來沒有被更改。 即使是一個微小的改變也會使簽名無效。 數位簽名在許多司法管轄區具有法律約束力,並且在金融、健康護理、法律和政府等行業中至關重要。 為什麼要在PDF中使用數位簽名? PDF是一個分發專業文件的標準格式,從法律契約到官方報告。 在PDF中添加數位簽名具有多個關鍵目的: 合法性與合規性:數位簽名符合eIDAS(歐洲)、ESIGN(美國)等法規,獲得法律認可。 安全性:簽署的文件不能被更改,否則會破壞簽名,防止篡改或欺詐。 效率:不需要打印、簽署和掃描。 通過安全的數位批准節省時間並簡化工作流程。 信任:客戶和合作夥伴可以自信地驗證文件的來源和完整性。 簡而言之,數位簽名為您的文件工作流程帶來信任和效率。 比較iTextSharp與IronPDF 在C#中實施數位簽名時,通常會突出的兩個程式庫是iTextSharp和IronPDF。 兩者都是強大的工具,但它們面向不同類型的開發人員和項目需求。 讓我們分析一下它們在實際使用中的比較。 iTextSharp:複雜性中的力量 iTextSharp是PDF操作世界中一個知名名稱。 它是更廣泛iText 7生態系統的一部分,提供豐富的低層次PDF操作支持,包括密碼技術數字簽名。 需要對簽名外觀、雜湊算法、證書鏈和自定義驗證工作流程進行細緻控制的開發人員會發現iTextSharp非常強大。 它具有高度的擴展性,設計考慮了複雜的企業需求。 然而,這種靈活性是有代價的。學習曲線很陡峭。簡單的任務,如添加可見簽名,通常需要多個類別、資料流和配置步驟。 對於新用戶而言,這可能會感到不知所措。 此外,iTextSharp使用AGPL授權,要求您的應用程式開源,除非您購買商業授權——這對於許多封閉源或專有項目來說是一個絕對不可行的選擇。 IronPDF:簡單與專業的結合 IronPDF,相對來說,採用現代、開發者優先的方法。 其API設計旨在處理常見的PDF任務——如數位簽名、生成、合併和編輯——而不需要太多設置,使其成為.NET框架項目的強大PDF庫。 例如,在IronPDF中簽署PDF無需直接處理資料流或密碼設置。 您只需加載PDF,調用SignPdf()並傳入您的證書。 它甚至支持額外的元數據,如簽署人位置、原因和聯繫信息——這些都可以在單一方法調用中完成。 另一個關鍵好處是授權。 IronPDF提供專業友好的商業授權,沒有AGPL限制,這對於專業和企業級應用程式理想。 雖然它是一個付費產品,但豐富的免費試用讓您可以輕鬆評估後再做決定。 對比總結 功能 iTextSharp IronPDF 易用性 學習曲線陡峭 對於初學者友好,代碼簡潔 授權 AGPL(或付費商業授權) 商業授權,沒有開源要求 簽名自定義 具有加密控件的高度可定製化 簡化的API,可選的元數據字段 文件 詳細但密集 清晰的例子和以開發者為中心的文件 最佳使用 具有深度定製的企業應用程式 需要快速實施和支持的團隊 開始使用iTextSharp和IronPDF 在深入數位簽名實施之前,至關重要的是了解如何開始使用每個程式庫。 無論您是正在構建企業級解決方案還是快速的內部工具,正確的設置可以帶來巨大的不同。 設置iTextSharp iTextSharp是基於Java的強大iText PDF庫的.NET移植。 要開始,您需要通過NuGet安裝它並在項目中引用正確的命名空間。 安裝 您可以通過NuGet套件管理器控制台輕鬆將iTextSharp庫安裝到您的項目中。 您只需要運行以下命令: Install-Package iTextSharp 這種簡單的安裝確保在您的C#項目中快速實施此庫。 基本設置 安裝後,您可以在項目中開始使用iTextSharp命名空間: using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; $vbLabelText $csharpLabel 請記住,iTextSharp是模塊化的。 如果您計劃使用高級加密功能或時間戳記,您可能需要像BouncyCastle.Cryptography這樣的附加包。 這可以像安裝iTextSharp一樣安裝,只需運行這一行: Install-Package BouncyCastle.Cryptography 需要注意的事項 授權: AGPL許可要求使用iTextSharp的任何軟體必須開源,除非您購買商業授權。 依賴性:加密操作通常需要使用BouncyCastle進行證書處理。 學習曲線:即使是基本的簽名也涉及了解IExternalSignature以及各種加密服務提供者。 如果您對配置這些基本構建塊感到舒適並需要對簽名過程進行完全控制(例如設置外觀、驗證級別或時間戳伺服器),iTextSharp是個不錯的選擇。 設置IronPDF IronPDF是一個以提高開發者生產力為目標的商業PDF庫。 它專為希望生成、編輯和簽署PDF的.NET開發人員設計,減少麻煩。 IronPDF提供了一個更流暢的上手體驗,特別是對於那些重視清晰API和快速結果的人。 安裝 通過NuGet安裝最新的IronPDF包: Install-Package IronPdf 或者使用.NET CLI: Install-Package IronPdf 基本設置 首先導入主IronPDF命名空間: using IronPdf; using IronPdf; $vbLabelText $csharpLabel 就是這樣——您已準備好加載PDF並開始新增數字簽名。 IronPDF內部管理所有事務:證書加載、可見簽名定位、元數據和最終匯出。 您不必手動管理PDF資料流或密碼算法,這對於快速開發來說是一個巨大的優勢。 初學者的主要優勢 一體化:無需其他依賴或密碼學庫。 無AGPL擔憂:IronPDF提供永久許可和慷慨的免費試用。 視覺渲染:IronPDF渲染PDF時正如它們打印出來時的樣子,非常適合契約和官方文件。 逐步:新增數字簽名 1. 準備您的證書 您將需要一個.pfx數字證書檔案和一個密碼。 這些用於生成數字簽名。 您可以從受信任的證書授權機構(CA)獲取證書,也可以使用OpenSSL等工具生成內部使用的證書。 2. 使用iTextSharp和BouncyCastle簽署PDF 包括必要的命名空間 首先,我們需要確保在代碼的頂部有正確的using語句,以便可以訪問簽署PDF數位內容所需的各種類別和方法。 using System; using System.IO; using System.Linq; using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Pkcs; using System; using System.IO; using System.Linq; using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Pkcs; $vbLabelText $csharpLabel 定義輸入PDF並將其加載到PdfReader中 然後我們指定現有PDF的路徑並將其加載到PdfReader中。 我們還將分配一些字串變量,這些變量將在代碼的後面用到。 // Path to the unsigned PDF you want to sign string filename = "example.pdf"; // Load the existing PDF into a reader PdfReader pdfReader = new PdfReader(filename); string reason = "Digital Signature Reason"; string location = "Digital Signature Location"; // Path to the unsigned PDF you want to sign string filename = "example.pdf"; // Load the existing PDF into a reader PdfReader pdfReader = new PdfReader(filename); string reason = "Digital Signature Reason"; string location = "Digital Signature Location"; $vbLabelText $csharpLabel 定義證書路徑和密碼 接下來,我們指向.pfx證書文件並提供保護它的密碼。 // Path to your .pfx certificate file (must contain private key) string pfxFilePath = "certificate-file.pfx"; // Password for the certificate (make sure to protect this securely!) string pfxPassword = "Password"; // Path to your .pfx certificate file (must contain private key) string pfxFilePath = "certificate-file.pfx"; // Password for the certificate (make sure to protect this securely!) string pfxPassword = "Password"; $vbLabelText $csharpLabel 使用Pkcs12Store加載.PFX證書 我們使用BouncyCastle將證書和私鑰加載到安全存儲中。 // Initialize a new PKCS#12 key store (used for handling the PFX certificate) Pkcs12StoreBuilder pkcs12StoreBuilder = new Pkcs12StoreBuilder(); Pkcs12Store pfxKeyStore = pkcs12StoreBuilder.Build(); // Load the certificate and private key from the PFX file using (FileStream pfxStream = new FileStream(pfxFilePath, FileMode.Open, FileAccess.Read)) { // Load into the key store using the provided password pfxKeyStore.Load(pfxStream, pfxPassword.ToCharArray()); } // Initialize a new PKCS#12 key store (used for handling the PFX certificate) Pkcs12StoreBuilder pkcs12StoreBuilder = new Pkcs12StoreBuilder(); Pkcs12Store pfxKeyStore = pkcs12StoreBuilder.Build(); // Load the certificate and private key from the PFX file using (FileStream pfxStream = new FileStream(pfxFilePath, FileMode.Open, FileAccess.Read)) { // Load into the key store using the provided password pfxKeyStore.Load(pfxStream, pfxPassword.ToCharArray()); } $vbLabelText $csharpLabel 準備PdfStamper以新增簽名 PdfStamper讓我們可以應用數位簽名同時保留原始內容。 // Create a PdfStamper that enables signing and appends the signature to the document PdfStamper pdfStamper = PdfStamper.CreateSignature( pdfReader, new FileStream("MyPDF_Signed.pdf", FileMode.Create), // Output path '\0', // PDF version (unchanged) null, // Temp file path (optional) true // Append mode (preserves original content) ); // Create a PdfStamper that enables signing and appends the signature to the document PdfStamper pdfStamper = PdfStamper.CreateSignature( pdfReader, new FileStream("MyPDF_Signed.pdf", FileMode.Create), // Output path '\0', // PDF version (unchanged) null, // Temp file path (optional) true // Append mode (preserves original content) ); $vbLabelText $csharpLabel 定製簽名出現 現在我們定義簽名在文檔中將如何及在何處視覺上顯示。 // Access the signature appearance settings PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance; // Add optional metadata (shows up in PDF signature details) signatureAppearance.Reason = reason; signatureAppearance.Location = location; // Position the visible signature on the page (x, y, width, height in points) float x = 360; float y = 130; signatureAppearance.Acro6Layers = false; // Use compact signature appearance signatureAppearance.Layer4Text = PdfSignatureAppearance.QuestionMark; // Custom label text signatureAppearance.SetVisibleSignature( new iTextSharp.text.Rectangle(x, y, x + 150, y + 50), // Rectangle position 1, // Page number "signature" // Field name ); // Access the signature appearance settings PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance; // Add optional metadata (shows up in PDF signature details) signatureAppearance.Reason = reason; signatureAppearance.Location = location; // Position the visible signature on the page (x, y, width, height in points) float x = 360; float y = 130; signatureAppearance.Acro6Layers = false; // Use compact signature appearance signatureAppearance.Layer4Text = PdfSignatureAppearance.QuestionMark; // Custom label text signatureAppearance.SetVisibleSignature( new iTextSharp.text.Rectangle(x, y, x + 150, y + 50), // Rectangle position 1, // Page number "signature" // Field name ); $vbLabelText $csharpLabel 提取私鑰並簽署PDF 我們檢索包含私鑰的證書條目的別名(名稱)。 如果別名存在,我們將著手產生並使用SHA-256嵌入數字簽名。 // Find the first alias in the PFX that has a private key entry string alias = pfxKeyStore.Aliases.Cast<string>().FirstOrDefault( entryAlias => pfxKeyStore.IsKeyEntry(entryAlias) ); // Ensure a valid alias (certificate) was found if (alias != null) { // Retrieve the private key for signing ICipherParameters privateKey = pfxKeyStore.GetKey(alias).Key; // Create a signer using SHA-256 and the private key IExternalSignature pks = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256); // Perform the digital signing operation using CMS format MakeSignature.SignDetached( signatureAppearance, // Signature appearance pks, // External signature handler new Org.BouncyCastle.X509.X509Certificate[] { pfxKeyStore.GetCertificate(alias).Certificate }, // Certificate chain (basic single-cert example) null, null, null, // Optional CRL, OCSP, TSA 0, // Estimated size for the signature (0 = auto) CryptoStandard.CMS // Signature standard (CMS vs CAdES) ); } else { Console.WriteLine("Private key not found in the PFX certificate."); } // Find the first alias in the PFX that has a private key entry string alias = pfxKeyStore.Aliases.Cast<string>().FirstOrDefault( entryAlias => pfxKeyStore.IsKeyEntry(entryAlias) ); // Ensure a valid alias (certificate) was found if (alias != null) { // Retrieve the private key for signing ICipherParameters privateKey = pfxKeyStore.GetKey(alias).Key; // Create a signer using SHA-256 and the private key IExternalSignature pks = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256); // Perform the digital signing operation using CMS format MakeSignature.SignDetached( signatureAppearance, // Signature appearance pks, // External signature handler new Org.BouncyCastle.X509.X509Certificate[] { pfxKeyStore.GetCertificate(alias).Certificate }, // Certificate chain (basic single-cert example) null, null, null, // Optional CRL, OCSP, TSA 0, // Estimated size for the signature (0 = auto) CryptoStandard.CMS // Signature standard (CMS vs CAdES) ); } else { Console.WriteLine("Private key not found in the PFX certificate."); } $vbLabelText $csharpLabel 最終化文件 最後,我們關閉stamper以完成簽署過程並將已簽署的PDF寫入磁盤。 // Close the stamper to save and finalize the signed PDF pdfStamper.Close(); // Close the stamper to save and finalize the signed PDF pdfStamper.Close(); $vbLabelText $csharpLabel 輸出 Bootstrap安全配置表單 專業的PDF安全需要直觀的配置介面。 這個Bootstrap 5範例展示了IronPDF如何呈現多步驟安全配置表單,帶有驗證狀態和進度跟踪。 using IronPdf; var renderer = new ChromePdfRenderer(); string securityConfigForm = @" <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'> <style> .step-indicator { display: flex; justify-content: space-between; margin-bottom: 30px; } .step { flex: 1; text-align: center; position: relative; } .step-number { width: 40px; height: 40px; border-radius: 50%; background: #e9ecef; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; } .step.completed .step-number { background: #198754; color: white; } .step.active .step-number { background: #0d6efd; color: white; } @media print { .form-section { page-break-inside: avoid; } } </style> </head> <body class='bg-light'> <div class='container py-4'> <div class='row justify-content-center'> <div class='col-lg-8'> <h2 class='text-center mb-4'>PDF Security Configuration</h2> <div class='step-indicator mb-4'> <div class='step completed'> <div class='step-number'>✓</div> <div class='small mt-2'>Certificate</div> </div> <div class='step completed'> <div class='step-number'>✓</div> <div class='small mt-2'>Signature</div> </div> <div class='step active'> <div class='step-number'>3</div> <div class='small mt-2'>Encryption</div> </div> <div class='step'> <div class='step-number'>4</div> <div class='small mt-2'>Finalize</div> </div> </div> <div class='card shadow-sm form-section'> <div class='card-header bg-primary text-white'> <h5 class='mb-0'>Step 3: Encryption & Permissions</h5> </div> <div class='card-body'> <div class='mb-3'> <label class='form-label'><strong>Encryption Level</strong></label> <select class='form-select'> <option>AES 128-bit</option> <option selected>AES 256-bit (Recommended)</option> <option>RC4 128-bit (Legacy)</option> </select> <small class='text-muted'>AES-256 provides enterprise-grade security</small> </div> <div class='mb-3'> <label class='form-label'><strong>Document Permissions</strong></label> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowPrint' checked> <label class='form-check-label' for='allowPrint'>Allow Printing</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowCopy'> <label class='form-check-label' for='allowCopy'>Allow Content Copying</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowModify'> <label class='form-check-label' for='allowModify'>Allow Document Modification</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowAnnotate' checked> <label class='form-check-label' for='allowAnnotate'>Allow Annotations</label> </div> </div> <div class='mb-3'> <label class='form-label'><strong>Password Protection</strong></label> <input type='password' class='form-control mb-2' placeholder='Owner Password' value='••••••••'> <input type='password' class='form-control' placeholder='User Password (Optional)'> <small class='text-muted'>Owner password controls document permissions</small> </div> <div class='alert alert-success'> <strong>✓ Configuration Valid</strong><br> <small>Security settings meet compliance requirements</small> </div> </div> <div class='card-footer'> <div class='d-flex justify-content-between'> <button class='btn btn-outline-secondary'>← Previous</button> <button class='btn btn-primary'>Continue →</button> </div> </div> </div> <div class='card mt-3 shadow-sm'> <div class='card-body'> <h6 class='text-primary'>Security Comparison</h6> <div class='row g-2'> <div class='col-6'> <div class='text-center p-2 bg-success text-white rounded'> <strong>IronPDF</strong><br> <small>AES-256 Native</small> </div> </div> <div class='col-6'> <div class='text-center p-2 bg-warning text-dark rounded'> <strong>iTextSharp</strong><br> <small>Complex Setup</small> </div> </div> </div> </div> </div> </div> </div> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(securityConfigForm); pdf.SaveAs("security-configuration.pdf"); using IronPdf; var renderer = new ChromePdfRenderer(); string securityConfigForm = @" <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'> <style> .step-indicator { display: flex; justify-content: space-between; margin-bottom: 30px; } .step { flex: 1; text-align: center; position: relative; } .step-number { width: 40px; height: 40px; border-radius: 50%; background: #e9ecef; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; } .step.completed .step-number { background: #198754; color: white; } .step.active .step-number { background: #0d6efd; color: white; } @media print { .form-section { page-break-inside: avoid; } } </style> </head> <body class='bg-light'> <div class='container py-4'> <div class='row justify-content-center'> <div class='col-lg-8'> <h2 class='text-center mb-4'>PDF Security Configuration</h2> <div class='step-indicator mb-4'> <div class='step completed'> <div class='step-number'>✓</div> <div class='small mt-2'>Certificate</div> </div> <div class='step completed'> <div class='step-number'>✓</div> <div class='small mt-2'>Signature</div> </div> <div class='step active'> <div class='step-number'>3</div> <div class='small mt-2'>Encryption</div> </div> <div class='step'> <div class='step-number'>4</div> <div class='small mt-2'>Finalize</div> </div> </div> <div class='card shadow-sm form-section'> <div class='card-header bg-primary text-white'> <h5 class='mb-0'>Step 3: Encryption & Permissions</h5> </div> <div class='card-body'> <div class='mb-3'> <label class='form-label'><strong>Encryption Level</strong></label> <select class='form-select'> <option>AES 128-bit</option> <option selected>AES 256-bit (Recommended)</option> <option>RC4 128-bit (Legacy)</option> </select> <small class='text-muted'>AES-256 provides enterprise-grade security</small> </div> <div class='mb-3'> <label class='form-label'><strong>Document Permissions</strong></label> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowPrint' checked> <label class='form-check-label' for='allowPrint'>Allow Printing</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowCopy'> <label class='form-check-label' for='allowCopy'>Allow Content Copying</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowModify'> <label class='form-check-label' for='allowModify'>Allow Document Modification</label> </div> <div class='form-check form-switch'> <input class='form-check-input' type='checkbox' id='allowAnnotate' checked> <label class='form-check-label' for='allowAnnotate'>Allow Annotations</label> </div> </div> <div class='mb-3'> <label class='form-label'><strong>Password Protection</strong></label> <input type='password' class='form-control mb-2' placeholder='Owner Password' value='••••••••'> <input type='password' class='form-control' placeholder='User Password (Optional)'> <small class='text-muted'>Owner password controls document permissions</small> </div> <div class='alert alert-success'> <strong>✓ Configuration Valid</strong><br> <small>Security settings meet compliance requirements</small> </div> </div> <div class='card-footer'> <div class='d-flex justify-content-between'> <button class='btn btn-outline-secondary'>← Previous</button> <button class='btn btn-primary'>Continue →</button> </div> </div> </div> <div class='card mt-3 shadow-sm'> <div class='card-body'> <h6 class='text-primary'>Security Comparison</h6> <div class='row g-2'> <div class='col-6'> <div class='text-center p-2 bg-success text-white rounded'> <strong>IronPDF</strong><br> <small>AES-256 Native</small> </div> </div> <div class='col-6'> <div class='text-center p-2 bg-warning text-dark rounded'> <strong>iTextSharp</strong><br> <small>Complex Setup</small> </div> </div> </div> </div> </div> </div> </div> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(securityConfigForm); pdf.SaveAs("security-configuration.pdf"); $vbLabelText $csharpLabel 輸出:一個專業的安全配置表單PDF,具有Bootstrap 5步驟指示器、表單控制、開關和驗證警報。 IronPDF以完美的忠實度渲染所有表單樣式、實用程序類和互動元素,展示了優於iTextSharp程式化方法的表單渲染能力。 有關Bootstrap表單支持的更多細節,請參見Bootstrap & Flexbox CSS指南。 3. 使用IronPDF在C#中對PDF進行數字簽名 包括必要的命名空間 我們從導入用於處理PDF簽署、證書處理和圖像定位的必要命名空間開始。 using IronPdf; using IronPdf.Signing; using IronSoftware.Drawing; using System.Security.Cryptography.X509Certificates; using IronPdf; using IronPdf.Signing; using IronSoftware.Drawing; using System.Security.Cryptography.X509Certificates; $vbLabelText $csharpLabel 加載您要簽署的PDF 我們使用IronPDF的簡單PdfDocument API從磁盤加載現有PDF文件。 您也可以創建一個新的PDF文件以執行此任務。 var pdf = PdfDocument.FromFile("example.pdf"); var pdf = PdfDocument.FromFile("example.pdf"); $vbLabelText $csharpLabel 加載用於簽名的PFX證書 我們加載包含私鑰的.pfx證書。 需要使用Exportable標誌以便可以訪問簽名密鑰。 X509Certificate2 cert = new X509Certificate2( "IronSoftware.pfx", "Password", X509KeyStorageFlags.Exportable ); X509Certificate2 cert = new X509Certificate2( "IronSoftware.pfx", "Password", X509KeyStorageFlags.Exportable ); $vbLabelText $csharpLabel 使用證書創建一個新的PdfSignature 我們從加載的證書中創建一個新的PdfSignature對象。 var sig = new PdfSignature(cert); var sig = new PdfSignature(cert); $vbLabelText $csharpLabel 應用簽名並保存輸出 我們對PDF進行數字簽名並將已簽署的PDF文件保存為新文件。 pdf.Sign(sig); pdf.SaveAs("signed.pdf"); pdf.Sign(sig); pdf.SaveAs("signed.pdf"); $vbLabelText $csharpLabel 輸出 4. 代碼說明 IronPDF使簽署過程簡單且易於閱讀。 您加載PDF,提供證書,並調用SignPdf()。 可選的元數據(聯繫方式、位置、原因)增加專業性。 iTextSharp提供更多控制,但需要詳細設置hash算法、資料流和證書鏈。 摘要:使用少量代碼,IronPDF使得使用標準.pfx證書應用數字簽名變得極其簡單,無需低層次密碼技術。 與使用像iTextSharp這樣的程式庫來處理同一任務需要的較長代碼相比,這更容易實施。 5. 真實世界使用案例 法律團隊:自動簽署從模板生成的合同。 金融:數字簽署發票和報告以防止篡改。 政府門戶:在提交之前簽署表單以符合監管標準。 數字簽名最佳實踐 要充分利用您的數字簽署實施: 使用強證書:選擇2048位RSA密鑰或更強的密鑰。 確保私鑰安全:安全存儲證書,最好在硬件安全模塊(HSM)中。 時間戳您的簽名: 添加受信任的時間戳以確保即使證書過期,簽名仍然有效。 驗證簽名:在您的應用程式中添加驗證以檢測篡改或過期的證書。 自動化:在您的部署管道中排程簽署操作以保證文件的一致性和完整性。 結論 在今天安全意識強烈的數字環境中,添加數字簽名到PDF文件已不再是奢侈品——而是必需品。 無論您是在保護合同、發票、報告還是法律文件,具有可信證書支持的防篡改簽名可確保您的文件保持其真實性和完整性。 在本文中,我們探討了在C#中實現PDF簽名的兩種強大方法: iTextSharp,它提供低層次的密碼技術控制和靈活性,但需要更多樣板和對BouncyCastle的熟悉。 IronPDF,它提供現代化的高層API,使應用安全簽名的過程流暢且開發者友好。 這兩個工具都支持安全的.pfx證書,但IronPDF顯然簡化了工作流程——它是那些希望花更少時間處理密碼原語、更多時間交付業務價值的.NET開發人員的理想選擇。 下一步 如果您尚未這樣做,請考慮下載IronPDF的免費試用版,然後嘗試僅使用幾行代碼簽署自己的PDF。 生產力的提升本身就值得切換,特別是在進行時間敏感的項目時。 請注意iTextSharp是其各自擁有者的註冊商標。 本網站未經iTextSharp認可或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,並反映了撰寫本文時的公開信息。)}] 常見問題解答 數位簽章如何確保 PDF 文件的真實性? 數位簽章使用加密技術來驗證簽署者的身份,並確保文件沒有被更改。這是通過使用私鑰加密文件的雜湊來實現的,提供認證和完整性。 為什麼數位簽章對電子文件很重要? 數位簽章對於維護電子文件的合法性、安全性、效率和信任至關重要。它們確保遵守規定、防止篡改、簡化文件工作流程並確認文件的來源和完整性。 如何在 C# 中將數位簽章新增到 PDF? 在 C# 中,您可以使用 IronPDF 來為 PDF 新增數位簽章。該過程包括加載 PDF,通過 X509Certificate2 提供 .pfx 證書,並調用 Sign 方法來應用簽章。 iTextSharp 與另一個 PDF 庫在數位簽章方面的主要區別是什麼? iTextSharp 提供詳細的 PDF 操作的廣泛支持,具有陡峭的學習曲線並需要特定的許可。相比之下,IronPDF 對初學者友好,具有簡化的 API,允許快速實施,且不需要開源許可。 如何為 C# 中的文件簽署安裝 PDF 庫? 您可以通過使用命令 Install-Package IronPDF 或透過 .NET CLI 使用 dotnet add package IronPDF 來安裝 IronPDF。 使用 iTextSharp 要將 PDF 進行數位簽署需要哪些步驟? 要使用 iTextSharp 使用數位簽署 PDF,您需要配置 PdfStamper,自訂簽章外觀,使用 BouncyCastle 加載 .pfx 證書,並利用 PdfSigner 和 IExternalSignature 進行加密操作。 在 PDF 中實行數位簽章的一些最佳實踐是什麼? 最佳實踐包括使用強大的證書、保護私鑰、為簽章加上時間戳、定期驗證簽章以及自動化簽名過程以維持文件的完整性。 數位簽章在 PDF 文件中的一些實際應用是什麼? 數位簽章通常用於法律部門的合同簽署,在金融業的核准發票和報告,及政府機構的處理表單中,以確保符合監管標準。 哪個 PDF 庫更適合需要快速實現數位簽章的開發人員? 對於尋求快速實施數位簽章的開發人員,推薦使用 IronPDF,因為它具有簡單和乾淨的 API,簡化了簽署過程,所需代碼最少。 IronPDF 是否與 .NET 10 相容,可以用來新增和驗證數位簽章? 是 - IronPDF 與 .NET 10 完全相容。它支援 .NET10(以及 .NET9、8、7 等),其數位簽章功能(例如使用 PdfSignature, X509Certificate2, 簽署與驗證)在 .NET 10 runtime 上開箱即用。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新2026年3月1日 在 ASP.NET MVC 中生成 PDF:iTextSharp vs IronPDF 指南 比較在 ASP.NET MVC 中使用 iTextSharp vs IronPDF 的 PDF 產生方法。發現哪個函式庫能提供更好的 HTML 呈現以及更容易的實作。 閱讀更多 更新2026年2月1日 Ghostscript GPL vs IronPDF:技術比較指南 探索 Ghostscript GPL 與 IronPDF 的主要差異。比較 AGPL 授權與商業授權、指令列開關與本機 .NET API,以及 HTML-to-PDF 功能。 閱讀更多 更新2026年3月1日 ASP PDF 庫:比較 IronPDF、Aspose 和 Syncfusion 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 在Windows中運行的Wkhtmltopdf與IronPDF之間的比較IronPDF vs ExpertPDF:資深 .NET ...
更新2026年3月1日 在 ASP.NET MVC 中生成 PDF:iTextSharp vs IronPDF 指南 比較在 ASP.NET MVC 中使用 iTextSharp vs IronPDF 的 PDF 產生方法。發現哪個函式庫能提供更好的 HTML 呈現以及更容易的實作。 閱讀更多
更新2026年2月1日 Ghostscript GPL vs IronPDF:技術比較指南 探索 Ghostscript GPL 與 IronPDF 的主要差異。比較 AGPL 授權與商業授權、指令列開關與本機 .NET API,以及 HTML-to-PDF 功能。 閱讀更多
更新2026年3月1日 ASP PDF 庫:比較 IronPDF、Aspose 和 Syncfusion 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多