產品比較 PDFsharp數位簽名文檔與IronPDF(代碼示例) Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 數位簽章是一種數學技術,用於驗證電子文件的真實性和完整性。 它作為電子簽章,用於在多個地區對文件進行數位簽名,以確保高度安全性和法律有效性。 數位簽章是使用僅簽署者知道的私鑰來創建的。 當文件被簽名時,這個私鑰產生了一個與該文件相連的唯一數位簽章。 簽章包括簽署者的姓名、電子郵件地址和其他個人信息。 為了確認數位簽名文件的真實性,接收者需要訪問簽署者的公鑰。 簽章的合法性通過使用公鑰解密來驗證。 In this tutorial, we compare how to add digital signatures to PDF documents using PDFSharp and IronPDF. 數位簽章對於驗證文件的真實性是必要的,而PDF文件是此類操作的一種流行格式。 PDFsharp是一個知名的開源PDF創建和操作庫,而IronPDF是一個強大的.NET PDF庫,提供類似的功能及額外的高級功能。 本指南涵蓋了使用私鑰簽署PDF文件和驗證簽章的過程,並提供兩個庫的示例源代碼。 為什麼數位簽章很重要? 數位簽章確保文件的完整性並提供強大的安全性。 它們常用於合同、協議和其他法律文件。 關鍵好處: 比傳統簽章更安全且防篡改。 電子驗證,減少手動驗證工作。 支持全球遠程簽署文件。 提供比傳統簽章更高的保證。 PDFsharp 概述 PDFSharp是一個主要用於PDF文件創建和操作的開源C#庫。 它廣泛用於基本的PDF任務,例如生成簡單的PDF文件、編輯現有文件以及渲染圖形。 然而,它對數位簽章等高級功能的原生支持有限,開發者通常需要依賴第三方庫如BouncyCastle來集成這些功能。 PDFsharp是開源的,基於MIT許可證,這使其成為在成本和靈活性優先的項目中的不錯選擇。 主要功能 開源並基於MIT許可證免費使用。 基本的PDF創建和操作。 可以通過外部庫如BouncyCastle擴展以支持數位簽章。 缺乏對HTML轉PDF轉換和複雜表單處理等高級PDF功能的即時支持。 IronPDF概覽 IronPDF是一個強大的.NET PDF庫,提供簡單而強大的API用於生成、編輯和操作PDF。 其中一個突出特點是開發者可以輕鬆實施數位簽章,這對於驗證文件真實性至關重要。 除了數位簽章,IronPDF還支持高級功能,如HTML轉PDF轉換、水印和表單處理。 它對於致力於商業項目的開發者尤其有價值,這些項目優先考慮快速實施和強大的功能。 主要功能 商業許可證提供付費支持,並且提供免費試用。 易於使用的API,具有現代功能以進行數位簽名和文件操作。 Includes built-in support for HTML-to-PDF format conversion, form handling, and PDF annotations (such as attachment annotations). 與時間戳、視覺簽章圖像和加密等高級功能的無縫集成。 使用PDFsharp以程式方式添加數位簽章 PDFsharp是一個用於C#的開源PDF創建和操作庫。 然而,儘管它提供了添加簽章的支持,但您需要整合第三方工具如BouncyCastle來確保PDF文件的安全、精確的數位簽名。 使用PDFsharp添加數位簽章的步驟 通過NuGet安裝PDFsharp和BouncyCastle。 使用X509Certificate2創建數位證書。 使用BouncyCastle對PDF進行簽名。 示例代碼 using System; using System.IO; using System.Security.Cryptography.X509Certificates; using PdfSharp.Drawing; using PdfSharp.Pdf; using BouncyCastleSigner; // Hypothetical namespace for illustration // Ensure that you have the appropriate namespaces added class Program { static void Main(string[] args) { // Create a font for the appearance of the signature var font = new XFont("Verdana", 10.0, XFontStyle.Regular); // Create a new PDF document var document = new PdfDocument(); var pdfPage = document.AddPage(); // Prepare graphics for drawing on the PDF page var xGraphics = XGraphics.FromPdfPage(pdfPage); var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point); // Add some text to the page xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter); // Define digital signature appearance options var options = new DigitalSignatureOptions { ContactInfo = "John Doe", Location = "Seattle", Reason = "License Agreement", Rectangle = new XRect(36.0, 700.0, 400.0, 50.0), AppearanceHandler = new SignatureAppearanceHandler() }; // Sign the document using BouncyCastle signer var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document, new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options); // Save the signed document document.Save("PdfSharpSignature.pdf"); } static (X509Certificate2, X509Certificate2Collection) GetCertificate() { // Locate the certificate file and read its data var certFolder = "C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security"; var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx"); var rawData = File.ReadAllBytes(pfxFile); // Load the certificate using its password (example password) var certificatePassword = "Passw0rd"; var certificate = new X509Certificate2(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); // Create and return the certificate collection var collection = new X509Certificate2Collection(); collection.Import(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); return (certificate, collection); } } using System; using System.IO; using System.Security.Cryptography.X509Certificates; using PdfSharp.Drawing; using PdfSharp.Pdf; using BouncyCastleSigner; // Hypothetical namespace for illustration // Ensure that you have the appropriate namespaces added class Program { static void Main(string[] args) { // Create a font for the appearance of the signature var font = new XFont("Verdana", 10.0, XFontStyle.Regular); // Create a new PDF document var document = new PdfDocument(); var pdfPage = document.AddPage(); // Prepare graphics for drawing on the PDF page var xGraphics = XGraphics.FromPdfPage(pdfPage); var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point); // Add some text to the page xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter); // Define digital signature appearance options var options = new DigitalSignatureOptions { ContactInfo = "John Doe", Location = "Seattle", Reason = "License Agreement", Rectangle = new XRect(36.0, 700.0, 400.0, 50.0), AppearanceHandler = new SignatureAppearanceHandler() }; // Sign the document using BouncyCastle signer var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document, new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options); // Save the signed document document.Save("PdfSharpSignature.pdf"); } static (X509Certificate2, X509Certificate2Collection) GetCertificate() { // Locate the certificate file and read its data var certFolder = "C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security"; var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx"); var rawData = File.ReadAllBytes(pfxFile); // Load the certificate using its password (example password) var certificatePassword = "Passw0rd"; var certificate = new X509Certificate2(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); // Create and return the certificate collection var collection = new X509Certificate2Collection(); collection.Import(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); return (certificate, collection); } } Imports System Imports System.IO Imports System.Security.Cryptography.X509Certificates Imports PdfSharp.Drawing Imports PdfSharp.Pdf Imports BouncyCastleSigner ' Hypothetical namespace for illustration ' Ensure that you have the appropriate namespaces added Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a font for the appearance of the signature Dim font = New XFont("Verdana", 10.0, XFontStyle.Regular) ' Create a new PDF document Dim document = New PdfDocument() Dim pdfPage = document.AddPage() ' Prepare graphics for drawing on the PDF page Dim xGraphics = XGraphics.FromPdfPage(pdfPage) Dim layoutRectangle = New XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point) ' Add some text to the page xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter) ' Define digital signature appearance options Dim options = New DigitalSignatureOptions With { .ContactInfo = "John Doe", .Location = "Seattle", .Reason = "License Agreement", .Rectangle = New XRect(36.0, 700.0, 400.0, 50.0), .AppearanceHandler = New SignatureAppearanceHandler() } ' Sign the document using BouncyCastle signer Dim pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document, New PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options) ' Save the signed document document.Save("PdfSharpSignature.pdf") End Sub Private Shared Function GetCertificate() As (X509Certificate2, X509Certificate2Collection) ' Locate the certificate file and read its data Dim certFolder = "C:\Users\kyess\AppData\Roaming\Adobe\Acrobat\DC\Security" Dim pfxFile = Path.Combine(certFolder, "IronSoftware.pfx") Dim rawData = File.ReadAllBytes(pfxFile) ' Load the certificate using its password (example password) Dim certificatePassword = "Passw0rd" Dim certificate = New X509Certificate2(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet Or X509KeyStorageFlags.Exportable) ' Create and return the certificate collection Dim collection = New X509Certificate2Collection() collection.Import(rawData, certificatePassword, X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet Or X509KeyStorageFlags.Exportable) Return (certificate, collection) End Function End Class $vbLabelText $csharpLabel 輸出 如您在這裡所見,雖然它能夠創建數位簽章欄位並將證書應用到我們的新文件,但與IronPDF等庫相比,該過程繁瑣、手動,且實施效率不高。 使用IronPDF添加數位簽章 IronPDF為開發者提供了一種簡潔的方法來數位簽署PDF文件。 using IronPdf; using System.Security.Cryptography.X509Certificates; public class Program { static void Main(string[] args) { // Load the certificate with its password var sig = new PdfSignature("IronSoftware.pfx", "your-password"); // Configure additional signature details sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256; sig.TimestampUrl = "http://timestamp.digicert.com"; sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200)); // Sign and save the PDF document sig.SignPdfFile("output.pdf"); } } using IronPdf; using System.Security.Cryptography.X509Certificates; public class Program { static void Main(string[] args) { // Load the certificate with its password var sig = new PdfSignature("IronSoftware.pfx", "your-password"); // Configure additional signature details sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256; sig.TimestampUrl = "http://timestamp.digicert.com"; sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200)); // Sign and save the PDF document sig.SignPdfFile("output.pdf"); } } Imports IronPdf Imports System.Security.Cryptography.X509Certificates Public Class Program Shared Sub Main(ByVal args() As String) ' Load the certificate with its password Dim sig = New PdfSignature("IronSoftware.pfx", "your-password") ' Configure additional signature details sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256 sig.TimestampUrl = "http://timestamp.digicert.com" sig.SignatureImage = New PdfSignatureImage("IronPdf.png", 0, New Rectangle(150, 100, 200, 200)) ' Sign and save the PDF document sig.SignPdfFile("output.pdf") End Sub End Class $vbLabelText $csharpLabel 輸出 此代碼展示了如何使用IronPDF的PdfSignature類簽署PDF文件。 程序首先創建一個PdfSignature對象,指定一個.pfx證書文件的位置及其密碼。 然後,它設置其他簽名屬性,例如雜湊算法(SHA256)、時間戳URL和自定義簽名圖像(IronPdf.png)。 最後,調用SignPdfFile方法將數位簽章應用到PDF文件,並將其保存為output.pdf。 該過程通過嵌入數位簽章以及時間戳和視覺圖像來確保PDF的完整性和真實性。 PDFSharp: 基於MIT許可證的開源。 需要外部庫(如BouncyCastle)來支持數位簽名等高級功能。 IronPDF: 商業許可證根據開發者和部署實例的數量定價。 免費試用可用 免費供開發使用 結論:IronPDF vs PDFsharp 在 C# 中的數位簽章 當比較IronPDF和PDFsharp在C#中向PDF添加數位簽章時,這兩個庫根據您的項目需求提供了不同的優勢。 IronPDF非常適合尋求快速簡單API來將數位簽章應用於PDF的開發者,無論是獨立工作地自由軟體開發者,還是為公司工作的開發者,它都是由於其易於使用的API和現代功能而備受青睞。 其與數位簽章應用、HTML轉PDF轉換以及其他PDF功能的無縫集成使其成為商業項目優先考慮易用性和快速實施的理想選擇。 有付費支持且結構清晰的商業許可證,使得IronPDF非常適合需要直接可靠解決方案的業務。 PDFsharp在基本PDF創建和操作中表現出色,但缺乏IronPDF提供的高級功能和直接支持數位簽名。 雖然PDFsharp是開源且免費使用的,但與IronPDF相比,API在處理數位簽章時不太直觀,開發者可能需要使用附加解決方案或第三方庫來處理這些功能。 總結來說,IronPDF是尋求簡單快速的數位簽章和相關PDF任務解決方案的開發者的最佳選擇,尤其是在商業環境中。 PDFsharp更適合於基本的PDF任務,但在數位簽章的易用性和功能集方面與IronPDF相比欠佳,因此更適合於需要額外定制化的簡單項目。 [{i:(PDFsharp和BouncyCastle是其各自所有者的註冊商標。 本網站與PDFsharp或BouncyCastle沒有關聯,也未經其認可或贊助。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。)}] 常見問題解答 數位簽名如何確保 PDF 文件的真實性和完整性? 數位簽名使用加密技術來驗證 PDF 文件在簽署後是否未被更改。它們確認簽署者的身份並確保文件的內容保持不變,提供了安全性和法律效力。 使用 PDFsharp 添加數位簽名時,開發者可能面臨哪些挑戰? 使用 PDFsharp 添加數位簽名時,開發者通常需要整合像 BouncyCastle 這樣的第三方庫。這一要求可能會使實施過程變得複雜,因為開發者需要管理額外的依賴性來創建數字證書並簽署 PDF。 為什麼開發者可能會選擇商業 PDF 庫而不是開源的進行數位簽名? 商業 PDF 庫提供了高級功能、內建的數位簽名支持和使用者友好的 API。這些特性使其更適合尋求快速、可靠解決方案的開發者,尤其是在時間和效率至關重要的商業專案中。 如何在 .NET 應用程式中將 HTML 轉換成 PDF? 可以使用像 IronPDF 這樣的庫在 .NET 應用程式中將 HTML 轉換成 PDF,通過RenderHtmlAsPdf等方法直接將 HTML 字符串或檔案轉換為 PDF 文件,支持各種樣式和程式選項。 在商業專案中使用 IronPDF 處理 PDF 有哪些好處? IronPDF 提供了一整套用於處理 PDF 的功能,包括數位簽名、HTML 到 PDF 轉換和表單處理。其強大的支持和使用者友好的 API 使其成為優先考慮高級功能和快速部署的商業專案的理想選擇。 可以使用 .NET 庫將時間戳添加到 PDF 數位簽名嗎? 是的,使用像 IronPDF 這樣的庫,可以在簽名配置中指定時間戳伺服器 URL 來將時間戳添加到 PDF 數位簽名中。此功能提高了簽署文件的可信度和法律合規性。 在選擇用於數位簽名實現的 PDF 庫時應考慮哪些因素? 選擇數位簽名的 PDF 庫時,考慮使用方便性、支持高級功能、與第三方工具的整合能力,以及專門的客戶支持的可用性,這些在像 IronPDF 這樣的商業庫中通常範圍更廣。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多 發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多 IronPDF與PDFsharp的PDF到圖像轉換(代碼示例)PDFsharp從PDF中提取文本VS Iro...
發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多
發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多
發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多