Syncfusion PDF Viewer与IronPDF:哪个C# PDF库提供更好的HTML到PDF转换?
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.
IronPDF 与 iText / iText
全面的 .NET PDF 库比较——逐项功能对比,并提供基于事实的上下文分析
| 特征 | iText / iText | IronPDF ✦ |
|---|---|---|
| PDF 创建与转换 | ||
| 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 文档,保留其结构和格式。 |
| 阅读与提取 | ||
| 文本提取 |
✓ 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 识别。
|
| 编辑与处理 | ||
| 合并与拆分 |
✓ Yes
PdfMerger .NET API 中的类;官方示例讨论了通过 PdfMerger 进行合并。
|
✓ Yes
通过直观的 API,实现一行式合并、拆分、追加、前置和页面重新排序。
|
| 页眉、页脚和页码 |
✓ Yes
PDF协会的列表证实了向现有PDF添加"页码"和类似功能的能力。
|
✓ Yes
基于 HTML 的页眉/页脚,带有自动页码、日期和自定义内容。
|
| 水印 |
✓ Yes
PDF协会的列表明确包括"在现有PDF文档上添加水印"。
|
✓ Yes
ApplyWatermark() 支持 HTML/CSS——完全控制透明度、旋转角度和位置。 |
| 添加文字和图像 |
✓ Yes
通过 iText 的 canvas 和 layout API 实现程序化内容投放。
|
✓ Yes
TextStamper 以及带有 Google Fonts、定位和页面级控制的ImageStamper 。 |
| 编辑内容 |
✓ Yes
iText 通过清理模块提供编辑注释支持。
|
✓ Yes
RedactTextOnAllPages() 永久删除一行中的敏感文本。
|
| 安全性与合规性 | ||
| 加密与密码 |
✓ Yes
通过 iText 的安全 API 实现完全加密和权限控制。
|
✓ Yes
AES 加密、所有者/用户密码、细粒度权限(打印、复制、注释)。
|
| 数字签名 |
✓ Yes
专用数字签名文档和签名 API (
PdfSigner )。 |
✓ Yes
PdfSignature 支持 X509/PFX 证书。 |
| PDF/A 和 PDF/UA 合规性 |
✓ Yes
文档涵盖了创建 PDF/A 的内容,并解释了其限制(从现有文件转换不是自动的)。
|
✓ Yes
适用于企业环境的原生 PDF/A 归档和 PDF/UA 无障碍访问合规性。
|
| 平台与部署 | ||
| 跨平台支持 |
✓ 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安装。 |
| 许可与支持 | ||
| 许可模式 |
~ Complex
双重许可:AGPLv3(网络使用需披露源代码)或商业许可。AGPL 对专有应用程序的限制可能较大。
|
✓ Commercial
永久授权。30天全功能免费试用,无水印。
|
| 商业支持和服务水平协议 |
✓ Yes
iText 网站的许可模式包括商业许可和支持协议。
|
✓ 24/5 Support
提供专属工程支持,并保证服务水平协议 (SLA)——电子邮件、在线聊天、电话。
|
| 文档 |
✓ Yes
提供安装指南、知识库文章和 API 参考(核心 + 附加组件)。
|
✓ Extensive
完整的 API 参考、100 多个操作指南、教程、代码示例、故障排除、视频。
|
数据源自官方 iText 文档、PDF 协会列表和 NuGet 包引用。
iText 虽然强大,但携带 AGPL 许可证的复杂性和多包安装开销。
IronPDF 提供简单安装的完整覆盖——试用免费 30
天。
对于处理PDF的开发人员来说,拥有一个可靠的PDF生成和操作库至关重要。 在.NET生态系统中,可能有十几个C# PDF库可供选择,那么你如何选择最符合您需求的一个呢?
为.NET应用程序选择适当的库对于有效的PDF功能开发至关重要。 本文提供了两个著名C# PDF库:IronPDF和iText(以前称为iText)之间的详细比较。 我们将探讨它们的功能、性能、许可以及适合各种项目要求的情况,以帮助您做出明智的决策。
为什么选择.NET PDF库?
PDF在报告、发票和法律文件中广泛使用,使得PDF生成和操作对于许多应用程序至关重要。 在选择库时,需要考虑的关键因素包括:
- 集成的容易性 —— 您能以多快的速度实现PDF功能?
- 支持HTML到PDF —— 是否允许从网页内容轻松转换?
- 许可和成本 —— 它是免费的吗,还是需要商业许可?
- 功能集 —— 是否支持文本提取、签名或编辑?
- 性能 —— 它生成或处理PDF的速度如何?
IronPDF和iText概述
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表单并提取表单数据。
最佳适用人群:寻求无需额外插件或复杂许可的简便一体化解决方案的开发人员。
iText简介
iText是一个强大且灵活的PDF库,提供广泛的PDF操作功能,包括文档创建、加密和签名。 然而,其核心库并不原生支持HTML到PDF的转换。
主要功能
- 低级别PDF自定义 —— 提供PDF结构、元数据和渲染的详细控制。
- 可访问性与合规性:生成PDF/A、PDF/UA和PDF/X,以实现长期存档和合规性。
- HTML到PDF转换:付费的pdfHTML插件可以将HTML内容转换为PDF。
- Java & .NET支持:主要为Java设计,并通过iText for .NET支持C#。
- PDF表单管理:创建和编辑AcroForms和XFA表单以进行交互式PDF表单操作。
最佳适用人群:需要高度可定制的PDF解决方案并愿意购买额外插件以获得扩展功能的开发人员。
功能和优势
在我们深入介绍功能及其代码示例之前,先来看看IronPDF和iText之间最大的功能差异之一,HTML到PDF转换。
- IronPDF原生支持HTML、CSS和JavaScript渲染,无需额外组件。
- iText,另一方面,需要pdfHTML附加组件,这是一项受商业许可约束的付费功能。 这增加了需要网页到PDF功能的开发人员的成本。

