跳過到頁腳內容
產品比較

QuestPDF數位簽名文檔VS IronPDF(代碼示例)

數位簽章是一種數學算法,用於驗證簽名者的身份並確保文檔的完整性。 它創建了一個與文檔相關聯的唯一標識符,這個標識符使用僅簽名者知道的私鑰簽署。 為了驗證數位簽章文檔的真實性,接收者使用公鑰解密簽名並確認其有效性。

在這篇文章中,我們將比較如何使用 C# 中的 QuestPDF 和 IronPDF 庫將數位簽章添加到 PDF 文檔中。 這兩個庫都提供了強大的 PDF 操作功能,包括數位簽章功能。

為什麼數位簽章很重要?

數位簽章確保文檔內容未被篡改,同時證明簽署者的身份。 與傳統簽名相比,這提供了更高層次的安全性。 數位簽章在許多國家具有法律效力,通常用於合同、協議和其他法律文件。

準備工作

在開始之前,請確保您對 C# 和 .NET Framework 有基本的了解。 You’ll need to install both QuestPDF and IronPDF. QuestPDF 可以從 NuGet 安裝,而 IronPDF 可以從 IronPDF 網站下載或通過 NuGet 包管理器獲得。

您還需要有一個準備好的數位簽章,如果您沒有,網上有許多資源可以幫助您創建新的簽章。

QuestPDF 簽署 PDF 文件與 IronPDF (代碼示例):圖 1

在 C# 中使用 QuestPDF 和 IronPDF 為 PDF 文件添加數位簽章的比較

現在讓我們仔細看看這兩個庫以及它們如何處理將數位簽章應用到 PDF 文檔的任務。

QuestPDF 是一個開源庫,專注於使用流利的 API 生成 PDF 文檔,以創建由於其全面的佈局引擎而具有複雜佈局的文檔。它較不適合 PDF 操作,但可以與其他工具結合使用以簽署 PDF。 它還配備了一個伴侶應用程序,允許用戶輕鬆探索其 PDF 的文檔結構,並利用熱重載功能提供即時文檔預覽,無需重新編譯代碼。

IronPDF 是一個強大的 .NET 庫,提供強大的 PDF 文檔生成、操作和簽署 PDF 文件功能。 它是專為尋求一個全面而易於使用的解決方案的 .NET 開發人員而設計的。 With IronPDF, you can easily encrypt PDF documents, add annotation, convert HTML to PDF, extract content, and more!

QuestPDF

QuestPDF 簽署 PDF 文件與 IronPDF (代碼示例):圖 2

QuestPDF 不原生支持數位簽章。 然而,您可以將其與其他庫(如BouncyCastlePdfSharp)結合使用來實現這一功能。 在使用 QuestPDF 生成文檔後,您可以使用提供 PDF 簽名工具的庫(如 iTextSharp)來簽署文檔,但 QuestPDF 庫本身不處理 PDF 簽名。

它提供其他形式的基本 PDF 安全性,如 PDF 加密,以及高級 PDF 創建和設計 PDF 文檔的能力,因此對於那些尋求能夠處理基本 PDF 任務而無需過多附加功能的 PDF 庫的人來說,這仍然是一個可行的選擇。

IronPDF

IronPDF 提供了一個簡單的 API 用於使用數位證書簽署 PDF。 以下代碼展示了如何使用 IronPDF 將數位簽章應用於 PDF 文件。

using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

public class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

        // Load a certificate from a .pfx file
        X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);

        // Create a PDF signature using the certificate
        var sig = new PdfSignature(cert);

        // Sign the PDF document
        pdf.Sign(sig);

        // Save the signed PDF document
        pdf.SaveAs("signed.pdf");
    }
}
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

public class Program
{
    static void Main(string[] args)
    {
        // Load an existing PDF document
        PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");

        // Load a certificate from a .pfx file
        X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);

        // Create a PDF signature using the certificate
        var sig = new PdfSignature(cert);

        // Sign the PDF document
        pdf.Sign(sig);

        // Save the signed PDF document
        pdf.SaveAs("signed.pdf");
    }
}
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

