產品比較

使用 Itextsharp 在 C# 中為 PDF 添加數字簽名

發佈 2023年4月18日
分享:

簽署PDF文件,包括PDF文件。數位簽名指的是一種用於驗證和確保電子文件完整性的數學公式。

在本教程中,我們將向您展示 如何將數位簽章資訊添加到PDF 在 C# 中使用 iTextSharp和 IronPDF 函式庫。

在進入程式碼之前,讓我們先快速了解一下什麼是數位簽章以及它的重要性。

什麼是數位簽名?

數位簽名是指用來驗證和確保電子文件完整性的數學公式。

它是一種用來數位簽署電子文件的電子簽名。數位簽名提供高度的安全性,且在許多國家具有法律效力。

當文件被數位簽署時,會產生一個與該文件鏈接的獨特數位簽名。

該簽名是使用僅簽署者知道的私鑰創建的。簽名密鑰包含關於簽署者的信息,包括他們的姓名、電子郵件地址和其他詳細信息。

要驗證數位簽署文件的真實性,接收者必須能夠取得簽署者的公鑰。公鑰用於解密簽名並驗證其真實性。

為什麼數位簽章很重要?

數位簽章很重要,因為它們提供高水平的安全性並確保數位文件的完整性。

它們通常用於需要簽署文件的情況,例如合同、協議和其他法律文件。

電子簽名相較於傳統簽名提供了幾個好處:

  1. 它們比傳統簽名更安全且防篡改。
  2. 它們可以電子方式驗證,這減少了手動驗證的需求。
  3. 它們可以用於在世界各地簽署文件。
  4. 它們提供了比傳統簽名更高的保證水平。

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

現在我們已經了解了數位簽章是什麼以及為什麼它很重要,讓我們來看看如何使用iTextSharp和IronPDF在C#中將數位簽章添加到PDF文件中。

數位簽章用於驗證電子文件的真實性,而PDF是此類文件的常用數據格式。

在本教程中,我們將比較如何使用兩個C#庫,即iTextSharp和IronPDF,將數位簽章添加到PDF文件中。

電子簽名:使用真實簽名進行數位簽署的未來

iTextSharp是一個用於在C#中創建和操作PDF文件的熱門開源庫。

它包括對數位簽章的支持,並廣泛應用於.NET項目。另一方面,IronPDF是一個商業庫,提供類似的PDF操作功能,包括數位簽章。

在本教程中,我們將演示如何使用這兩個庫為PDF文件添加數位簽章,涵蓋創建數位憑證、使用私鑰簽署PDF文件以及驗證簽章等概念。我們還將為每一步提供源代碼示例。

先決條件

在開始之前,您應該對C#編程和.NET框架有基本的了解。

此外,您需要在項目中安裝iTextSharp和IronPDF庫。

您可以從NuGet下載iTextSharp,而IronPDF可以從IronPDF網站下載。

以程式方式添加數位簽名 PDF

建立數位證書

IronPDF 是一個 .NET 庫,它允許你在 C# 中創建、編輯和簽署 PDF 文檔。在本指南中,我們將逐步介紹使用 IronPDF 和 X509Certificate2 物件簽署 PDF 的過程。

查看簽署人詳細資料

以下是使用IronPDF簽署PDF的步驟:

  1. 引入必要的命名空間。
    using IronPdf; using IronPdf.Signing; using System.Security.Cryptography.X509Certificates;  
    using IronPdf; using IronPdf.Signing; using System.Security.Cryptography.X509Certificates;  
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates
VB   C#
  1. 使用 ChromePdfRenderer 類別渲染 PDF 文件。
    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf("foo");  
    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf("foo");  
Dim renderer As New ChromePdfRenderer()
	Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("foo")
VB   C#

在此範例中,我們正在渲染一個包含單一標頭元素的簡單 HTML 文件。

![](/static-assets/pdf/blog/add-digital-signature-topdf-in-csharp-using-itextsharp/add-digital-signature-topdf-in-csharp-using-itextsharp-6.webp)
  1. 將您要用來簽署 PDF 的數位簽章載入到 X509Certificate2 物件中。
    X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);  
    X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);  
Dim cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)
VB   C#

在這個範例中,我們從名為 "IronSoftware.pfx" 的 PKCS#12 文件中加載證書,並提供密碼 "123456"。我們還將 X509KeyStorageFlags 設置為 Exportable,以便我們可以使用證書的私鑰簽署 PDF。

