如何使 PDF 填写
将HTML转换为PDF是许多现代软件开发工作流程中的关键任务,无论是生成报告、发票,还是创建文档。 作为C#开发人员,您可以使用多种库来简化此过程。
在本文中,我们将比较.NET生态系统中两个最受欢迎的库:IronPDF和iText。 这两个库都提供了强大的功能,但它们在易用性、PDF创建工具、CSS样式支持和许可方式等关键领域有所不同。 无论您是初学者还是经验丰富的开发人员,本指南将帮助您了解它们的核心功能,并根据您的需求和项目要求决定哪个最适合您。
*想跟随一起吗? 下载IronPDF免费试用版,亲自探索IronPDF的强大功能。
比较顶级库:iText与IronPDF
iText和IronPDF都为开发者提供了所需的工具,以在C#中进行HTML到PDF的转换。 然而,每个都有其自身的优缺点。
-
iText是一个存在已久的开源库。它提供了灵活性和广泛的定制选项,但设置和使用起来可能有些复杂,特别是在处理高级HTML和CSS渲染时。 但需要注意的是,iText是一个遗留产品,现在仅接收与安全相关的更新。
-
另一方面,IronPDF是由Iron Software开发的商业产品。 它以用户友好的界面、强大的CSS支持和易用性而闻名。 它无缝集成到C#应用程序中,是需要快速高效生成PDF而不牺牲质量的开发人员的绝佳选择。
在深入研究如何使用这两个库将HTML转换为PDF格式之前,让我们先来看一个基本的示例比较,展示这两个库在处理含有大量CSS和JavaScript的网页/HTML内容时转换为PDF文档的差异。

从生成的PDF文档中可以看出,这表明iText只能处理提供的URL的原始HTML内容,而IronPDF能够保持原始CSS布局和样式,确保生成的PDF文档与原始网页非常接近。
逐步安装指南
设置iText
- 通过NuGet控制台安装:要开始使用iText,您可以直接从NuGet安装它。 打开您的Visual Studio项目,前往NuGet包管理器,运行以下命令:

- 通过NuGet包管理器安装:或者,您可以通过解决方案屏幕的NuGet包管理器安装。 为此,请导航到"工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包"。

然后,搜索iText库并单击"安装"。

- 添加引用:安装完成后,在您的项目中添加必要的引用,特别是那些与HTML到PDF转换相关的引用。
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparserImports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser设置IronPDF
- 通过NuGet控制台安装:要开始使用IronPDF,您可以直接从NuGet安装。 打开您的Visual Studio项目,前往NuGet包管理器,运行以下命令:
-
通过NuGet包管理器安装: 或者,您可以通过解决方案屏幕上的NuGet包管理器为iText安装它,与之前针对iText的步骤相同。 不过,这次要搜索IronPDF,然后单击"安装"。