总结:如果您需要HTML到PDF转换,IronPDF是更具成本效益的解决方案,因为它提供了此功能开箱即用。
IronPDF主要功能(附代码示例)
IronPDF拥有丰富的功能集,用于处理PDF文档。 这些功能范围从PDF创建到PDF操作和安全性。 为了更清楚地了解此库提供的广泛功能,我们将研究一些关键功能。
HTML 至 PDF 转换
将HTML内容转换为高质量的PDF文档,IronPDF拥有强大的渲染引擎。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文件传递给PdfDocument对象中。 最后,我们将PDF保存到指定的文件位置。
URL到PDF
对于需要将URL内容转换为PDF的开发人员,请选择IronPDF。 通过此库,您将能够创建像素完美的PDF文档,通过使用ChromePdfRenderer渲染引擎,当它渲染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
PDF输出:

就像我们的HTML到PDF示例中一样,使用IronPDF将任何URL转换为PDF的第一步是首先创建一个新的ChromePdfRenderer实例。 一旦该方法已将URL内容渲染为PDF格式,使用SaveAs方法保存PDF。
PDF签名
通过在PDF文档上应用数字签名来确保您的PDF文档的真实性。 Developer可能会考虑不同的方法来应用数字签名,例如使用安全证书对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文档。
iText关键功能(包含代码示例)
iText提供了一系列功能,用于定制和增强您的PDF文档。 拥有针对各种PDF标准的广泛格式支持和高级PDF处理,这个PDF库功能丰富。 然而,如前所述,iText可能需要其他包来执行某些与PDF相关的任务,例如HTML到PDF。
HTML 至 PDF 转换
虽然iText本身无法处理HTML到PDF转换,但我们可以利用iText的商业许可下的付费附加组件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
现在,是时候比较iText在将URL转换为PDF时与IronPDF的表现如何了。 为此,我们将使用之前完全相同的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