![](/static-assets/pdf/blog/add-digital-signature-topdf-in-csharp-using-itextsharp/add-digital-signature-topdf-in-csharp-using-itextsharp-7.webp) 
  1. 使用證書創建 PdfSignature 物件。
    var sig = new PdfSignature(cert);  
    var sig = new PdfSignature(cert);  
Dim sig = New PdfSignature(cert)
VB   C#

在此範例中,我們正在使用在步驟3中創建的證書物件來創建一個新的 PdfSignature 物件。

  1. 使用 PdfDocument.Sign 簽署PDF文件()在方法中,傳入您在步驟 4 中創建的 PdfSignature 對象。
  pdf.Sign(sig);  
  pdf.Sign(sig);  
pdf.Sign(sig)
VB   C#
  1. 將已簽名的PDF文件保存到文件中。
    pdf.SaveAs("signed.pdf");  
    pdf.SaveAs("signed.pdf");  
pdf.SaveAs("signed.pdf")
VB   C#

以下是完整的程式碼:

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

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("foo");
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

var sig = new PdfSignature(cert);
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");  
using IronPdf; 
using IronPdf.Signing; 
using System.Security.Cryptography.X509Certificates;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("foo");
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

var sig = new PdfSignature(cert);
pdf.Sign(sig);
pdf.SaveAs("signed.pdf");  
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("foo")
Private cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)

Private sig = New PdfSignature(cert)
pdf.Sign(sig)
pdf.SaveAs("signed.pdf")
VB   C#

IronPDF - 現在在Software.com.br購買

簽署 PDF 檔案

一旦我們創建了數字證書,我們就可以用它來簽署 PDF 檔案。

要簽署 PDF 檔案,我們需要創建一個新的簽名對象並設置其屬性,例如新簽名在頁面上的位置以及簽名的原因。

使用 iTextSharp 簽署 PDF 文件,我們可以使用以下代碼

iTextSharp 是一個流行的開源庫,用於在 C# 中處理 PDF 文檔。使用 iTextSharp,可以創建和操作 PDF 文檔,並向其添加數字簽名。在這篇文章中,我們將重點介紹如何使用 iTextSharp 簽署 PDF 文件。

要開始,你需要在項目中添加 iTextSharp.dll 文件的引用。你可以從他們的官方網站下載 iTextSharp 的最新版本。

在向項目添加了 iTextSharp 引用後,你可以使用 PDFSigner 庫來簽署 PDF 文檔。PDFSigner 庫包含三個類:CertMetaDataPDFSigner

Cert 類用於保存證書並提取簽名所需的信息。MetaData 類保存 PDF 的元數據,PDFSigner 類用於創建簽名並將其添加到 PDF 文檔中。

processCert 方法用於讀取數字證書並提取私鑰條目。如果有可用的數字證書鏈,該方法也會構建它們。

Sign 方法用於使用 PDFStamper 創建新的 PDF 文檔並向其添加簽名。你可以配置簽名外觀,並添加原因、聯系方式和位置屬性。SetCrypto 方法用於使用從證書文件中提取的私鑰和鏈證書來簽署文檔。最後,如果你需要向文檔添加可見簽名,可以使用 SetVisibleSignature 方法。

以下是一個顯示如何使用 iTextSharp 簽署 PDF 文件的示例代碼片段:

using iTextSharp.text.pdf;
using System.IO; 

public void SignPDF(string inputPDF, string outputPDF, string certPath, string certPassword, string reason, string contact, string location, bool visible) { 
  PdfReader reader = new PdfReader(inputPDF);

  //Activate MultiSignatures
  PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true); 

  //To disable Multi signatures uncomment this line: 
  //every new signature will invalidate older ones! 

  //PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(outputPDF, FileMode.Create, FileAccess.Write), '\0'); 

  MetaData metadata = new MetaData();
  metadata.setAuthor("John Doe"); 
  metadata.setTitle("Signed PDF Document");
  st.MoreInfo = metadata.getMetaData();
  st.XmpMetadata = metadata.getStreamedMetaData();
  PdfSignatureAppearance sap = st.SignatureAppearance;

  //Read the certificate and extract the private key entry
  Cert myCert = new Cert(certPath, certPassword);
  sap.SetCrypto(myCert.Akp, myCert.Chain, null, PdfSignatureAppearance.WINCER_SIGNED);
  sap.Reason = reason;
  sap.Contact = contact;
  sap.Location = location; 

  //Add a visible signature to the document if (visible)
  sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
  st.Close();
}  
using iTextSharp.text.pdf;
using System.IO; 

