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.
IronPDF 與 iTextSharp / iText7 的對比
全面的 .NET PDF 庫比較——逐項功能對比,並提供基於事實的上下文分析
| 特點 | 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 負責佈置。
|
| URL 到 PDF |
$ 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、ARM。 .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 函式庫:IronPDF和 iText 7(以前稱為 iTextSharp)。 我們將探討它們的功能、效能、授權,以及是否適合各種專案需求,以協助您做出明智的決定。
為何選擇 .NET PDF 函式庫?
PDF 廣泛應用於 報表、發票和法律文件,因此 PDF 的產生和處理對許多應用程式來說是不可或缺的。 選擇資料庫時,需要考慮的關鍵因素包括
- 整合容易度 - 如何快速實作 PDF 功能?
- 支援 HTML-to-PDF - 是否可從網頁內容輕鬆轉換?
- 授權與成本 - 是免費的,還是需要商業授權?
- 功能集 - 是否支援文字擷取、簽署或編輯?
- Performance - 產生或處理 PDF 的速度有多快?
IronPDF 和 iText7 概述。
IronPDF 簡介
IronPDF 是專為 .NET 開發人員設計的商用 PDF 函式庫。 它簡化了 PDF 的產生、操作和轉換,使其成為 C# 應用程式中最容易使用的函式庫之一。
IronPDF 支援 .NET Core、.NET Framework 和 .NET Standard,确保在各种 .NET 环境中的兼容性。 其高度的跨平台相容性使其成為在不同應用程式環境下工作的團隊的理想選擇,並可與 Visual Studio 等 IDE 無縫整合。 除了 .NET 版本,IronPDF 適用於 Python 也有 Java、Python 和 Node.js 版本。
主要特色:
- Built-in HTML-to-PDF 支援 - 將 網頁、HTML、CSS 及 JavaScript 轉換為 PDF,無需額外的附加元件。
- PDF 編輯 - 透過新增文字、圖片、頁首及頁尾來修改現有的 PDF。
- PDF 安全性 - 加密 PDF、設定 密碼保護,以及管理檢視、列印或編輯的權限。
- Watermarking and Annotations - 可輕鬆將文字和影像 水印、戳記或註解套用至文件。
- Form Filling and Data Extraction - 以程式化方式填入互動式 PDF 表單,並擷取表單資料。
最適合: 開發人員正在尋找一個簡單直接的多合一解決方案,而不需要額外的附加元件或複雜的授權。
iText7 介紹
iText 7 是功能強大且靈活的 PDF 函式庫,提供廣泛的 PDF 操作功能,包括文件建立、加密和簽章。 然而,其核心函式庫並不支援 HTML 至 PDF 的原生轉換。
主要功能
- Low-Level PDF Customization - 提供對 PDF 結構、元資料和呈現的詳細控制。
- Accessibility & Compliance: 生成 PDF/A、PDF/UA 和 PDF/X,以實現长期存檔和可访問性合規性。
- HTML 至 PDF 轉換:付費的 pdfHTML 附加元件可將 HTML 內容轉換為 PDF。
- Java & .NET 支援:主要針對 Java 設計,並透過 iText 7 適用於 .NET 支援 C#。
- PDF 表單管理: 建立和編輯 AcroForms 和 XFA 表單,以建立互動式 PDF 表單。
最適合需要高度客製化的 PDF 解決方案,並願意購買額外附加元件以擴充功能的開發人員。
特點和優勢
在介紹功能及其附帶的程式碼範例之前,我們先來看看 IronPDF 與 iText 7 最大的功能差異之一,HTML 到 PDF 的轉換。
- IronPDF原生支援HTML、CSS和JavaScript呈現,不需要任何額外的元件。
- 另一方面,iText 7需要pdfHTML 附加元件,這是商業授權下的付費功能。 這會增加需要網頁轉 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
輸入 HTML
。
輸出 PDF