如这里所示,iText将URL转换为PDF的方法更为手动和复杂。 首先,我们需要从URL下载HTML内容,然后按照HTML到PDF示例中的类似步骤将URL内容渲染为PDF文档并保存。 正如您在输出图像中看到的,iText无法保持许多原始样式和布局,而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

正如您所见,虽然iText能够对PDF文档进行数字签名,但该过程比IronPDF复杂得多。 这段代码加载PFX证书并使用它来数字签名PDF。 它提取私钥和证书,设置签名者,并向PDF添加一个独立的签名,然后保存签名的文档。
竞争分析
性能和可用性
IronPDF的关键优势
IronPDF因其易用性和高性能优化而闻名,提供更简单的API,使HTML到PDF转换等常见任务变得更简单。 它提供了抽象复杂PDF处理任务的高级方法,使开发人员可以以最少的代码生成、编辑和处理PDF。 对于多线程或大规模文档处理,IronPDF支持并行执行。
- 易于使用的API —— 高级方法简化了HTML到PDF转换等常见任务。
- 所需设置较少 —— 轻松集成到.NET项目中。
- 内置并行执行 —— 为处理大量PDF生成和转换优化。
- 简化的API —— 使用较少代码行即可实现结果。
iText - 强大但复杂
另一方面,iText提供了更详细和细粒度的控制,这对于需要大量定制的开发者来说是有利的。 它为处理低级PDF操作提供了一个强大的API。 然而,由于其复杂性,与IronPDF相比,iText通常需要更多的代码来实现类似的结果。
- 细粒度的PDF控制 —— 适用于需要严格合规(如PDF/A、PDF/UA、数字签名)的企业应用程序。
- 高度自定义 —— 为高级用例提供低级PDF操作。
- 学习曲线更陡峭 —— 比IronPDF所需更多设置和配置。
- 代码密集程度更高 —— 常见任务通常需要更长的实现。
许可和成本
在为.NET项目选择PDF库时,许可和成本考虑是关键。 无论是IronPDF还是iText,它们采用不同的许可模式,选择合适的模式取决于项目的要求、预算和合规需求。
IronPDF许可和成本
IronPDF遵循商业许可模式,这意味着尽管它为商业许可提供了免费试用,并且在开发和评估中是免费的,但全生产使用需要购买许可。 定价透明,取决于诸如使用规模、开发人员数量和项目类型等因素。
IronPDF许可:
- 商业许可模式(无开源限制)。
- 基于开发人员或团队许可证的简明定价。
- HTML到PDF、PDF安全或其他核心功能无额外成本。
- 最适合那些需要简单而高效成本的PDF解决方案的企业。
总结:IronPDF在一个许可证中包含所有主要功能,使其成为团队和企业的经济实用选择。
iText许可和成本
iText采用双重许可模式,包括:
- AGPL(GNU Affero通用公共许可证) – 免费用于开源项目,但要求使用iText的整个项目开源并符合AGPL。 这意味着对项目所做的任何修改或增加也必须公开共享。
- 商业许可证 —— 需要用于任何不希望公开其源代码的专有软件或商业应用。 定价结构取决于使用、支持级别和部署规模。
对于希望将iText集成到专有软件中的公司,必须持有商业许可证。 成本可能很大,尤其是对于企业级解决方案,因为它是基于每个开发人员定价的,但它提供了专业支持并确保法律合规。
为何iText可能更昂贵:
- 每个开发人员的定价 —— 每个开发人员需要一个单独的许可证,增加了团队的总成本。
- 某些基本功能需要昂贵的附加组件:
- pdfHTML(付费)—— 需要用于HTML到PDF转换。
- pdfOCR(付费)—— 需要用于从图像中识别文本。
- pdfCalligraph(付费)—— 改善文本渲染和字体支持。
- pdfRender(付费)—— 添加PDF到图像转换。
- pdf2Data(付费)—— 从PDF中提取结构化数据。
- 当需要多个功能和开发人员时,企业成本可能迅速上升。
底线:如果您的团队需要多个开发者和关键功能(如HTML到PDF和OCR),iText可能比IronPDF要昂贵得多,而IronPDF无需额外成本即可提供这些功能。
使用案例场景
小型与企业项目
对于需要快速实施PDF功能且配置最少的中小型项目,IronPDF由于其用户友好的API和全面的功能集是一个引人注目的选择。 需要大量定制和遵循特定PDF标准的企业级项目可能会受益于iText的高级功能,然而,IronPDF也因其高性能、许可选项和可用性成为此级别工作的强有力竞争者。
学术及商业用途
在学术环境或开源项目中,iText的AGPL许可证允许免费使用,前提是项目的许可证兼容。 对于商业应用而言,IronPDF和iText都需要购买商业许可证。 建议查看每个库的许可条款,以确保符合您项目的目标。
结论
为您的.NET项目选择合适的PDF库是一个至关重要的决定,取决于易用性、功能集和许可需求等因素。 IronPDF和iText都提供强大的PDF功能,但它们迎合不同的需求。 除了这两个库,Aspose、Syncfusion 和 PDFSharp 等竞争者都提供了竞争性的.NET PDF库。 然而,IronPDF通过其易于使用的API、全面的功能集合和高性价比在PDF行业中始终处于领先地位。
下面,我们总结了为什么IronPDF是您所有.NET PDF库需求的绝佳选择的有力论据。