public void SignPDF(string inputPDF, string outputPDF, string certPath, string certPassword, string reason, string contact, string location, bool visible) { 
  PdfReader reader = new PdfReader(inputPDF);

  //Activate MultiSignatures
  PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true); 

  //To disable Multi signatures uncomment this line: 
  //every new signature will invalidate older ones! 

  //PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(outputPDF, FileMode.Create, FileAccess.Write), '\0'); 

  MetaData metadata = new MetaData();
  metadata.setAuthor("John Doe"); 
  metadata.setTitle("Signed PDF Document");
  st.MoreInfo = metadata.getMetaData();
  st.XmpMetadata = metadata.getStreamedMetaData();
  PdfSignatureAppearance sap = st.SignatureAppearance;

  //Read the certificate and extract the private key entry
  Cert myCert = new Cert(certPath, certPassword);
  sap.SetCrypto(myCert.Akp, myCert.Chain, null, PdfSignatureAppearance.WINCER_SIGNED);
  sap.Reason = reason;
  sap.Contact = contact;
  sap.Location = location; 

  //Add a visible signature to the document if (visible)
  sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), 1, null);
  st.Close();
}  
Imports Microsoft.VisualBasic
Imports iTextSharp.text.pdf
Imports System.IO

Public Sub SignPDF(ByVal inputPDF As String, ByVal outputPDF As String, ByVal certPath As String, ByVal certPassword As String, ByVal reason As String, ByVal contact As String, ByVal location As String, ByVal visible As Boolean)
  Dim reader As New PdfReader(inputPDF)

  'Activate MultiSignatures
  Dim st As PdfStamper = PdfStamper.CreateSignature(reader, New FileStream(outputPDF, FileMode.Create, FileAccess.Write), ControlChars.NullChar, Nothing, True)

  'To disable Multi signatures uncomment this line: 
  'every new signature will invalidate older ones! 

  'PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(outputPDF, FileMode.Create, FileAccess.Write), '\0'); 

  Dim metadata As New MetaData()
  metadata.setAuthor("John Doe")
  metadata.setTitle("Signed PDF Document")
  st.MoreInfo = metadata.getMetaData()
  st.XmpMetadata = metadata.getStreamedMetaData()
  Dim sap As PdfSignatureAppearance = st.SignatureAppearance

  'Read the certificate and extract the private key entry
  Dim myCert As New Cert(certPath, certPassword)
  sap.SetCrypto(myCert.Akp, myCert.Chain, Nothing, PdfSignatureAppearance.WINCER_SIGNED)
  sap.Reason = reason
  sap.Contact = contact
  sap.Location = location

  'Add a visible signature to the document if (visible)
  sap.SetVisibleSignature(New iTextSharp.text.Rectangle(100, 100, 250, 150), 1, Nothing)
  st.Close()
End Sub
VB   C#

使用 IronPDF 簽署 PDF 文件,我們可以使用以下代碼

簽署 PDF 文件的步驟如下:

  1. 包含必要的命名空間:
    using IronPdf;  
    using IronPdf;  
Imports IronPdf
VB   C#
  1. 使用 PdfDocument.FromFile 加載 PDF 文件() 方法:
    PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");  
    PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");  
Dim pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf")
VB   C#

在此範例中,我們從目前目錄加載名為 "annual_census.pdf" 的 PDF 檔案。此方法返回一個 PdfDocument 物件,代表所加載的 PDF 檔案。

  1. 對 PDF 檔案進行任何必要的編輯。

在此範例中,我們假定已對 PDF 檔案進行了一些編輯。

  1. 使用 PdfDocument.SignWithFile 簽署 PDF 檔案。() 方法:
    pdf.SignWithFile("/assets/IronSignature.p12", "password", null, PdfDocument.SignaturePermissions.FormFillingAllowed);  
    pdf.SignWithFile("/assets/IronSignature.p12", "password", null, PdfDocument.SignaturePermissions.FormFillingAllowed);  
pdf.SignWithFile("/assets/IronSignature.p12", "password", Nothing, PdfDocument.SignaturePermissions.FormFillingAllowed)
VB   C#

在此示例中,我們使用位於 "/assets" 目錄中的名為 "IronSignature.p12" 的 PKCS#12 文件對 PDF 文件進行簽名。我們還將密碼作為 PKCS#12 文件的第二個參數提供。第三個參數指定簽名 PDF 文件的原因,我們將其設為 null。第四個參數指定簽名權限,我們設置為只允許填寫表單。

  1. 使用 PdfDocument.SaveAsRevision 將帶有簽名的 PDF 文件另存為修訂版本。() 方法:
    PdfDocument pdfWithRevision = pdf.SaveAsRevision();  
    PdfDocument pdfWithRevision = pdf.SaveAsRevision();  
