跳至頁尾內容
產品對比

PDFsharp 將 PDF 文件進行數位簽章與 IronPDF 比較(程式碼範例)

數位簽名是一種數學技術,用於驗證電子文件的真實性和完整性。 它可作為電子簽名,在多個司法管轄區對文件進行數位簽名,確保高度安全性和法律效力。

數位簽章是使用私鑰創建的,只有簽署者才知道該私鑰。 該私鑰用於在簽署文件時建立一個與該文件關聯的唯一數位簽章。 簽名包含簽名者的姓名、電子郵件地址和其他個人資訊。 為了確認數位簽章文件的真實性,接收者需要存取簽署者的公鑰。 使用公鑰解密簽名,即可驗證簽名的合法性。

在本教學中,我們將比較如何使用PDFSharpIronPDF為 PDF 文件添加數位簽章。 數位簽章對於驗證文件的真實性至關重要,而 PDF 文件是此類操作的常用格式。

PDFsharp 是一個知名的開源 PDF 創建和操作庫,而 IronPDF 是一個功能強大的 .NET PDF 庫,提供類似的功能以及額外的高級功能。

本指南涵蓋使用私鑰對 PDF 文件進行簽名和驗證簽名,以及兩個函式庫的範例原始碼。

為什麼數位簽章很重要?

數位簽章可確保文件完整性並提供強大的安全性。 它們通常用於合約、協議和其他法律文件。

主要優勢:

  • 比傳統簽章更安全、更防竄改。
  • 以電子方式驗證,減少了人工驗證工作量。
  • 啟用全球遠端文件簽章功能。
  • 比傳統簽名更具保障性。

PDFsharp 概述

PDFSharp是一個開源的 C# 函式庫,主要用於建立和操作 PDF 文件。 它廣泛用於基本的 PDF 任務,例如產生簡單的 PDF 文件、編輯現有文件和渲染圖形。 然而,它對數位簽章等高級功能的原生支援有限,開發人員通常需要依賴第三方程式庫(例如 BouncyCastle)來整合這些功能。 PDFsharp 是開源軟體,採用 MIT 許可證,因此對於成本和靈活性是優先考慮因素的專案來說,它是一個不錯的選擇。

主要特點

  • 根據 MIT 許可證開源且免費。
  • 基本的PDF創建和操作。
  • 可透過 BouncyCastle 等外部函式庫進行擴展,實現數位簽章功能。
  • 缺乏對進階 PDF 功能(例如 HTML 到 PDF 轉換和複雜表單處理)的開箱即用支援。

IronPDF概述

PDFsharp 對 PDF 文件進行數位簽章比較 IronPDF(程式碼範例):圖 1 - image.png

IronPDF是一個強大的 .NET PDF 庫,它提供了一個簡單而強大的 API,用於產生、編輯和操作 PDF。 其突出特點之一是開發人員可以輕鬆實現數位簽名,這對於驗證文件的真實性至關重要。 除了數位簽章外,IronPDF 還支援HTML 轉 PDF 、浮水印和表單處理等進階功能。 對於從事商業專案的開發人員來說,這尤其有價值,因為快速實施和強大的功能是優先考慮的。

主要特點

  • 提供商業許可及付費支持,並提供免費試用。
  • 易於使用的 API,具備用於數位簽章和文件處理的現代化功能。
  • 內建支援 HTML 到 PDF 格式轉換、表單處理PDF 註解(例如附件註解)。
  • 與時間戳、視覺簽名影像和加密等高級功能無縫整合。

使用 PDFsharp 以程式設計方式新增數位簽名

PDFsharp 是一個開源程式庫,專為在 C# 中建立和操作 PDF 而設計。 不過,雖然它確實提供了添加簽名的支持,但您需要整合像 BouncyCastle 這樣的第三方工具,以確保對 PDF 文件進行安全、準確的數位簽章。

使用 PDFsharp 新增數位簽章的步驟

1.透過 NuGet 安裝 PDFsharp 和 BouncyCastle 。 2.使用 X509Certificate2 建立數位憑證。 3.使用 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);
    }
}
$vbLabelText   $csharpLabel

輸出

PDFsharp 將 PDF 文件進行數位簽章與 IronPDF 比較(程式碼範例):圖 2

正如您在這裡看到的,雖然它能夠創建數位簽名域並將證書應用於我們的新文檔,但與 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");
    }
}
$vbLabelText   $csharpLabel

輸出

PDFsharp 將 PDF 文件進行數位簽章與 IronPDF 比較(程式碼範例):圖 3

