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

iTextSharp C# HTML到PDF的替代方案適用於.NET Core

Full Comparison

Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.

View Full Comparison
## IronPDF與 iTextSharp / iText7 的對比

全面的.NET PDF 函式庫比較-逐項功能對比,並提供基於事實的上下文分析

iTextSharp / iText7
13
共 20 項功能
10 是7 複雜3 否
IronPDF ✦
20
共 20 項功能
20 是0 複雜0 否
特徵 iTextSharp / iText7 IronPDF ✦
PDF Creation & Conversion
HTML/CSS 轉 PDF $ Paid Add-on
透過`pdfHTML`將 HTML 轉換為 PDF(單獨的軟體包;AGPL/商業模式)。
✓ Yes
基於 Chromium 核心的引擎,內建像素級精準的 CSS3、Flexbox 和 Grid 渲染圖。
JavaScript執行 ? Unknown
pdfHTML 描述了 HTML/CSS→PDF 的轉換,但文件中沒有說明是否支援 JS 執行。
✓ Yes
在渲染過程中完全執行 JS——動態圖表、SPA 和互動式內容。
程式化生成 ✓ Yes
定位為面向.NET的可程式 PDF SDK—建立、編輯和增強。
✓ Yes
可從 HTML 模板、字串、ASPX 視圖或圖像生成內容。 Chromium 負責佈置。
PDF檔案的URL $ Paid Add-on
可以透過 pdfHTML 外掛程式的 URL 取得功能實現,但這不是核心功能。
✓ Yes
`RenderUrlAsPdf()` 捕獲任何實時 URL 並進行完整的 CSS/JS 渲染。
DOCX 轉 PDF ✕ No
不支援原生 Word 轉換-iText 是一個 PDF 原生 SDK。
✓ Yes
`DocxToPdfRenderer` 轉換 Word 文檔,保留其結構和格式。
Reading & Extraction
文字擷取 ✓ Yes
`PdfTextExtractor.GetTextFromPage()` 採用多種提取策略。
✓ Yes
提取文字時能夠感知佈局。可與IronOCR結合使用,識別掃描文件。
將頁面渲染為圖像 ? Unknown
OCR 工作流程中提到了渲染,但在引用的 iText 文件中沒有發現主要來源的"PDF→影像渲染器"模組。
✓ Built-in
原生柵格化為PNG、JPEG、BMP格式,DPI可設定。
內建OCR $ Paid Add-on
提供 pdfOCR 插件;安裝說明中提到了平台特定的/原生依賴項(例如,Linux/macOS 運行時要求)。
✓ Via IronOCR
與IronOCR原生集成,可對掃描的 PDF 文件進行 127 種以上語言的 OCR 識別。
Editing & Manipulation
Merging & Splitting ✓ Yes
`PdfMerger` .NET API 中的類別;官方範例討論了透過 PdfMerger 進行合併。
✓ Yes
透過直覺的 API,實現一行式合併、分割、追加、前置和頁面重新排序。
Headers, Footers & Page Numbers ✓ Yes
PDF協會的清單證實了為現有PDF添加"頁碼"和類似功能的能力。
✓ Yes
基於 HTML 的頁首/頁腳,帶有自動頁碼、日期和自訂內容。
水印 ✓ Yes
PDF協會的清單明確包括"在現有PDF文件上新增浮水印"。
✓ Yes
`ApplyWatermark()` 支援 HTML/CSS——完全控制透明度、旋轉角度和位置。
Stamp Text & Images ✓ Yes
透過 iText 的 canvas 和 layout API 實現程式化內容投放。
✓ Yes
`TextStamper` 以及 Google Fonts、定位和頁面級控制的`ImageStamper` 。
編輯內容 ✓ Yes
iText 透過清理模組提供編輯註解支援。
✓ Yes
`RedactTextOnAllPages()` 永久刪除一行中的敏感文字。
Security & Compliance
Encryption & Passwords ✓ Yes
透過 iText 的安全 API 實現完全加密和權限控制。
✓ Yes
AES 加密、擁有者/使用者密碼、細粒度權限(列印、複製、註釋)。
數位簽名 ✓ Yes
專用數位簽章文件和簽章 API ( `PdfSigner` )。
✓ Yes
`PdfSignature` 支援 X509/PFX 證書。
PDF/A & PDF/UA Compliance ✓ Yes
文件涵蓋了創建 PDF/A 的內容,並解釋了其限制(從現有文件轉換不是自動的)。
✓ Yes
適用於企業環境的原生 PDF/A 歸檔和 PDF/UA 無障礙存取合規性。
Platform & Deployment
跨平台支援 ✓ Yes
.NET Standard 2.0 / .NET Framework 4.6.1 — 可在.NET 6+ 及更高版本的作業系統上運作。
✓ Yes
Windows、Linux、macOS、x64、x86、 .NET 6–10、Core、Standard 2.0+、Framework 4.6.2+。
伺服器/Docker/雲 ~ Complex
核心安裝需要多個軟體套件(iText + Bouncy Castle 適配器);附加元件(pdfHTML/pdfOCR)會增加進一步的依賴/合規步驟。
✓ Yes
Docker、Azure、AWS、IIS。官方 Docker 映像和部署指南。
安裝簡單 ~ Complex
核心安裝需要多個軟體套件(Bouncy Castle 轉接器);HTML/OCR 需要額外的插件,有時還需要原生依賴項。
✓ Simple
只需一條NuGet `Install-Package IronPdf`安裝。
Licensing & Support
許可模式 ~ Complex
雙重許可:AGPLv3(網路使用需揭露原始碼)或商業許可。 AGPL 對專有應用程式的限制可能較大。
✓ Commercial
永久授權。 30天全功能免費試用,無浮水印。
Commercial Support & SLA ✓ Yes
iText 網站的授權模式包括商業授權和支援協議。
✓ 24/5 Support
提供專屬工程支持,並保證服務等級協議 (SLA)——電子郵件、線上聊天、電話。
文件 ✓ Yes
提供安裝指南、知識庫文章和 API 參考(核心 + 附加元件)。
✓ Extensive
完整的 API 參考、100 多個操作指南、教學課程、程式碼範例、故障排除、影片。