Public Class Program
	Shared Sub Main(ByVal args() As String)
		' Load an existing PDF document
		Dim pdf As PdfDocument = PdfDocument.FromFile("invoice.pdf")

		' Load a certificate from a .pfx file
		Dim cert As New X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable)

		' Create a PDF signature using the certificate
		Dim sig = New PdfSignature(cert)

		' Sign the PDF document
		pdf.Sign(sig)

		' Save the signed PDF document
		pdf.SaveAs("signed.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

QuestPDF 簽署 PDF 文件與 IronPDF (代碼示例):圖 4

此代碼展示了如何使用 IronPDF 和 X.509 證書對 PDF 文檔進行數位簽署。 首先,它將現有的 PDF (invoice.pdf) 加載到 PdfDocument 對象中。 然後,它通過提供密碼(your-password)和設置標誌X509KeyStorageFlags.Exportable.pfx文件(IronSoftware.pfx)加載證書以允許導出證書的私鑰(如果需要)。

接下來,使用加載的證書創建 PdfSignature 對象。 然後將此簽名應用到 PDF 文檔中,有效地進行了簽署。 最後,簽署的 PDF 另存為名為signed.pdf的新文件。 此過程確保 PDF 被安全簽署,確認其真實性和完整性。

如何使用 IronPDF 驗證簽章

IronPDF 也提供了一個簡便的方法來驗證數位簽章。 您可以調用 VerifyPdfSignatures 方法來檢查文檔中簽章的有效性。

using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("signed_test_document.pdf");

// Verify the digital signatures in the PDF document
bool isValid = pdf.VerifyPdfSignatures();
if (isValid)
{
    Console.WriteLine("The digital signature is valid.");
}
else
{
    Console.WriteLine("The digital signature is invalid or missing.");
}
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("signed_test_document.pdf");

// Verify the digital signatures in the PDF document
bool isValid = pdf.VerifyPdfSignatures();
if (isValid)
{
    Console.WriteLine("The digital signature is valid.");
}
else
{
    Console.WriteLine("The digital signature is invalid or missing.");
}
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("signed_test_document.pdf")

' Verify the digital signatures in the PDF document
Private isValid As Boolean = pdf.VerifyPdfSignatures()
If isValid Then
	Console.WriteLine("The digital signature is valid.")
Else
	Console.WriteLine("The digital signature is invalid or missing.")
End If
$vbLabelText   $csharpLabel

如果文檔中的所有簽章都有效,則此方法返回true,如果任何簽章無效或缺失,則返回false

QuestPDF 和 IronPDF 之間的差異總結

易用性:與 QuestPDF 相比,IronPDF 提供了一個更加簡單的 API 來簽署 PDF。 QuestPDF 不提供數位簽章的原生支持,並需要外部庫(例如 BouncyCastle)來實現此功能。 相比之下,IronPDF 具有對 PDF 簽署和驗證的內建方法,這使得實施數位簽章更加直接。

證書管理:這兩個庫都可以與證書一起使用,但 IronPDF 通過其內建方法(例如 SignWithFile)直接處理證書,簡化了過程。 它還允許您指定簽章權限,這是 QuestPDF 所不具備的。 只需幾行代碼即可使用 IronPDF 簽署 PDF 文件。

數位簽章驗證: IronPDF 提供了一個易於使用的方法(VerifyPdfSignatures)來檢查 PDF 中數位簽章的有效性,而 QuestPDF 缺乏此功能,需要依賴外部庫進行簽章驗證。

許可及成本: QuestPDF 是一個免費使用的開源庫。 這伴隨著缺乏高階功能(如數位簽章支持)的成本。 IronPDF 在開發中免費,此外還提供一系列的商業許可定價,可以先試用再購買。

結論

總之,儘管 QuestPDF 在創建具有複雜佈局的 PDF 方面表現卓越,但缺乏對數位簽章的原生支持,需要外部庫如 BouncyCastle 以實現此功能。 相比之下,IronPDF 提供了集成的數位簽名和驗證解決方案,提供了一個更簡單和更高效的過程。

For developers needing a complete PDF solution with built-in digital signature capabilities, IronPDF is the better choice, with a wide range of features, extensive documentation, and more. 然而,QuestPDF 仍然是 PDF 生成的一個強大的開源選擇,但數位簽名需要額外的複雜性。 決定最終取決於專案的需求和想要的簡潔程度。

請注意QuestPDF 和 BouncyCastle 是其各自所有者的註冊商標。 此網站與 QuestPDF 或 BouncyCastle 無關、未經其認可或贊助。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

我如何使用 C# 向 PDF 添加數位簽章?

要在 C# 中向 PDF 添加數位簽章,您可以使用 IronPDF 的內建方法,通過數位證書簽署文檔。此庫提供了簡單的 API,可以輕鬆地簽署和驗證 PDF 文檔。

使用 IronPDF 進行數位簽章有何優勢?

IronPDF 提供了一個全面的 API,可以在 PDF 中添加和驗證數位簽章,使其使用者友好且高效。它包括證書管理和簽章驗證的內建方法,為開發者提供無縫體驗。

QuestPDF 是否原生支持數位簽章?

QuestPDF 不原生支持數位簽章。要實現此功能,需要整合外部庫,如 BouncyCastle 或 PdfSharp。

IronPDF 如何簡化簽章驗證過程?

IronPDF 提供內建方法,簡化簽章驗證,可以輕鬆檢查 PDF 上的所有簽章是否有效,確保文件的完整性和簽署者的真實性。

我可以使用 QuestPDF 生成帶有數位簽章的 PDF 嗎?

雖然 QuestPDF 在生成具有複雜佈局的 PDF 方面表現優秀,但它缺乏對數位簽章的原生支持。您將需要其他庫來添加和驗證 QuestPDF 生成文檔中的簽章。

與 QuestPDF 相比,IronPDF 處理數位簽章的優勢是什麼?

IronPDF 是處理數位簽章的更好選擇,因為它具有集成的數位簽章功能和使用者友好的 API。它允許輕鬆實現、管理和驗證數位簽章,而不需要外部庫。

IronPDF 在數位簽章實現方面是否免費使用?

IronPDF 免費用於開發目的,使您可以測試其數位簽章功能。對於商業用途,它提供各種許可選項以完全訪問其功能。

IronPDF 如何處理數位證書的管理?

IronPDF 提供管理數位證書的內建方法,簡化了簽署文檔和驗證簽章的過程,這使得確保文檔的完整性和簽署者的真實性變得容易。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。