跳過到頁腳內容
產品比較
IronPDF 與 iTextSharp 在 C# 中的比較

iTextSharp C# HTML to PDF Alternative for .NET Core

對於從事 PDF 開發的人員來說,擁有一個可靠的 PDF 生成和操作庫至關重要。 在 .NET 生態系統中,有數十個 C# PDF 函式庫可供選擇,那麼如何選擇最適合您需求的函式庫呢?

在 .NET 應用程式中使用 PDF 功能時,選擇合適的程式庫對於高效開發至關重要。 本文對兩個著名的 C# PDF 庫進行了詳細的比較: IronPDFiText 7 (以前稱為 iTextSharp)。 我們將探討它們的特性、性能、許可和對各種專案需求的適用性,以幫助您做出明智的決定。

為什麼選擇 .NET PDF 函式庫?

PDF 文件廣泛用於報告、發票和法律文件中,因此 PDF 文件的產生和處理對於許多應用至關重要。 選擇圖書館時,需要考慮的關鍵因素包括:

*整合便利性*– 您可以多快實現 PDF 功能?
支援 HTML 轉 PDF – 是否可以輕鬆地從網頁內容轉換?
許可證和費用-是免費的,還是需要商業許可?
功能集**– 是否支援文字擷取、簽名或編輯?
*效能– 產生或處理 PDF 的速度有多快?

IronPDF 和 iText7 概述

IronPDF簡介

IronPDF 是一個專為 .NET 開發人員設計的商業 PDF 程式庫。 它簡化了 PDF 的生成、操作和轉換,使其成為 C# 應用程式中最容易使用的程式庫之一。

IronPDF 支援 .NET Core、.NET Framework 和 .NET Standard,確保與各種 .NET 環境相容。 它具有高度的跨平台相容性,使其成為在不同應用程式環境下工作的團隊的理想選擇,並且可以與 Visual Studio 等 IDE 無縫整合。 除了 .NET 版本外,IronPDF 還提供 Java、Python 和 Node.js 版本。

主要特點:

*內建 HTML 轉 PDF 支援– 無需額外外掛程式即可將網頁HTML 、CSS 和 JavaScript 轉換為 PDF。

  • PDF 編輯– 透過新增文字、圖像頁首和頁尾來修改現有 PDF。
  • PDF 安全性– 加密 PDF、設定密碼保護並管理檢視、列印或編輯權限。
    *浮水印和註釋*– 輕鬆地將文字和圖像浮水印、圖章或註釋套用到文件中。
    表單填寫與資料擷取**– 以程式方式填入互動式PDF 表單並擷取表單資料。

最適合:尋求簡單、一體化解決方案,而不想受額外外掛程式或複雜授權的開發人員。

iText7 簡介

iText 7 是一個功能強大且靈活的 PDF 函式庫,提供廣泛的 PDF 操作功能,包括文件建立、加密和簽章。 但是,它的核心庫本身並不支援 HTML 到 PDF 的轉換。

主要特點

*低階 PDF 自訂*– 提供 PDF 結構、元資料和渲染的詳細控制。
可存取性和合規性:**產生 PDF/A、PDF/UA 和 PDF/X,用於長期存檔和可訪問性合規性。

  • HTML 轉 PDF 轉換:付費的 pdfHTML 外掛程式可以將 HTML 內容轉換為 PDF。
  • Java 和 .NET 支援:主要為 Java 設計,透過 iText 7 for .NET 提供 C# 支援。
  • PDF 表單管理:建立和編輯 AcroForms 和 XFA 表單,用於互動式 PDF 表單。

最適合:需要高度可自訂的 PDF 解決方案,並且願意購買額外外掛程式以獲得擴充功能的開發人員。

特點和優勢

在深入了解各項功能及其對應的程式碼範例之前,讓我們先來看看 IronPDF 和 iText 7 之間最大的功能差異之一: HTML 到 PDF 的轉換

  • IronPDF原生支援HTML、CSS 和 JavaScript渲染,無需任何額外元件。
  • iText 7則需要pdfHTML 插件,這是商業許可下的付費功能。 這會增加需要網頁轉PDF功能的開發人員的成本。