IronPDF的优势
- 易用性: IronPDF专注于简化,使其成为需要快速将PDF功能集成到.NET应用程序中的开发人员的理想选择。 其直观的API减少了学习曲线并加速了开发时间。
- 全面的文档和支持: IronPDF提供了详细的文档和响应迅速的客户支持,确保开发人员可以快速上手,并有效地解决问题。
- 无缝的HTML到PDF转换: IronPDF擅长将HTML,包括CSS和JavaScript,转换为高质量的PDF。 这对于需要从基于网络的内容动态生成PDF的项目尤其有利。
- 商业许可: 对于商业应用程序,IronPDF提供了灵活的许可选项,确保合规性,而没有可能附带开源许可的限制。
iText的限制
- 复杂性:虽然iText提供高级功能和可定制性,但其API对于新手开发者来说可能比较复杂。 这可能导致相比于IronPDF更简单的方法更长的开发时间。
- 商业使用的许可成本:iText的AGPL许可证要求任何衍生作品开源,除非您购买商业许可证。 对于无法符合AGPL条款的专有应用程序,这可能是一个限制。
- Java中心功能:虽然iText可用于.NET,但其根植于Java生态系统,有时对于C#开发人员来说可能感觉不太原生,尤其是在处理跨平台问题或与Java工具集成时。
最终思考
如果您的项目需要快速的PDF生成,尤其是从Web内容生成,并且您在寻找易于使用的解决方案,IronPDF可能是更好的选择。然而,如果您的应用程序需要高级PDF操作或严格遵守PDF标准,并且您需要灵活的定制能力,iText可能更合适。 考虑您的项目的具体需求和许可限制以确定最适合的库。
立即尝试IronPDF:下载免费试用版,开始探索IronPDF的强大功能吧!
常见问题解答
如何在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 生成经验有限的开发人员也易于实现。
using IronPDF 的 HTML 到 PDF 转换是否有额外费用?
没有,IronPDF 包含内置的 HTML 到 PDF 转换功能,消除了对额外组件或费用的需求,与某些其他 PDF 库需要付费插件的情况不同。
IronPDF 的性能和速度如何?
IronPDF 针对性能和速度进行了优化,提供快速可靠的 PDF 生成和处理,这对于处理大量 PDF 的应用程序至关重要。