-
添加引用:安装完成后,将IronPDF导入到您的项目中:
using IronPdf;using IronPdf;Imports IronPdfImports IronPdf需要记住的是,IronPDF在商业使用/开发环境之外使用时需要许可证密钥。
实现HTML到PDF转换
使用iText
一旦iText设置完毕,请确保您在项目中安装了itextsharp.xmlworker包,然后就可以开始从HTML内容创建PDF。 不过,面对更复杂的HTML时,尤其是涉及CSS样式时,您将面临挑战。 iText往往需要额外的努力来实现完美的样式对比IronPDF。 重要的是要记住,iText仅处理基础HTML/CSS2(不支持flexbox、grid和有限的CSS)。
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
// Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Define HTML content with CSS styling
string html = @"
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>";
// Create PDF from HTML content
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
using (StringReader sr = new StringReader(html))
{
// Use XMLWorkerHelper to parse and add HTML content to the PDF document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
// Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Define HTML content with CSS styling
string html = @"
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>";
// Create PDF from HTML content
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
using (StringReader sr = new StringReader(html))
{
// Use XMLWorkerHelper to parse and add HTML content to the PDF document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
' Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
' Define HTML content with CSS styling
Dim html As String = "
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>"
' Create PDF from HTML content
Using stream As New FileStream("report.pdf", FileMode.Create)
Dim pdfDoc As New Document(PageSize.A4, 25, 25, 30, 30)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
Using sr As New StringReader(html)
' Use XMLWorkerHelper to parse and add HTML content to the PDF document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
End Using
pdfDoc.Close()
writer.Close()
End Using
Console.WriteLine("PDF generation completed successfully.")Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
' Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
' Define HTML content with CSS styling
Dim html As String = "
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>"
' Create PDF from HTML content
Using stream As New FileStream("report.pdf", FileMode.Create)
Dim pdfDoc As New Document(PageSize.A4, 25, 25, 30, 30)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
Using sr As New StringReader(html)
' Use XMLWorkerHelper to parse and add HTML content to the PDF document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
End Using
pdfDoc.Close()
writer.Close()
End Using
Console.WriteLine("PDF generation completed successfully.")输出PDF文件

使用 IronPDF.
IronPDF使PDF生成过程简化,只需几行代码即可轻松将HTML内容转换为新的PDF文档,如下面的代码示例所示。 它能够处理使用CSS文件进行样式化的高级HTML文档、HTML字符串以及CSS/JavaScript密集的网页内容。
这是一个简单的包含内联CSS的示例:
using IronPdf;
class Program
{
static void Main()
{
// Define HTML content with inline CSS styling
var content = @"
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>";
// Use ChromePdfRenderer to render the HTML content as a PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}using IronPdf;
class Program
{
static void Main()
{
// Define HTML content with inline CSS styling
var content = @"
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>";
// Use ChromePdfRenderer to render the HTML content as a PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(content);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Define HTML content with inline CSS styling
Dim content = "
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>"
' Use ChromePdfRenderer to render the HTML content as a PDF
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(content)
pdf.SaveAs("AdvancedStyledReport.pdf")
End Sub
End ClassImports IronPdf
Friend Class Program
Shared Sub Main()
' Define HTML content with inline CSS styling
Dim content = "
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>"
' Use ChromePdfRenderer to render the HTML content as a PDF
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(content)
pdf.SaveAs("AdvancedStyledReport.pdf")
End Sub
End Class输出PDF文件

通过IronPDF,您可以期待拥有精美的PDF文档,并具有准确的CSS样式,如上面的代码示例所示,这使其成为许多开发人员处理复杂HTML文件、字符串等的首选工具。 除了简单的HTML到PDF文档转换任务之外,IronPDF还能够处理高级PDF操作任务和PDF安全。 这使其成为一个优秀的一体化PDF库。
关键差异和竞争格局
在评估iText和IronPDF时,不仅要考虑它们的功能,还要考虑竞争态势。 其他竞争对手如Apryse和Aspose.PDF也提供类似的HTML到PDF解决方案,但在定价和功能上都有各自的权衡。
| 功能 | IronPDF | iText | Apryse | Aspose.PDF |
|---|---|---|---|---|
| 易用性 | 高 | 中等的 | 高 | 中等的 |
| CSS支持 | Full | 部分 | Full | 满的 |
| 许可 | Commercial | 开源 | Commercial | 商业的 |
| 支持 | 优秀 | 社区 | Premium | 优质的 |
| 定价 | 来自$999 | 免费/商业许可 | 基于报价 | 每年1679美元起 |
IronPDF因其对现代HTML5和CSS3的全面支持而脱颖而出,这对今天大多数开发人员来说至关重要。 它对PDF文档的操作扩展支持、对PDF文档创建的控制、转换过程的便捷性等,使IronPDF成为受欢迎的PDF库。
结论和建议
总之,无论IronPDF还是iText都提供了强大的能力进行C#中的HTML到PDF的转换,但它们满足于不同类型的开发者。 如果您在寻找开源的解决方案并且希望有强大的社区支持,那么iText可能是正确的选择。 然而,对于需要易用性、强大的CSS支持和商业解决方案的开发者来说,IronPDF提供了更简化和功能丰富的体验。 无论您是在寻找一个可以自动生成发票、创建品牌PDF文档,还是将整个网页转换为PDF文件的工具,IronPDF都能为您提供支持。
立即尝试IronPDF的用户友好功能——下载免费试用版,亲身体验HTML到PDF转换有多么简单。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章