這段程式碼示範如何使用 IronPDF 的 PdfSignature 類別對 PDF 文件進行簽名。 程式首先建立一個 PdfSignature 對象,指定 .pfx 憑證檔案的位置及其密碼。 然後,它會設定其他簽章屬性,例如雜湊演算法(SHA256)、時間戳記 URL 和簽署的自訂圖像(IronPdf.png)。

最後,呼叫 SignPdfFile 方法將數位簽章套用至 PDF 文件並將其儲存為 output.pdf。 該過程透過嵌入數位簽名、時間戳記和圖像來確保 PDF 的完整性和真實性。

PDFSharp:

  • 根據 MIT 許可證開源。
  • 需要外部程式庫(例如 BouncyCastle)才能實現數位簽章等進階功能。

IronPDF:

*商業許可,定價基於開發者數量和部署實例數。

結論:IronPDF 與 PDFsharp 在 C# 數位簽章的比較

在比較IronPDFPDFsharp這兩個用於在 C# 中向 PDF 添加數位簽章的庫時,根據您的專案需求,這兩個庫都具有明顯的優勢。

  • IronPDF非常適合開發人員,無論是獨立自由軟體開發人員,還是在公司工作的開發人員,它都能提供簡單易用的 API,用於將數位簽名應用於 PDF,並且具有現代化的功能。 它與數位簽章應用程式、 HTML 轉 PDF以及其他 PDF 功能的無縫集成,使其成為注重易用性和快速實施的商業專案的絕佳選擇。 IronPDF 提供付費支援和清晰的商業許可結構,非常適合需要簡單、可靠解決方案的企業。

  • PDFsharp在基本的 PDF 建立和操作方面表現出色,但缺乏 IronPDF 提供的高級功能和對數位簽章的直接支援。 雖然 PDFsharp 是開源且免費使用的,但與 IronPDF 相比,其 API 在處理數位簽章方面不夠直觀,開發人員可能需要採用額外的解決方案或第三方程式庫來處理這些功能。

總而言之,對於尋求簡單、快速的數位簽章及相關 PDF 任務解決方案的開發人員來說, IronPDF是最佳選擇,尤其是在商業環境中。 PDFsharp更適合基本的 PDF 任務,但在數位簽章方面缺乏相同的易用性和功能集,因此更適合簡單的項目或有額外自訂需求的項目。

請注意PDFsharp 和 BouncyCastle 是其各自所有者的註冊商標。 本網站與 PDFsharp 或 BouncyCastle 沒有任何關聯,也未獲得其認可或贊助。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 文中比較僅供參考,反映的是撰寫本文時可公開取得的資訊。

常見問題解答

數位簽章如何確保PDF文件的真實性和完整性?

數位簽章利用加密技術來驗證PDF文件在簽名後是否被竄改。它確認簽名者的身份,並確保文件內容保持不變,從而提供安全性和法律效力。

開發者在使用 PDFsharp 添加數位簽章時可能會面臨哪些挑戰?

使用 PDFsharp 新增數位簽章時,開發人員通常需要整合 BouncyCastle 等第三方函式庫。這會使實作過程變得複雜,因為開發人員需要管理額外的依賴項才能建立數位憑證並對 PDF 進行簽署。

為什麼開發人員可能會選擇商業 PDF 庫而不是開源 PDF 庫來進行數位簽章?

商業PDF庫提供進階功能、內建數位簽章支援和使用者友善的API。這些特性使其更適合尋求快速、可靠且有專門支援的解決方案的開發人員,尤其是在時間和效率至關重要的商業專案中。

如何在.NET應用程式中將HTML轉換為PDF?

在 .NET 應用程式中,可以使用 IronPDF 等庫將 HTML 轉換為 PDF,該庫提供RenderHtmlAsPdf等方法,可以直接將 HTML 字串或文件轉換為 PDF 文檔,並支援各種樣式和腳本選項。

在商業專案中,使用 IronPDF 處理 PDF 檔案有哪些好處?

IronPDF 提供一套全面的 PDF 處理功能,包括數位簽章、HTML 轉 PDF 和表單處理。其強大的支援和用戶友好的 API 使其成為注重高級功能和快速部署的商業專案的理想之選。

能否使用 .NET 函式庫為 PDF 數位簽章新增時間戳記?

是的,使用 IronPDF 等函式庫,您可以透過在簽章配置中指定時間戳記伺服器 URL,為 PDF 數位簽章新增時間戳記。此功能可增強已簽署文件的可信度和法律合規性。

在選擇用於數位簽章實現的 PDF 庫時應考慮哪些因素?

在選擇用於數位簽章的 PDF 庫時,請考慮易用性、對高級功能的支援、與第三方工具的整合能力以及是否提供專門的客戶支援等因素,這些因素在 IronPDF 等商業庫中通常更為全面。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

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