Dim pdfWithRevision As PdfDocument = pdf.SaveAsRevision()
VB   C#

此方法創建了一個新的 PdfDocument 對象,其中包含原始 PDF 文件和作為修訂的簽名。

  1. 使用 PdfDocument.SaveAs 將已簽名的 PDF 文件保存到磁碟。() 方法:
    pdfWithRevision.SaveAs("annual\_census\_2.pdf");  
    pdfWithRevision.SaveAs("annual\_census\_2.pdf");  
pdfWithRevision.SaveAs("annual\_census\_2.pdf")
VB   C#

在此範例中,我們將已簽署的PDF文件檔名儲存為在當前目錄中的“annual_census_2.pdf”。

這裡是完整程式碼:

    using IronPdf; PdfDocument pdf = PdfDocument.FromFile("annual\_census.pdf"); 
    // make any necessary edits 
    pdf.SignWithFile("/assets/IronSignature.p12", "password", null, PdfDocument.SignaturePermissions.FormFillingAllowed); 
    PdfDocument pdfWithRevision = pdf.SaveAsRevision(); 
    pdfWithRevision.SaveAs("annual\_census_2.pdf");  
    using IronPdf; PdfDocument pdf = PdfDocument.FromFile("annual\_census.pdf"); 
    // make any necessary edits 
    pdf.SignWithFile("/assets/IronSignature.p12", "password", null, PdfDocument.SignaturePermissions.FormFillingAllowed); 
    PdfDocument pdfWithRevision = pdf.SaveAsRevision(); 
    pdfWithRevision.SaveAs("annual\_census_2.pdf");  
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

IronPDF for .NET

驗證數位簽章

一旦 PDF 文件被簽署後,驗證簽章非常重要,以確保文件在簽署後未被修改。

使用 iTextSharp 庫驗證數字簽名

要使用 iTextSharp 在 C# 中驗證 PDF 數字簽名,請按照以下步驟操作:

  1. 首先,將 iTextSharp 庫添加到您的項目中。您可以使用 Visual Studio 中的 NuGet 包管理器來完成此操作。搜索 iTextSharp 並安裝它。

  2. 定義一個函數來使用 iTextSharp 庫驗證 PDF 文件的數字簽名。

  3. 在函數中,通過傳遞您要驗證的 PDF 文件的路徑來創建一個 PdfReader 對象。

  4. 接下來,使用 PdfReader 對象的 GetAcroFields 方法獲取 PDF 文件的 AcroFields。

  5. 使用 AcroFields 對象的 GetSignatureNames 方法獲取簽名名稱。

  6. 迭代簽名名稱,並使用 AcroFields 對象的 GetSignature 方法檢索每個簽名的 PdfPKCS7 對象。

  7. 使用 PdfPKCS7 對象的 Verify 方法驗證簽名。

  8. 如果簽名有效,返回 true。否則,返回 false。

以下是使用 iTextSharp 在 C# 中驗證 PDF 文件數字簽名的代碼:

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;
using System.IO;

public bool VerifyPdfSignature(string pdfPath)
{
    // 創建 PdfReader 對象
    using (PdfReader reader = new PdfReader(pdfPath))
    {
        // 獲取 AcroFields
        AcroFields fields = reader.AcroFields;

        // 獲取簽名名稱
        var names = fields.GetSignatureNames();

        foreach (string name in names)
        {
            // 獲取 PdfPKCS7 對象
            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            // 驗證簽名
            bool isValidSignature = pkcs7.Verify();

            if (!isValidSignature)
            {
                return false;
            }
        }
    }
    return true;
}
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;
using System.IO;

public bool VerifyPdfSignature(string pdfPath)
{
    // 創建 PdfReader 對象
    using (PdfReader reader = new PdfReader(pdfPath))
    {
        // 獲取 AcroFields
        AcroFields fields = reader.AcroFields;

        // 獲取簽名名稱
        var names = fields.GetSignatureNames();

        foreach (string name in names)
        {
            // 獲取 PdfPKCS7 對象
            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            // 驗證簽名
            bool isValidSignature = pkcs7.Verify();

            if (!isValidSignature)
            {
                return false;
            }
        }
    }
    return true;
}
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.security
Imports System.IO