在這個程式碼範例中,我們首先建立了一個新的 ChromePdfRenderer 實例,這使我們能夠存取 IronPDF 用來將 HTML 渲染為 PDF 的強大渲染引擎。 然後,我們將 HTML 檔案傳遞給 RenderHtmlFileAsPdf() 方法,該方法會將 HTML 渲染成 PDF,並將其儲存在 PdfDocument 物件中。 最後,我們會將 PDF 儲存到指定的檔案位置。
URL 至 PDF
對於希望將 URL 內容轉換成 PDF 的開發人員而言,IronPDF 是最佳選擇。 有了這個函式庫,您就可以透過使用 ChromePdfRenderer 渲染引擎來建立像素完美的 PDF 文件,當它渲染 URL 至 PDF 時,會維持所有原始的樣式與版面設計。 在本範例中,我們將使用 this 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
PDF輸出:
。
就像我們的 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
輸出 PDF
。
在這個例子中,我們加載了證書對象,創建了簽名的可視化表示(在本例中是 IronPDF 圖像),並創建了一個新的 PdfSignature 對象,該對象處理 PDF 文件本身的簽名。 最後,我們使用 SignPdfFile 對 PDF 文件進行簽名並儲存。
如果您想探索 IronPDF 提供的更多功能,請務必查看其資訊豐富的 功能頁面,或 使用指南,其中包含每個功能的深入程式碼範例。
iText7 主要功能(附程式碼範例)
iText7 提供廣泛的功能,可自訂和強化您的 PDF 文件。 此 PDF 函式庫具備各種 PDF 標準的廣泛格式支援以及進階的 PDF 操作功能,其中包含許多內容。 然而,如前所述,iText7 可能需要額外的套件才能執行某些 PDF 相關工作,例如 HTML 轉 PDF。
HTML 至 PDF 轉換
雖然 iText7 本身無法處理 HTML 到 PDF 的轉換,但我們可以利用 iText7 商業授權下的付費附加元件 pdfHTML,將 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
輸出 PDF
。
在本範例中,我們載入 HTML 檔案,並指定檔案位置以儲存呈現的 PDF。 然後,使用 ConvertToPdf 方法,我們可以輕鬆地將 HTML 文件轉換為 PDF 文件。
URL 至 PDF
現在,是時候比較 iText7 與 IronPDF 在將 URL 轉換為 PDF 方面的優劣了。 為此,我們將使用與之前完全相同的 URL,以確保公平的比較。
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
輸出 PDF
。
從這裡可以看出,iText7 將 URL 轉換為 PDF 的方法較為手動且複雜。 首先,我們需要從 URL 下載 HTML 內容,然後再依照 HTML 轉 PDF 的範例中的類似步驟,將 URL 內容轉換成 PDF 文件並儲存。 正如您在輸出的圖像中所看到的,iText7 並無法像 IronPDF 一樣,保留許多原始的樣式與版面。
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
輸出 PDF !a href="/static-assets/pdf/blog/itextsharp/itextsharp-8.webp">iText7 數位簽章輸出。
如您所見,雖然 iText7 能夠對 PDF 文件進行數位簽章,但過程往往比 IronPDF 複雜得多。 此代碼載入 PFX 憑證,並使用它對 PDF 進行數位簽章。 它會擷取私密金鑰和憑證、設定簽署者,並將獨立簽章加入 PDF,然後儲存已簽署的文件。
競爭分析
效能與可用性
IronPDF 的主要優勢
IronPDF 以其易用性和優化高效能而聞名,它提供了更直接的 API,簡化了 HTML 轉換為 PDF 等常見工作。 它提供了抽象出複雜 PDF 處理任務的高階方法,讓開發人員可以用最少的程式碼來產生、編輯和處理 PDF。 對於多執行緒或大規模的文件處理,IronPDF 支援平行執行。
- Easy-to-use API - 高階方法可簡化 HTML 至 PDF 轉換等常見工作。
- Minimal setup required - 可輕鬆整合至 .NET 專案。
- 內建平行執行 - 優化處理大量 PDF 產生與轉換。
- 簡化的 API - 需要較少的程式碼行數才能達到效果。
iText 7 - 功能強大但複雜。
另一方面,iText 7 提供了更詳細、更細緻的控制層級,對於需要大量客製化的開發人員來說是有利的。 它提供了強大的 API,可用於處理低階 PDF 作業。 然而,由於其複雜性,與 IronPDF 相比,iText 7 通常需要更多的程式碼才能達到類似效果。
- Granular PDF 控制項 - 適合需要嚴格遵循規範 (例如 PDF/A、PDF/UA、數位簽署) 的 企業應用程式。
- 高度可客製化 - 為進階使用個案提供低階 PDF 操作。
- Steeper learning curve - 與 IronPDF 相比,需要更多的設定和配置。
- 較密集的程式碼 - 常見的工作通常需要較長的實作。
授權與成本
在為 .NET 專案選擇 PDF 函式庫時,授權與成本的考量至關重要。 IronPDF 和 iText 7 都遵循不同的授權模式,選擇合適的模式取決於您專案的要求、預算和合規需求。
IronPDF 授權與成本
IronPDF 採用商業授權模式,也就是說,儘管它提供免費試用的商業授權,而且開發和評估都是免費的,但若要完全投入生產使用,則必須取得付費授權。 定價透明,取決於使用規模、開發人員數量和專案類型等因素。
IronPDF 授權:
- 商業授權模式(無開放源碼限制)。
- 直接定價基於開發人員或 Team License。
- HTML-to-PDF、PDF 安全性或其他核心功能無需額外費用。
- 最適合需要簡單、具成本效益的 PDF 解決方案的企業。
底線:IronPDF 在單一 License 中包含所有主要功能,使其成為團隊和企業的高性價比選擇。
iText7 授權與成本
iText7 採用 雙授權模式,其中包括:
1.AGPL (GNU Affero General Public License) - 開放原始碼專案免費使用,但要求使用 iText7 的整個專案必須是開放原始碼且符合 AGPL。 這表示對專案所做的任何修改或新增內容也必須公開分享。
2.Commercial License - 任何不想公開原始碼的專屬軟體或商業應用程式都需要。 定價結構根據使用量、支援等級和部署規模而有所不同。
對於希望將 iText 7 整合至專屬軟體的公司而言,必須取得商業授權。 成本可能會很高,尤其是對於企業級解決方案而言,因為它是以每位開發人員為單位來定價,但它提供了獲得專業支援的機會,並確保符合法律規定。
為什麼 iText 7 可能比較昂貴:
- Per-developer pricing - 每位開發人員都需要獨立的 License,增加了 Team 的總成本。
*基本功能需要昂貴的附加元件:- pdfHTML(付費) - HTML轉PDF所需的軟體。
- pdfOCR (付費) - 需要從影像中識別文字。
- pdfCalligraph (Paid) - 改善文字呈現與字型支援。
- pdfRender (付費) - 加入 PDF 到圖片的轉換。
- pdf2Data (Paid) - 從 PDF 中萃取結構化資料。
- 當需要多種功能和開發人員時,企業成本會迅速增加。
底線:如果您的團隊需要多名開發人員以及 HTML-to-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 的優點
- 易用性: IronPDF 的設計著重於簡易性,使其成為需要快速將 PDF 功能整合至 .NET 應用程式的開發人員的理想選擇。 其直覺式 API 可降低學習曲線,加快開發時間。
- 全面的說明文件和支援: IronPDF 提供詳盡的說明文件和快速回應的客戶支援,確保開發人員能快速上手,並有效排除故障。
- Seamless HTML to PDF Conversion: IronPDF 擅長將 HTML(包括 CSS 和 JavaScript)轉換為高品質 PDF。 這對需要從網頁內容動態產生 PDF 的專案特別有利。
- 商業授權:對於商業應用程式,IronPDF 提供彈性的授權選項,可確保符合規範,而不會受到開放原始碼授權所帶來的限制。
iText 的限制
- 複雜性:儘管 iText 7 提供了先進的功能和自訂性,但其 API 對於剛開始處理 PDF 的開發人員而言可能相當複雜。 相較於 IronPDF 更直接的方式,這可能會導致一般任務的開發時間較長。
- 商業使用的授權費用: iText 7 的 AGPL 授權要求任何衍生作品都必須開放原始碼,除非您購買商業授權。 對於無法遵守 AGPL 條款的專屬應用程式而言,這可能是一種限制。
- 以 Java 為中心的功能:儘管 iText 7 適用於 .NET,但其植根於 Java 生態系統,有時可能會讓 C# 開發人員覺得不太原生,尤其是在處理跨平台問題或與基於 Java 的工具整合時。
最後的想法
如果您的專案需要快速產生 PDF,尤其是從網頁內容產生 PDF,而且您正在尋找易於使用的解決方案,IronPDF 可能是更好的選擇。但是,如果您的應用程式需要進階的 PDF 操作或嚴格遵守 PDF 標準,而且您需要彈性來進行廣泛的客製化,iText7 可能會是更適合的選擇。 考慮您專案的特定需求和授權限制,以決定最適合您需求的函式庫。
立即試用 IronPDF: 下載 免費試用版,開始探索 IronPDF 的強大功能!
常見問題解答
怎樣在 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 的應用程序至关重要。