HTML 轉 PDF 比較流程圖

結論:如果您需要將 HTML 轉換為 PDF ,IronPDF 是更具成本效益的解決方案,因為它本身就包含此功能。

IronPDF 主要功能(附程式碼範例)

IronPDF 擁有豐富的 PDF 文件處理功能。 這些功能包括PDF創建、PDF操作和安全保障。 為了更清楚地了解該程式庫提供的廣泛功能,我們將重點介紹其中的一些關鍵功能。

HTML 轉 PDF

使用 IronPDF 強大的渲染引擎將 HTML 內容轉換為高品質的 PDF 文件。 IronPDF 的渲染器不只是轉換 HTML 內容; 有了它,您將能夠保留所有原始的 CSS 樣式和 JavaScript 互動性。

using IronPdf;

public class Program
{
    static void Main(string[] args)
    {
        // Create a new ChromePdfRenderer instance for rendering HTML to PDF
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the specified HTML file as a PDF document
        PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");

        // Save the rendered PDF to the specified file path
        pdf.SaveAs("HtmlToPdf.pdf");
    }
}
using IronPdf;

public class Program
{
    static void Main(string[] args)
    {
        // Create a new ChromePdfRenderer instance for rendering HTML to PDF
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the specified HTML file as a PDF document
        PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");

        // Save the rendered PDF to the specified file path
        pdf.SaveAs("HtmlToPdf.pdf");
    }
}
Imports IronPdf

Public Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a new ChromePdfRenderer instance for rendering HTML to PDF
		Dim renderer As New ChromePdfRenderer()

		' Render the specified HTML file as a PDF document
		Dim pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("example.html")

		' Save the rendered PDF to the specified file path
		pdf.SaveAs("HtmlToPdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

輸入 HTML
輸入 HTML 內容

輸出 PDF
使用 IronPDF 將 HTML 輸出為 PDF

在這個程式碼範例中,我們首先建立了一個新的ChromePdfRenderer實例,這使我們能夠存取 IronPDF 用於將 HTML 渲染為 PDF 的強大渲染引擎。 然後,我們將 HTML 檔案傳遞給RenderHtmlFileAsPdf()方法,該方法會將 HTML 渲染成 PDF,並將其儲存在PdfDocument物件中。 最後,我們將把PDF文件儲存到指定的文件位置。

PDF檔案的URL

對於想要將 URL 內容轉換為 PDF 的開發人員來說,IronPDF 是最佳選擇。 借助此庫,您可以使用 ChromePdfRenderer 渲染引擎創建像素級完美的 PDF 文檔,該引擎在將URL 渲染為 PDF時將保持所有原始樣式和佈局。 在這個範例中,我們將使用此 URL來示範 IronPDF 如何處理更複雜的 CSS 樣式。

using IronPdf;

public class Program
{
    static void Main(string[] args)
    {
        // Create a new ChromePdfRenderer instance
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the specified URL as a PDF document
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");

        // Save the rendered PDF to the specified file path
        pdf.SaveAs("UrlToPdf.pdf");
    }
}
using IronPdf;

public class Program
{
    static void Main(string[] args)
    {
        // Create a new ChromePdfRenderer instance
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render the specified URL as a PDF document
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");

        // Save the rendered PDF to the specified file path
        pdf.SaveAs("UrlToPdf.pdf");
    }
}
Imports IronPdf

Public Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a new ChromePdfRenderer instance
		Dim renderer As New ChromePdfRenderer()

		' Render the specified URL as a PDF document
		Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.apple.com")

		' Save the rendered PDF to the specified file path
		pdf.SaveAs("UrlToPdf.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

PDF 輸出:
使用 IronPDF 輸出 PDF 文件的 URL

與我們的 HTML 轉 PDF 範例類似,使用 IronPDF 將任何 URL 轉換為 PDF 的第一步是先建立一個新的ChromePdfRenderer實例。 此方法使用RenderUrlAsPdf將 URL 內容渲染成 PDF 格式後,會將產生的 PDF 儲存到新的PdfDocument物件中,然後再使用SaveAs方法儲存 PDF。

PDF簽名

透過對 PDF 文件套用數位簽名,確保 PDF 文件的真實性。 開發人員可以考慮採用不同的方法來應用數位簽名,例如使用安全憑證對 PDF 進行數位簽名、將手寫簽名的影像添加到 PDF 中,或將憑證的影像蓋章到 PDF 本身上。

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

public class Program
{
    static void Main(string[] args)
    {
        // Load the certificate used for signing the PDF
        X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);

        // Create a PdfSignature instance and set the signature image
        var sig = new PdfSignature(cert);
        sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));

        // Sign the existing PDF file and save the signed version
        sig.SignPdfFile("product_report.pdf");
    }
}
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
using IronSoftware.Drawing;

