在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
將 HTML 轉換為 PDF 是許多現代軟件開發工作流中的重要任務,無論是用於生成報告、發票還是創建文檔。 作為 C# 開發人員,您可以使用多個程式庫來簡化此過程。
在本文中,我們將比較 .NET 生態系統中最受歡迎的兩個程式庫:IronPDF 和 iTextSharp。 兩個庫都提供強大的功能,但它們在關鍵領域存在差異,如易用性、PDF 創建工具、CSS 樣式支持和授權。 無論您是初學者還是經驗豐富的開發人員,本指南將幫助您了解它們的核心功能,並根據您的需求和專案要求決定哪一個最適合您。
想一起跟隨嗎? 下載IronPDF 免費試用版,親自探索 IronPDF 的強大功能。
無論是iTextSharp還是IronPDF,都為開發人員提供所需的工具,用於在 C# 中進行 HTML 到 PDF 的轉換。 然而,每個都有其自身的優勢和劣勢。
IronPDF,另一方面,是由 Iron Software 開發的商業產品。 它以用户友好的界面、强大的 CSS 支援和易于使用而闻名。 它可以無縫整合到 C# 應用程式中,是開發人員在不犧牲質量的前提下,需要快速且高效 PDF 生成的絕佳選擇。
在我們深入了解如何使用這兩個庫將 HTML 轉換為 PDF 格式之前,讓我們先看看一個基本的示例比較,這示範了這兩個庫在處理將 CSS 和 JavaScript 繁重的網頁/HTML 內容轉換為 PDF 文件的差異。
從生成的 PDF 文件可以看出,這表明 iTextSharp 只能處理提供的 URL 的原始 HTML 內容,而 IronPDF 能夠保持原始的 CSS 佈局和樣式,確保生成的 PDF 文件與原始網頁高度相似。
Install-Package iTextSharp
Install-Package iTextSharp
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package iTextSharp
透過 NuGet 套件管理器安裝:或者,您可以透過方案螢幕上的 NuGet 套件管理器安裝它。 要執行此操作,請導航至「工具 > NuGet 套件管理員 > 為解決方案管理 NuGet 套件」。
然後,搜尋 iTextSharp 庫並點擊「安裝」。
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
通過 NuGet 套件管理器安裝:或者,您可以在解決方案的 NuGet 套件管理器中安裝,就像我們在上面的步驟中為 iTextSharp 所做的那樣。不過這次,在點擊 "Install" 之前,請搜索 IronPDF。
using IronPdf;
using IronPdf;
Imports IronPdf
重要的是要記住,IronPDF 用於商業用途/在開發之外使用時需要授權金鑰。
一旦設置好 iTextSharp,你還需要確保在項目中安裝了 itextsharp.xmlworker 套件,然後才能開始從 HTML 內容創建 PDF。 然而,當涉及到更複雜的 HTML 尤其是包含 CSS 樣式時,您將面臨挑戰。 iTextSharp 與 IronPDF 相比,往往需要額外的精力來達到完美的樣式。 重要的是要記住,iTextSharp 僅處理基本 HTML/CSS2(不支持 flexbox、不支持 grid、有限的 CSS)。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
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>";
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))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
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>";
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))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)
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>"
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)
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
End Using
pdfDoc.Close()
writer.Close()
End Using
Console.WriteLine("PDF generation completed successfully.")
輸出 PDF 文件
IronPDF 使 PDF 生成過程變得簡單,只需幾行代碼即可輕鬆將 HTML 內容轉換為新的 PDF 文件,如下面的代碼示例所示。 它能夠處理使用 CSS 文件進行樣式設計的高級 HTML 文檔、HTML 字串以及大量使用 CSS/JavaScript 的網頁內容。
這是一個包含內聯 CSS 的簡單範例:
using IronPdf;
class Program
{
static void Main()
{
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>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
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>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
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>"
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("AdvancedStyledReport.pdf")
End Sub
End Class
輸出 PDF 文件
使用 IronPDF,你可以期待製作出一個擁有精確 CSS 樣式的精美 PDF 檔案,如上述程式碼範例所示,這使其成為處理複雜 HTML 檔案、字串等的許多開發者的首選工具。 除了簡單的 HTML 到 PDF 文件轉換任務外,IronPDF 還能執行高級 PDF 操作任務和 PDF 安全性。 這使得它成為一個出色的全合一 PDF 程式庫。
在評估 iTextSharp 和 IronPDF 時,不僅要考慮它們的功能,還要考慮競爭環境。 其他競爭對手如Apryse和Aspose.PDF也提供類似的HTML轉PDF解決方案,但在定價和功能上各有取捨。
功能 IronPDF iTextSharp Apryse Aspose.PDF
易於使用 高 中等 高 中等
CSS 支援 完整 部分 完整 完整
授權 商業 開源 商業 商業
支援 優秀 社群 高級版 高級
定價 從 $749 起 免費/商業許可證 基於報價 每年從 $1,679 起
IronPDF 因為全面支援現代HTML5和CSS3而脫穎而出,這對於當今大多數開發者來說至關重要。 由於對 PDF 文件操作的擴展支持、對 PDF 文件創建的控制、轉換過程的簡便性等,IronPDF 成為受歡迎的 PDF 庫。
總之,IronPDF 和 iTextSharp 都具備將 HTML 轉換為 C# 中 PDF 的強大功能,但它們滿足不同類型開發者的需求。 如果您正在尋找具有強大社區支持的開源解決方案,iTextSharp 可能是合適的選擇。然而,對於需要易用性、強大 CSS 支持和商業解決方案的開發者來說,IronPDF 提供了更精簡且功能豐富的體驗。 無論您是在尋找能自動生成發票、創建品牌PDF文件,還是將整個網頁轉換為PDF文件的工具,IronPDF都能滿足您的需求。
立即嘗試IronPDF的用户友好功能——下載免費試用版,親身體驗HTML到PDF轉換的簡單。