Public Function VerifyPdfSignature(ByVal pdfPath As String) As Boolean
	' 創建 PdfReader 對象
	Using reader As New PdfReader(pdfPath)
		' 獲取 AcroFields
		Dim fields As AcroFields = reader.AcroFields

		' 獲取簽名名稱
		Dim names = fields.GetSignatureNames()

		For Each name As String In names
			' 獲取 PdfPKCS7 對象
			Dim pkcs7 As PdfPKCS7 = fields.VerifySignature(name)

			' 驗證簽名
			Dim isValidSignature As Boolean = pkcs7.Verify()

			If Not isValidSignature Then
				Return False
			End If
		Next name
	End Using
	Return True
End Function
VB   C#
public static bool VerifyPdfDigitalSignature(string filePath)
{
    bool isValid = false;

    try
    {
        // Create a PdfReader object
        PdfReader reader = new PdfReader(filePath);

        // Get the AcroFields of the PDF document
        AcroFields fields = reader.AcroFields;

        // Get the signature names
        List<string> names = fields.GetSignatureNames();

        // Iterate through the signature names
        foreach (string name in names)
        {
            // Get the PdfPKCS7 object for the signature
            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            // Verify the signature
            if (pkcs7.Verify())
            {
                isValid = true;
            }
            else
            {
                isValid = false;
            }
        }

        reader.Close();
    }
    catch (Exception ex)
    {
        // Handle exception
        isValid = false;
    }

    return isValid;
}
public static bool VerifyPdfDigitalSignature(string filePath)
{
    bool isValid = false;

    try
    {
        // Create a PdfReader object
        PdfReader reader = new PdfReader(filePath);

        // Get the AcroFields of the PDF document
        AcroFields fields = reader.AcroFields;

        // Get the signature names
        List<string> names = fields.GetSignatureNames();

        // Iterate through the signature names
        foreach (string name in names)
        {
            // Get the PdfPKCS7 object for the signature
            PdfPKCS7 pkcs7 = fields.VerifySignature(name);

            // Verify the signature
            if (pkcs7.Verify())
            {
                isValid = true;
            }
            else
            {
                isValid = false;
            }
        }

        reader.Close();
    }
    catch (Exception ex)
    {
        // Handle exception
        isValid = false;
    }

    return isValid;
}
Public Shared Function VerifyPdfDigitalSignature(ByVal filePath As String) As Boolean
	Dim isValid As Boolean = False

	Try
		' Create a PdfReader object
		Dim reader As New PdfReader(filePath)

		' Get the AcroFields of the PDF document
		Dim fields As AcroFields = reader.AcroFields

		' Get the signature names
		Dim names As List(Of String) = fields.GetSignatureNames()

		' Iterate through the signature names
		For Each name As String In names
			' Get the PdfPKCS7 object for the signature
			Dim pkcs7 As PdfPKCS7 = fields.VerifySignature(name)

			' Verify the signature
			If pkcs7.Verify() Then
				isValid = True
			Else
				isValid = False
			End If
		Next name

		reader.Close()
	Catch ex As Exception
		' Handle exception
		isValid = False
	End Try

	Return isValid
End Function
VB   C#

在上述程式碼中,VerifyPdfDigitalSignature 函式將 PDF 文件的路徑作為參數,並返回一個布林值,指示數位簽章是否有效。

請注意,該函式會驗證 PDF 文件中的所有簽章。如果您只想驗證特定的簽章,可以將簽章名稱作為參數傳遞給 VerifySignature 方法。

使用 IronPDF 驗證數位簽名

如果您有一個包含一個或多個數位簽名的 PDF 文件,您可以使用 IronPDF 驗證簽名,確保文件未被竄改。以下是使用 IronPDF 的 VerifyPdfSignatures 方法進行驗證的方式:

  1. 加載 PDF 文件 首先,您需要使用 IronPDF 的 PdfDocument.FromFile 方法加載 PDF 文件。此方法接收文件路徑作為參數,並返回表示該 PDF 文件的 PdfDocument 物件。
    PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");  
    PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");  
Dim pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf")
VB   C#
  1. 驗證簽名 一旦擁有 PdfDocument 物件後,您可以調用 VerifyPdfSignatures 方法來驗證文件中的所有簽名。此方法返回一個布爾值,指示所有簽名是否有效。
    bool isValid = pdf.VerifyPdfSignatures();  
    bool isValid = pdf.VerifyPdfSignatures();  
Dim isValid As Boolean = pdf.VerifyPdfSignatures()
VB   C#