public class Program
{
    static void Main(string[] args)
    {
        // Load the certificate used for signing the PDF
        X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);

        // Create a PdfSignature instance and set the signature image
        var sig = new PdfSignature(cert);
        sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));

        // Sign the existing PDF file and save the signed version
        sig.SignPdfFile("product_report.pdf");
    }
}
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates
Imports IronSoftware.Drawing

Public Class Program
	Shared Sub Main(ByVal args() As String)
		' Load the certificate used for signing the PDF
		Dim cert As New X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable)

		' Create a PdfSignature instance and set the signature image
		Dim sig = New PdfSignature(cert)
		sig.SignatureImage = New PdfSignatureImage("IronPdf.png", 0, New Rectangle(150, 100, 350, 250))

		' Sign the existing PDF file and save the signed version
		sig.SignPdfFile("product_report.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出 PDF
使用 IronPDF 輸出數位簽名

在這個例子中,我們加載了證書對象,創建了簽名的可視化表示(在本例中是 IronPDF 圖像),並創建了一個新的PdfSignature對象,該對象負責對 PDF 文件本身進行簽名。 最後,我們使用SignPdfFile對 PDF 文件進行簽名並儲存。

如果您想探索 IronPDF 的更多功能,請務必查看其內容豐富的"功能"頁面,或包含每個功能深入程式碼範例的"操作指南"

iText7 主要功能(附程式碼範例)

iText7 提供多種功能,可用於自訂和增強您的 PDF 文件。 該 PDF 庫具有廣泛的格式支持,涵蓋各種 PDF 標準和高級 PDF 操作功能,內容非常豐富。 但是,如前所述,iText7 可能需要額外的軟體包才能執行某些與 PDF 相關的任務,例如 HTML 轉到 PDF。

HTML 轉 PDF

雖然 iText7 本身無法處理 HTML 到 PDF 的轉換,但我們可以利用 pdfHTML(iText7 商業許可下的付費外掛程式)將我們在 IronPDF 範例中使用的 HTML 檔案轉換為 PDF 文件。

using iText.Html2pdf;
using System.IO;

public class Program
{
    static void Main(string[] args)
    {
        // Open the HTML file stream
        using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
        // Create the output PDF file stream
        using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
        {
            // Initialize ConverterProperties for HTML to PDF conversion
            ConverterProperties converterProperties = new ConverterProperties();

            // Convert the HTML source to a PDF document
            HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);

            // Close the PDF file
            pdf.Close();
        }
    }
}
using iText.Html2pdf;
using System.IO;

public class Program
{
    static void Main(string[] args)
    {
        // Open the HTML file stream
        using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
        // Create the output PDF file stream
        using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
        {
            // Initialize ConverterProperties for HTML to PDF conversion
            ConverterProperties converterProperties = new ConverterProperties();

            // Convert the HTML source to a PDF document
            HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);

            // Close the PDF file
            pdf.Close();
        }
    }
}
Imports iText.Html2pdf
Imports System.IO

Public Class Program
	Shared Sub Main(ByVal args() As String)
		' Open the HTML file stream
		Using htmlSource As FileStream = File.Open("example.html", FileMode.Open)
		' Create the output PDF file stream
		Using pdf As FileStream = File.Open("HtmlToPdfOutput.pdf", FileMode.Create)
			' Initialize ConverterProperties for HTML to PDF conversion
			Dim converterProperties As New ConverterProperties()

			' Convert the HTML source to a PDF document
			HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties)

			' Close the PDF file
			pdf.Close()
		End Using
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出 PDF
! iText7 HTML 轉 PDF 輸出

在這個例子中,我們載入了 HTML 文件,並指定了渲染後的 PDF 文件的儲存位置。 然後,使用ConvertToPdf方法,我們可以輕鬆地將 HTML 檔案轉換為 PDF 文件。

PDF檔案的URL

現在,是時候比較一下 iText7 與 IronPDF 在將 URL 轉換為 PDF 方面的表現了。 為此,我們將使用與先前完全相同的網址,以確保公平比較。

using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;

public class Program
{
    public static async System.Threading.Tasks.Task Main(string[] args)
    {
        string url = "https://www.apple.com";  // Replace with your target URL
        string outputPdfPath = "output.pdf";

        try
        {
            // Download HTML content from the URL
            using (HttpClient client = new HttpClient())
            {
                string htmlContent = await client.GetStringAsync(url);

                // Convert HTML to PDF
                using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
                {
                    ConverterProperties properties = new ConverterProperties();
                    HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
                }
            }

            Console.WriteLine("PDF created successfully: " + outputPdfPath);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;

public class Program
{
    public static async System.Threading.Tasks.Task Main(string[] args)
    {
        string url = "https://www.apple.com";  // Replace with your target URL
        string outputPdfPath = "output.pdf";

        try
        {
            // Download HTML content from the URL
            using (HttpClient client = new HttpClient())
            {
                string htmlContent = await client.GetStringAsync(url);

                // Convert HTML to PDF
                using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
                {
                    ConverterProperties properties = new ConverterProperties();
                    HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
                }
            }

            Console.WriteLine("PDF created successfully: " + outputPdfPath);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
Imports System
Imports System.Net.Http
Imports System.IO
Imports iText.Html2pdf

Public Class Program
	Public Shared Async Function Main(ByVal args() As String) As System.Threading.Tasks.Task
		Dim url As String = "https://www.apple.com" ' Replace with your target URL
		Dim outputPdfPath As String = "output.pdf"

		Try
			' Download HTML content from the URL
			Using client As New HttpClient()
				Dim htmlContent As String = Await client.GetStringAsync(url)

				' Convert HTML to PDF
				Using pdfStream As New FileStream(outputPdfPath, FileMode.Create)
					Dim properties As New ConverterProperties()
					HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties)
				End Using
			End Using

			Console.WriteLine("PDF created successfully: " & outputPdfPath)
		Catch ex As Exception
			Console.WriteLine("Error: " & ex.Message)
		End Try
	End Function
End Class
$vbLabelText   $csharpLabel

輸出 PDF
! iText7 URL 到 PDF 輸出

從這裡可以看出,iText7 的 URL 轉 PDF 方法更加手動且複雜。 首先,我們需要從 URL 下載 HTML 內容,然後按照 HTML 轉 PDF 範例中類似的步驟,將 URL 內容渲染成 PDF 文件並儲存。 從輸出影像中可以看到,與 IronPDF 不同,iText7 無法保留大部分原始樣式和佈局。

PDF簽名

using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
using System.IO;

public class Program
{
    static void Main(string[] args)
    {
        string inputPdf = "input.pdf";       // PDF to be signed
        string outputPdf = "signed_output.pdf";  // Signed PDF output
        string pfxFile = "IronSoftware.pfx";  // Path to your PFX certificate
        string password = "Passw0rd";   // Password for PFX file

        try
        {
            // Load your certificate
            Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
            using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
            {
                ks.Load(fs, password.ToCharArray());
            }

            string alias = null;
            foreach (string al in ks.Aliases)
            {
                if (ks.IsKeyEntry(al))
                {
                    alias = al;
                    break;
                }
            }

            if (alias == null)
            {
                throw new Exception("Alias not found in the PFX file.");
            }

            ICipherParameters pk = ks.GetKey(alias).Key;
            X509CertificateEntry[] chain = ks.GetCertificateChain(alias);

            // Convert BouncyCastle certificates to iText certificates
            var itextCertChain = new IX509Certificate[chain.Length];
            for (int i = 0; i < chain.Length; i++)
            {
                itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
            }

            // Create output PDF with signed content
            using (PdfReader reader = new PdfReader(inputPdf))
            using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
            {
                PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());

                // Set up the external signature (private key + digest algorithm)
                IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
                IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
                IExternalDigest digest = new BouncyCastleDigest();

                // Perform the signing (detached signature)
                signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
            }

            Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error signing PDF: {ex.Message}");
        }
    }
}
using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
using System.IO;

public class Program
{
    static void Main(string[] args)
    {
        string inputPdf = "input.pdf";       // PDF to be signed
        string outputPdf = "signed_output.pdf";  // Signed PDF output
        string pfxFile = "IronSoftware.pfx";  // Path to your PFX certificate
        string password = "Passw0rd";   // Password for PFX file

        try
        {
            // Load your certificate
            Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
            using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
            {
                ks.Load(fs, password.ToCharArray());
            }

            string alias = null;
            foreach (string al in ks.Aliases)
            {
                if (ks.IsKeyEntry(al))
                {
                    alias = al;
                    break;
                }
            }

            if (alias == null)
            {
                throw new Exception("Alias not found in the PFX file.");
            }

            ICipherParameters pk = ks.GetKey(alias).Key;
            X509CertificateEntry[] chain = ks.GetCertificateChain(alias);

            // Convert BouncyCastle certificates to iText certificates
            var itextCertChain = new IX509Certificate[chain.Length];
            for (int i = 0; i < chain.Length; i++)
            {
                itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
            }

            // Create output PDF with signed content
            using (PdfReader reader = new PdfReader(inputPdf))
            using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
            {
                PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());

                // Set up the external signature (private key + digest algorithm)
                IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
                IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
                IExternalDigest digest = new BouncyCastleDigest();

                // Perform the signing (detached signature)
                signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
            }

            Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error signing PDF: {ex.Message}");
        }
    }
}
Imports System.Security.Cryptography.X509Certificates
Imports iText.Kernel.Pdf
Imports iText.Signatures
Imports iText.Bouncycastle.Crypto
Imports iText.Commons.Bouncycastle.Cert
Imports iText.Commons.Bouncycastle.Crypto
Imports Org.BouncyCastle.Pkcs
Imports Org.BouncyCastle.Crypto
Imports iText.Bouncycastle.X509
Imports iText.Kernel.Crypto
Imports System.IO

Public Class Program
	Shared Sub Main(ByVal args() As String)
		Dim inputPdf As String = "input.pdf" ' PDF to be signed
		Dim outputPdf As String = "signed_output.pdf" ' Signed PDF output
		Dim pfxFile As String = "IronSoftware.pfx" ' Path to your PFX certificate
		Dim password As String = "Passw0rd" ' Password for PFX file

		Try
			' Load your certificate
			Dim ks As Pkcs12Store = (New Pkcs12StoreBuilder()).Build()
			Using fs As New FileStream(pfxFile, FileMode.Open, FileAccess.Read)
				ks.Load(fs, password.ToCharArray())
			End Using

			Dim [alias] As String = Nothing
			For Each al As String In ks.Aliases
				If ks.IsKeyEntry(al) Then
					[alias] = al
					Exit For
				End If
			Next al

			If [alias] Is Nothing Then
				Throw New Exception("Alias not found in the PFX file.")
			End If

			Dim pk As ICipherParameters = ks.GetKey([alias]).Key
			Dim chain() As X509CertificateEntry = ks.GetCertificateChain([alias])

			' Convert BouncyCastle certificates to iText certificates
			Dim itextCertChain = New IX509Certificate(chain.Length - 1){}
			For i As Integer = 0 To chain.Length - 1
				itextCertChain(i) = New X509CertificateBC(chain(i).Certificate)
			Next i

			' Create output PDF with signed content
			Using reader As New PdfReader(inputPdf)
			Using os As New FileStream(outputPdf, FileMode.Create, FileAccess.Write)
				Dim signer As New PdfSigner(reader, os, (New StampingProperties()).UseAppendMode())

				' Set up the external signature (private key + digest algorithm)
				Dim iTextPrivateKey As IPrivateKey = New PrivateKeyBC(pk)
				Dim pks As IExternalSignature = New PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256)
				Dim digest As IExternalDigest = New BouncyCastleDigest()

				' Perform the signing (detached signature)
				signer.SignDetached(digest, pks, itextCertChain, Nothing, Nothing, Nothing, 0, PdfSigner.CryptoStandard.CMS)
			End Using
			End Using

			Console.WriteLine($"PDF digitally signed successfully: {outputPdf}")
		Catch ex As Exception
			Console.WriteLine($"Error signing PDF: {ex.Message}")
		End Try
	End Sub
End Class
$vbLabelText   $csharpLabel

輸出 PDF
! iText7 數位簽章輸出

如您所見,雖然 iText7 能夠對 PDF 文件進行數位簽名,但該過程往往比 IronPDF 要複雜得多。 這段程式碼載入 PFX 憑證並使用該憑證對 PDF 檔案進行數位簽署。 它提取私鑰和證書,設定簽名者,並將分離簽名添加到 PDF,然後保存簽名文件。

競爭分析

效能和可用性

IronPDF 的主要優勢

IronPDF 以其易用性和高效能最佳化而聞名,提供更直接的 API,簡化了 HTML 轉 PDF 等常見任務。 它提供了抽象複雜 PDF 處理任務的高級方法,使開發人員能夠以最少的程式碼產生、編輯和操作 PDF。 對於多執行緒或大規模文件處理,IronPDF 支援並行執行。

*易於使用的 API* – 進階方法簡化了常見任務,例如 HTML 到 PDF 的轉換。
只需極少的設定-可輕鬆整合到 .NET 專案中。
內建並行執行- 針對大量 PDF 產生和轉換進行了最佳化。
簡化的 API** – 只需更少的程式碼行即可實現結果。

iText 7-功能強大但略顯複雜

另一方面,iText 7 提供了更詳細、更精細的控制級別,這對於需要進行大量自訂的開發人員來說可能是一個優勢。 它提供了一個強大的 API,用於處理底層 PDF 操作。 然而,由於其複雜性,與 IronPDF 相比,iText 7 通常需要更多的程式碼才能達到類似的結果。

*精細的 PDF 控制– 非常適合需要嚴格合規性的企業應用程式*(例如,PDF/A、PDF/UA、數位簽章)。
高度可自訂– 為進階用例提供底層 PDF 操作
學習曲線更陡峭——與 IronPDF 相比,需要更多的設定和配置。
程式碼量較大-常見任務通常需要更長的實作時間**。

許可證和成本

為 .NET 專案選擇 PDF 庫時,許可和成本因素至關重要。 IronPDFiText 7採用不同的授權模式,選擇合適的模式取決於您的專案需求、預算和合規性要求。

IronPDF 許可和費用

IronPDF 採用商業許可模式,這意味著雖然它提供免費的商業許可試用版,並且對開發和評估是免費的,但要進行完整的生產使用,則需要付費許可。 定價透明,取決於使用規模、開發人員數量和專案類型等因素。

IronPDF 許可:

*商業授權模式(無開源限制)。

  • 基於開發者或團隊許可的**簡單定價**。
  • HTML 轉 PDF、PDF 安全性或其他核心功能無需額外費用
  • 最適合需要簡單、經濟高效的 PDF 解決方案**的企業**。

結論: IronPDF 將所有主要功能都包含在一個許可證中,使其成為團隊和企業的經濟高效之選。

iText7 許可證和費用

iText7 採用雙重授權模式,其中包括:

  1. AGPL(GNU Affero 通用公共授權) ——對開源專案免費,但要求使用 iText7 的整個專案必須是開源的且符合 AGPL 規定。 這意味著對專案所做的任何修改或新增都必須公開分享。
    2.商業許可– 任何專有軟體或商業應用程序,如果不想公開其原始程式碼,則需要商業許可。 定價結構根據使用量、支援等級和部署規模而有所不同。

對於希望將 iText 7 整合到專有軟體中的公司而言,商業許可證是必要的。 成本可能很高,尤其是對於企業級解決方案而言,因為它是按開發人員數量定價的,但它可以提供專業支援並確保合法合規。

為什麼 iText 7 的價格可能更高:

*按開發者定價*-每個開發者都需要單獨的許可證,這會增加團隊的總成本。
基本功能需要昂貴的附加元件:**

  • pdfHTML (付費)– HTML 轉 PDF 轉換所需。
  • pdfOCR (付費)-用於從圖像中識別文字
  • pdfCalligraph (付費)– 改善文字渲染和字體支援。
  • pdfRender (付費)– 新增PDF 到影像的轉換功能
  • pdf2Data (付費)– 從 PDF 中擷取結構化資料。
    當需要多個功能和開發人員時,企業成本會迅速增加

結論:如果您的團隊需要多名開發人員以及 HTML 轉 PDF 和 OCR 等關鍵功能,iText 7 的價格可能比 IronPDF 高得多,因為 IronPDF 提供這些功能而無需額外費用。

使用案例場景

小型專案與企業專案

對於需要快速實現 PDF 功能且配置最少的小型到中型專案而言,IronPDF 憑藉其用戶友好的 API 和全面的功能集,是一個極具吸引力的選擇。 對於需要大量自訂和遵守特定 PDF 標準的企業級項目,iText7 的高級功能可能有所幫助;然而,IronPDF 憑藉其高性能、許可選項和易用性,也證明是此類工作的有力候選者。

學術和商業用途

在學術環境或開源專案中,只要專案的授權相容,iText 7 的 AGPL 授權允許免費使用。 對於商業應用,IronPDF 和 iText 7 都需要購買商業授權。 建議您仔細閱讀每個庫的授權條款,以確保其符合您的專案目標。

結論

為您的 .NET 專案選擇合適的 PDF 程式庫是一個至關重要的決定,這取決於易用性、功能集和授權需求等因素。 IronPDF 和 iText 7 都提供強大的 PDF 功能,但它們滿足的需求不同。 除了這兩個函式庫之外,Aspose、Syncfusion 和 PDFSharp 等競爭對手也都提供具有競爭力的 .NET PDF 函式庫。 然而,IronPDF憑藉其易於使用的API、全面的功能集和成本效益,在PDF行業中始終處於領先地位。

下面,我們總結了 IronPDF 作為 .NET PDF 庫的絕佳選擇的理由。

IronPDF 與 iText7 比較概述

IronPDF 的優勢

*易用性:* IronPDF 的設計著重簡潔性,是需要快速將 PDF 功能整合到 .NET 應用程式中的開發人員的理想選擇。 其直覺的 API 降低了學習難度,並加快了開發速度。
全面的文件和支援: IronPDF 提供詳細的文件和快速回應的客戶支持,確保開發人員能夠快速上手並有效地解決問題。
無縫 HTML 轉 PDF 轉換: IronPDF 擅長將 HTML(包括 CSS 和 JavaScript)轉換為高品質的 PDF。 這對於需要從基於 Web 的內容動態產生 PDF 的專案尤其有利。
商業許可:**對於商業應用,IronPDF 提供靈活的許可選項,確保合規性,而不會受到開源許可可能帶來的限制。

iText 的局限性

*複雜性:*雖然 iText 7 提供了高級功能和可自訂性,但對於不熟悉 PDF 操作的開發人員來說,其 API 可能比較複雜。 與 IronPDF 更直接的方法相比,這可能會導致常見任務的開發時間更長。
商業用途的授權費用:** iText 7 的 AGPL 授權要求任何衍生作品都必須開源,除非您購買商業授權。 對於無法遵守 AGPL 條款的專有應用程式來說,這可能是一個限制。

  • Java 為中心的功能:雖然 iText 7 適用於 .NET,但它源自於 Java 生態系統,因此對於 C# 開發人員來說,它有時會感覺不太像原生軟體,尤其是在處理跨平台問題或與基於 Java 的工具整合時。

最後想說的話

如果您的專案需要快速生成 PDF 文件(尤其是從網頁內容生成),並且您正在尋找易於使用的解決方案,那麼IronPDF可能是更好的選擇。但是,如果您的應用程式需要進階 PDF 操作或嚴格遵守 PDF 標準,並且您需要高度的自訂靈活性,那麼iText7可能更適合您。 請考慮專案的特定要求和許可限制,以確定最適合您需求的庫。

立即試用 IronPDF:下載免費試用版,親自體驗 IronPDF 的強大功能!

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

常見問題解答

如何在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換成 PDF。您也可以使用 RenderHtmlFileAsPdf 將 HTML 檔案轉換成 PDF。

IronPDF 與 iText 7 的主要差異為何?

IronPDF 提供 HTML 到 PDF 的無縫轉換,以及直接的商業授權模式,而 iText 7 則需要付費附加元件才能進行 HTML 到 PDF 的轉換,並採用雙重授權模式。IronPDF 以易用著稱,而 iText 7 則提供進階的 PDF 操作功能,但學習曲線較陡。

為什麼我的 .NET 專案應該選擇 IronPDF?

IronPDF 是快速且符合成本效益的 PDF 實作的理想選擇,它提供易用性、全面的說明文件,以及內建的 HTML 至 PDF 轉換功能,且無需額外成本。它支援多種 .NET 版本,並提供 PDF 編輯和安全性等廣泛功能。

IronPDF 適合生成複雜的 PDF 文件嗎?

是的,IronPDF 支援複雜 PDF 文件的產生,具有填表、註解和資料抽取等功能。其廣泛的功能使其適用於各種專業的 PDF 應用程式。

IronPDF 有哪些授權選項?

IronPDF 採用商業授權模式,生產使用需要付費授權。這種直接的模式消除了雙重授權系統(如 iText 7)的複雜性。

IronPDF 可以集成到 .NET Core 專案中嗎?

是的,IronPDF 可以集成到 .NET Core 项目中。它支援 .NET Core、.NET Framework 和 .NET Standard,因此適用於各種開發環境。

IronPdf 如何處理 PDF 安全功能?

IronPDF 提供強大的 PDF 安全功能,包括加密、密碼保護和數位簽署,有助於保護敏感文件資訊的安全。

與其他函式庫相比,IronPDF 何處易於使用?

IronPdf 因其直接的 API、最低限度的設置要求以及全面的說明文件而被視為用戶友好型工具,即使是對 PDF 生成經驗有限的開發人員也能輕鬆實現。

在 IronPDF 中使用 HTML 轉換為 PDF 是否需要額外費用?

不,IronPDF 包含內建的 HTML 至 PDF 轉換功能,不需要額外的元件或成本,不像其他一些 PDF 函式庫需要付費附加元件。

IronPDF 的性能和速度如何?

IronPDF 在性能和速度方面進行了優化,提供快速可靠的 PDF 生成和操作,這對於處理大量 PDF 處理的應用程式而言至關重要。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。