如何使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 Package Manager > Manage NuGet Packages for Solution"。

然後,搜索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所做的那樣。 不過,這次請搜索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字串和CSS/JavaScript的網頁內容的高階HTML文件。
這是一個包含內聯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起 | 免費/商業授權 | 基於報價 | 每年1,679美元起 |
IronPDF因其對現代HTML5和CSS3的全面支持而脫穎而出,這對於今天的大多數開發人員來說至關重要。 它對PDF文件的操作擴展支持、對PDF文件建立的控制、簡化的轉換過程及更多,令IronPDF成為熱門的PDF程式庫。
結論和建議
總之,IronPDF和iText都為C#中的HTML到PDF轉換提供了強大的功能,但它們滿足不同型別開發人員的需求。 如果您尋求一個開源解決方案並有強大的社群支持,iText可能是正確的選擇。然而,對於需要使用便利性、強大的CSS支持和商業解決方案的開發者而言,IronPDF提供了更流暢和功能豐富的體驗。 無論您是在尋找一個可以自動生成發票的工具,建立品牌PDF文件,還是將整個網頁轉換為PDF文件,IronPDF都能滿足您的需求。
立即試用IronPDF的使用者友好功能——下載免費試用版親自體驗,看看HTML到PDF的轉換有多麼簡單。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。