數據來源於 iText 官方文件、PDF 協會列表和NuGet包參考資料。
iText7 功能強大,但 AGPL 授權較為複雜,且需要安裝多個軟體包,因此設定起來比較麻煩。
IronPDF提供全面覆蓋,設定更簡單—免費試用 30 天

對於從事 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 指向 PDF 輸出的 URL

從這裡可以看出,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-to-PDF 轉换和簡单的商业許可模式,而 iText 7 需要付费附加组件来進行 HTML-to-PDF 轉换,并遵循双重許可模式。IronPDF 以易用性著称,而 iText 7 提供高级 PDF 處理功能,但学习曲线更陡峭。

為什么我應該為我的 .NET 項目選择 IronPDF?

IronPDF 是快速且具成本效益的 PDF 實施的理想選择,其易用性、全面的文檔和內置的 HTML-to-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 用戶友好的原因是什么?

由于其簡单的 API、最低限度的設置要求和全面的文檔,IronPDF 被認為是用戶友好的,即使是有有限 PDF 生成经验的開發人员也能轻松實施。

使用 IronPDF 進行 HTML-to-PDF 轉换是否有额外费用?

没有,IronPDF 包含內置的 HTML-to-PDF 轉换能力,消除了對额外组件或费用的需求,这与某些需要付费附加组件的 PDF 庫不同。

IronPDF 的性能和速度如何比较?

IronPDF 针對性能和速度進行了优化,提供了快速可靠的 PDF 生成和處理,这對于處理大量 PDF 的應用程序至关重要。

Curtis Chau
技術作家

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

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我