處理結果

VerifyPdfSignatures 方法返回一個布林值,用於指示文檔中的所有簽名是否有效。如果值為 true,即表示所有簽名均有效且文檔未被竄改。如果值為 false,即表示一個或多個簽名無效,且文檔可能已被竄改。

您可以使用此訊息採取適當的行動,例如向用戶顯示消息或防止文檔進一步處理。

以下是顯示如何使用 IronPDF 驗證 PDF 文檔中所有簽名的完整代碼片段:

using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");
bool isValid = pdf.VerifyPdfSignatures();

if (isValid)
{
    Console.WriteLine("All signatures are valid");
}
else
{
    Console.WriteLine("One or more signatures are invalid");
}
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");
bool isValid = pdf.VerifyPdfSignatures();

if (isValid)
{
    Console.WriteLine("All signatures are valid");
}
else
{
    Console.WriteLine("One or more signatures are invalid");
}
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf")
Private isValid As Boolean = pdf.VerifyPdfSignatures()

If isValid Then
	Console.WriteLine("All signatures are valid")
Else
	Console.WriteLine("One or more signatures are invalid")
End If
VB   C#

許可證與定價

iTextSharp 和 IronPDF 庫有不同的許可證和定價模式,這可能是為您的項目選擇它們時需要考慮的因素。

iTextSharp 是根據 Affero 通用公共許可證 授權的。 (AGPL), 這是要求使用iTextSharp的任何軟體也必須在AGPL許可下進行許可的copyleft許可證。

但是,iTextSharp也有商業許可證,允許開發人員在專有軟體中使用這個庫而無需發布其源代碼。

iTextSharp商業許可證的價格根據開發人員數量和部署實例而有所不同。

另一方面,IronPDF 是一個需要付費授權才能使用的商業庫。IronPDF 的定價是基於開發人員數量和部署實例數量,類似於 iTextSharp。

但是,IronPDF 也為用戶提供了一個 免費試用版 具有有限功能,可用於非商業專案。

在...方面 定價IronPDF可能是較小型項目或預算有限項目的更好選擇,因為免費試用版本提供了一些功能而不收費。

然而,對於較大型項目或有特定授權需求的項目來說,iTextSharp可能是一個更好的選擇,因為它的雙重授權模式和對AGPL的支持。

結論

IronPDF 和 iTextSharp 是兩個在 C# 中處理 PDF 文件的熱門庫。雖然這兩個庫在處理 PDF 文件方面提供了類似的功能,但在選擇使用哪個庫時,開發人員應該考慮一些差異。

從技術角度來看,IronPDF 是一個現代化的 .NET 庫,提供了一個簡單直觀的 API 來處理 PDF 文件。它支持廣泛的 PDF 功能,包括文本、圖像、表單和數字簽名。IronPDF 還提供了進階功能,如 HTML 到 PDF 的轉換和 PDF 編輯。

另一方面,iTextSharp 是一個成熟的 PDF 庫,其 API 比較複雜,可能需要更多的學習努力。然而,iTextSharp 提供了進階功能,如低層次的 PDF 操作以及對廣泛的 PDF 標準的支持。

在許可方面,IronPDF 提供了一個簡單的商業許可模式,允許開發人員在專有軟體中使用該庫,而不需要發布他們的源代碼。這使開發人員能夠更輕鬆地在商業項目中使用 IronPDF,而不用擔心許可合規性。另一方面,iTextSharp 提供了一個免費的 AGPL 許可和一個用於專有軟體的商業許可的雙重許可模式。雖然這為開發人員提供了更多的靈活性,但也可能更複雜。

從技術角度來看,IronPDF 提供了一個更現代和直觀的 API,可能更容易讓開發人員使用。此外,IronPDF 提供了進階功能,如 HTML 到 PDF 的轉換和 PDF 編輯,這對某些項目來說可能很有用。然而,iTextSharp 提供了更多進階功能,如低層次的 PDF 操作以及對廣泛的 PDF 標準的支持。

IronPDF 可供用戶使用於 免費試用 並且可以 授權 商業用途的Lite套裝價格僅從499美元起。

最後,IronPDF提供特別促銷,開發人員可以以兩個產品的價格購買所有6個Iron Software產品 Iron Software 產品。這可以讓開發人員以實惠的價格獲得廣泛強大的 .NET 程式庫。

< 上一頁
Windows 上 Wkhtmltopdf 與 IronPDF 的比較
下一個 >
IronPDF 和 ExpertPDF for .NET 的比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >