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到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转换,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 文件传递给 RenderHtmlFileAsPdf() 方法,该方法会将 HTML 渲染成 PDF,并将其存储在 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 实例。 该方法使用 RenderUrlAsPdf 将 URL 内容渲染成 PDF 格式后,会将生成的 PDF 保存到新的 PdfDocument 对象中,然后再使用 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 文档进行签名并保存。
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
输出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文档并保存。 如您在输出图片中所见,与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
输出PDF

如您所见,虽然iText7能够对PDF文档进行数字签名,但过程比IronPDF复杂得多。 这段代码加载PFX证书并使用它来数字签名PDF。 它提取私钥和证书,设置签名者,并向PDF添加一个独立的签名,然后保存签名的文档。
竞争分析
性能和可用性
IronPDF的关键优势
IronPDF因其易用性和高性能优化而闻名,提供更简单的API,使HTML到PDF转换等常见任务变得更简单。 它提供了抽象复杂PDF处理任务的高级方法,使开发人员可以以最少的代码生成、编辑和处理PDF。 对于多线程或大规模文档处理,IronPDF支持并行执行。
- 易于使用的API —— 高级方法简化了HTML到PDF转换等常见任务。
- 所需设置较少 —— 轻松集成到.NET项目中。
- 内置并行执行 —— 为处理大量PDF生成和转换优化。
- 简化的API —— 使用较少代码行即可实现结果。
iText 7 - 强大但复杂
iText 7,另一方面,提供更详细和细粒度的控制,这对于需要大量自定义的开发人员是一个优势。 它为处理低级PDF操作提供了一个强大的API。 然而,由于其复杂性,iText 7通常需要更多代码才能实现类似于IronPDF的结果。
- 细粒度的PDF控制 —— 适用于需要严格合规(如PDF/A、PDF/UA、数字签名)的企业应用程序。
- 高度自定义 —— 为高级用例提供低级PDF操作。
- 学习曲线更陡峭 —— 比IronPDF所需更多设置和配置。
- 代码密集程度更高 —— 常见任务通常需要更长的实现。
许可和成本
在为.NET项目选择PDF库时,许可和成本考虑是关键。 IronPDF和iText 7均遵循不同的许可模式,选择合适的一个取决于您的项目要求、预算和合规性需求。
IronPDF许可和成本
IronPDF遵循商业许可模式,这意味着尽管它为商业许可提供了免费试用,并且在开发和评估中是免费的,但全生产使用需要购买许可。 定价透明,取决于诸如使用规模、开发人员数量和项目类型等因素。
IronPDF许可:
- 商业许可模式(无开源限制)。
- 基于开发人员或团队许可证的简明定价。
- HTML到PDF、PDF安全或其他核心功能无额外成本。
- 最适合那些需要简单而高效成本的PDF解决方案的企业。
总结:IronPDF在一个许可证中包含所有主要功能,使其成为团队和企业的经济实用选择。
iText7许可和成本
iText7在双重许可模型下运行,其中包括:
- AGPL(GNU Affero通用公共许可证) —— 对于开源项目是免费的,但是要求使用iText7的整个项目是开源的且符合AGPL标准。 这意味着对项目所做的任何修改或增加也必须公开共享。
- 商业许可证 —— 需要用于任何不希望公开其源代码的专有软件或商业应用。 定价结构取决于使用、支持级别和部署规模。
对于希望将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的优势
- 易用性: IronPDF专注于简化,使其成为需要快速将PDF功能集成到.NET应用程序中的开发人员的理想选择。 其直观的API缩短了学习曲线,加速了开发时间。
- 全面的文档和支持: IronPDF提供了详细的文档和响应迅速的客户支持,确保开发人员可以快速上手,并有效地解决问题。
- 无缝的HTML到PDF转换: 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,特别是从网络内容,并且您正在寻找一个易于使用的解决方案,IronPDF可能是更好的选择。然而,如果您的应用程序需要高级PDF操控或严格遵守PDF标准,并且您需要灵活地进行大量自定义,iText7可能更合适。 考虑您的项目的具体需求和许可限制以确定最适合的库。
立即尝试IronPDF:下载免费试用版,开始探索IronPDF的强大功能吧!
[{i:(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 的应用程序至关重要。


