在編輯PDF方面iTextSharp與IronPDF之間的比較
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.
在現代 .NET 應用程式中,建立和管理 PDF 文件是常見的需求——無論您是在生成報告、發票還是數位記錄。 開發人員通常會轉向第三方 PDF 函式庫來完成此任務,而 .NET 生態系統中兩個最受歡迎的選擇是IronPDF和iText(iText 的後繼者)。
每個函式庫都為不同的使用案例提供了強大的工具集。 但哪一個最適合從 C# 中的[位元組陣列](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/dd831853(v=vs.120)生成 PDF? 本文通過比較、程式碼範例和見解將其分解,幫助 .NET 開發人員做出正確的選擇。
無論您是在構建企業級應用程式還是小型內部工具,選擇合適的 PDF 函式庫都可以節省開發時間並確保穩健的輸出。 讓我們看看每個函式庫提供了什麼。
介紹 PDF 函式庫
PDF 函式庫的用途是什麼?
C# 中的 PDF 函式庫允許開發人員以程式化的方式生成、操作和讀取 PDF 文件。 它們服務於廣泛的使用案例,例如:
- 導出報告和發票
- 從 Web 表單生成動態內容
- 將 HTML 頁面或模板轉換為 PDF
- 為您的 PDF 文件新增視覺元素,如頁碼、圖表、圖像等
- 合併或拆分文件
- 數位簽署 PDF
它們在資料可攜性和遵守 PDF/A 等標準(如存檔或可存取性要求)方面也發揮著至關重要的作用。
iText 和 IronPDF:頂尖競爭者
在可用的 .NET PDF 函式庫中,iText 和 IronPDF 已成為領先的解決方案——各自擁有獨特的優勢:
- iText 是一個成熟的開源庫,基於 Java 的 iText,提供強大的 PDF 控制,但學習曲線陡峭且授權規範複雜。
- IronPDF 是一個現代的商業庫,專注於簡化、速度和 Web 整合,允許您直接將 HTML 和ASP.NET 視圖轉換為 PDF 文件。
為什麼選擇合適的函式庫很重要
選擇兩者之一不僅僅是偏好的問題——它影響生產力、維護、性能,甚至法律授權合規性。 要求快速周轉、頻繁格式更改或從 HTML 模板渲染 PDF 的項目受益於快速開發,而企業級應用程式可能優先考慮標準合規性和長期可維護性。
功能比較
iText for .NET(iText 的後繼者)
iText 是 iText 的官方繼任者,提供完全重新設計的架構。 它是一個強大且可擴展的函式庫,適合在法務、金融和政府等合規要求嚴格的行業中建立、編輯和驗證 PDF。 iText 套件包括對 PDF/A、PDF/UA、數位簽名、修訂和表單建立的支持。
雖然它仍然在 AGPL 授權下開源,但對於專有項目有商業授權可用。
iText 主要功能
- 現代 API,替代 iText 的舊結構
- 模塊化支持:HTML 到 PDF、PDF/A、表單、修訂、數位簽名
- 高性能企業級應用程式
- 非常適合 PDF/A、可存取性、合規性
安裝 (NuGet)
要下載用於 PDF 生成的 iText 核心包:
Install-Package itext7

您還可以通過方案螢幕上的包管理器安裝 iText。 要執行此操作,您首先需要轉到工具下拉選單,然後找到"NuGet Package Manager > Manage NuGet Packages for Solution"。

然後,簡單地搜索 iText,然後點擊"安裝"。

程式碼範例:使用 iText 從 Byte Array 建立 PDF 文件
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfWithIText7();
// Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfWithIText7()
{
using (var ms = new MemoryStream())
{
var writer = new PdfWriter(ms);
var pdf = new iText.Kernel.Pdf.PdfDocument(writer);
var doc = new Document(pdf);
doc.Add(new Paragraph("Hello from iText 7 for .NET!"));
doc.Close(); // Always close the document to finalize content
return ms.ToArray();
}
}
}
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfWithIText7();
// Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfWithIText7()
{
using (var ms = new MemoryStream())
{
var writer = new PdfWriter(ms);
var pdf = new iText.Kernel.Pdf.PdfDocument(writer);
var doc = new Document(pdf);
doc.Add(new Paragraph("Hello from iText 7 for .NET!"));
doc.Close(); // Always close the document to finalize content
return ms.ToArray();
}
}
}
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.GeneratePdfWithIText7()
' Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function GeneratePdfWithIText7() As Byte()
Using ms = New MemoryStream()
Dim writer = New PdfWriter(ms)
Dim pdf = New iText.Kernel.Pdf.PdfDocument(writer)
Dim doc = New Document(pdf)
doc.Add(New Paragraph("Hello from iText 7 for .NET!"))
doc.Close() ' Always close the document to finalize content
Return ms.ToArray()
End Using
End Function
End Class
輸出 PDF 文件

解釋
PdfWriter將內容寫入MemoryStream。PdfDocument管理 PDF 的內部結構。Document用於新增高層次的內容(文字、圖像、表格)。- 調用
doc.Close()後,PDF 內容被完整地寫入並準備作為位元組陣列返回。
此範例展示了 iText 的 更模塊化和可讀的 API 比較 iText。 然而,它仍然缺乏對 HTML/CSS 渲染的原生支持,除非您單獨包含 pdfhtml,這是經授權的。
iText 的優缺點
優點:
-
全面的 PDF 控制\ iText 提供對 PDF 元件的完全控制,如表格、表單和數位簽名。 這使得它非常適合需要特定 PDF 標準(如 PDF/A 或 PDF/UA)的合規性應用程式。
-
模塊化和可擴展\ iText 是模塊化的,這意味著您可以只安裝您需要的特定模塊(例如,用於 HTML 到 PDF 轉換的 pdfhtml)。 如果您不使用所有功能,這允許更輕量的實施。
-
支持複雜的 PDF 標準\ iText 支持 ISO 標準,如 PDF/A(存檔)、PDF/UA(可存取性)和 PDF/X(列印),使其適合於在合規性至關重要的專業和法律環境中使用。
-
豐富的文件和支持\ iText 擁有全面的文件和龐大的社群。 公司還提供專業支持,確保開發人員在需要時可以獲得幫助。
- 提供免費版本 (AGPL)\ 開發者可以免費使用 iText 在 AGPL 許可下,這非常適合開源項目或個人使用。
缺點:
-
AGPL 商業使用許可\ 雖然 iText 提供一個免費版本,商業使用者必須遵循 AGPL 許可,這要求發布任何使用 iText 的軟體的源程式碼或購買商業許可。
-
學習曲線陡峭\ iText 的 API 更加複雜和功能豐富,相對於像 IronPDF 這樣的簡單函式庫,它需要一個陡峭的學習過程。 開發人員需要熟悉其低級文件結構和模塊化架構。
-
對於簡單任務過於笨重\ iText 在基本的 PDF 任務上可能覺得笨重,如簡單的文件建立或基本的 HTML 到 PDF 轉換,相比像 IronPDF 這樣的函式庫簡化的過程。
- 需要外部模塊進行 HTML 到 PDF 轉換\ iText 中的 HTML 到 PDF 轉換僅在額外的 pdfhtml 模塊可用,它需要單獨的安裝,並且可能沒辦法像 IronPDF 那樣順暢地處理現代網頁內容。
IronPDF for .NET:一個強大的 PDF 函式庫
IronPDF 是一個高級 .NET 函式庫,旨在簡化 PDF 文件生成,專注於開發者的生產力。 它特別適合於呈現 HTML 內容和樣式,使其成為現代 Web 到 PDF 工作流程的理想選擇。
主要功能:
- 從位元組陣列建立 PDF 文件,並使用 PDF 文件而無需安裝 Adobe Reader
- 使用完整的 Chromium 引擎直接將 HTML 呈現為 PDF 文件,從 HTML 內容建立 PDF 文件
- 支持 MVC 視圖、Razor 頁面和本地/遠程 URL
- 支持圖像文件、JavaScript、CSS 以及響應式佈局開箱即用
- 易於使用的語法和簡單的設置
- 永久授權且無 AGPL 約束
安裝 IronPDF
IronPDF 也可以通過 NuGet 安裝,通過在 NuGet 包管理器控制台中運行以下命令:
Install-Package IronPdf

或者,您可以通過方案螢幕的 NuGet 包管理器安裝它。 要執行此操作,請導航到"工具 > NuGet Package Manager > Manage NuGet Packages for Solution"。

然後,搜索 IronPDF,然後點擊"安裝"。

安裝後,您可以在幾秒鐘內開始將完整的 HTML 頁面呈現為 PDF——不需要額外的模塊。 它支持現代 CSS、JavaScript,甚至不需要額外配置的互動式網頁內容。
程式碼範例:使用 IronPDF 從 Byte Array 建立 PDF 文件
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfWithIronPdf();
// Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfWithIronPdf()
{
var renderer = new ChromePdfRenderer();
var pdfDoc = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>");
return pdfDoc.BinaryData;
}
}
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfWithIronPdf();
// Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfWithIronPdf()
{
var renderer = new ChromePdfRenderer();
var pdfDoc = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>");
return pdfDoc.BinaryData;
}
}
Imports IronPdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.GeneratePdfWithIronPdf()
' Save the PDF to a file
File.WriteAllBytes("output.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function GeneratePdfWithIronPdf() As Byte()
Dim renderer = New ChromePdfRenderer()
Dim pdfDoc = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>")
Return pdfDoc.BinaryData
End Function
End Class
輸出 PDF 文件

解釋
using IronPdf聲明導入 IronPDF 函式庫以存取所有與 PDF 相關的類別。var renderer = new ChromePdfRenderer()建立了一個新的 HTML 到 PDF 渲染器,由無頭 Chromium 引擎提供支持。renderer.RenderHtmlAsPdf(...)將給定的 HTML 字串轉換為 PDF 文件。 您還可以傳遞文件路徑或 URL。pdfDoc.BinaryData返回最終的 PDF 作為位元組陣列,準備保存、串流或資料庫儲存。
IronPDF 的優缺點
優點:
-
輕鬆 HTML 到 PDF 呈現\ 直接將 HTML、CSS 和 JavaScript 內容渲染為帶有完整樣式的 PDF,包括 Bootstrap 和自訂字型——無需複雜的佈局程式碼或額外模塊。
-
快速入門和直觀的 API\ 僅需幾行程式碼即可建立完全樣式化的 PDF 文件,語法簡潔,完全相容 .NET Core 和 .NET Framework。
-
全面支持 Web 技術\ IronPDF 支持 JavaScript、現代 CSS、SVG 和媒體查詢——這是大多數函式庫難以實現的,除非它們使用像 Chromium 這樣的無頭瀏覽器(這是 IronPDF 內部所使用的)。
-
內建圖像和資產處理\ 輕鬆包含圖像、本地文件,甚至可以從遠程 URL 提取資產,而無需額外配置。
-
永久授權 & 無 AGPL\ 與 iText 不同,IronPDF 提供靈活的商業許可,而無需開放源程式碼 AGPL 義務的限制。
- 非常適合 MVC 和 Razor 視圖\ 無縫地將 ASP.NET 應用程式中的 .cshtml Razor 視圖轉換為可列印的 PDF。
缺點:
-
商業使用需許可\ 雖然有免費試用,但 IronPDF 並不是開源的。 預算緊張的項目可能需要評估許可成本。
- 較大的初始包大小\ 由於它捆綁了一個無頭 Chromium 引擎,NuGet 包蘊量比一些替代品重。
實用程式碼範例比較
以下本節中的程式碼範例展示這些函式庫在操作中,在此期間,我們將使用相同的任務來比較 IronPDF 和 iText。 這兩個函式庫將被放入相同的情景:從 URL 生成 PDF、將圖像渲染為 PDF 並將樣式化的 HTML 轉換為 PDF,同時使用位元組陣列處理我們的 PDF 內容。 這將允許開發人員評估每個函式庫如何處理這些常見的使用案例。
1. 使用 Byte Array 從 URL 生成簡單的 PDF
IronPDF
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfFromUrlWithIronPdf();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-from-url.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfFromUrlWithIronPdf()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitForJavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
return pdf.BinaryData;
}
}
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.GeneratePdfFromUrlWithIronPdf();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-from-url.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] GeneratePdfFromUrlWithIronPdf()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitForJavaScript(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
return pdf.BinaryData;
}
}
Imports IronPdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.GeneratePdfFromUrlWithIronPdf()
' Save the PDF to a file
File.WriteAllBytes("ironpdf-from-url.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function GeneratePdfFromUrlWithIronPdf() As Byte()
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.WaitForJavaScript(5000)
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Dim pdf = renderer.RenderUrlAsPdf("https://www.apple.com")
Return pdf.BinaryData
End Function
End Class
輸出 PDF

IronPDF 使用無頭 Chromium 引擎進行像素完美的渲染,提供完整的 JavaScript 和 CSS 支持。
iText
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Net.Http;
using System.Threading.Tasks;
using iText.Html2pdf;
using System.IO;
class Program
{
static async Task Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = await pdfGenerator.GeneratePdfFromUrlWithIText7Async();
// Save the PDF to a file
File.WriteAllBytes("itext7-from-url.pdf", pdfBytes);
}
}
class PdfGenerator
{
public async Task<byte[]> GeneratePdfFromUrlWithIText7Async()
{
using var httpClient = new HttpClient();
string html = await httpClient.GetStringAsync("https://www.apple.com");
using var stream = new MemoryStream();
HtmlConverter.ConvertToPdf(html, stream);
return stream.ToArray();
}
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Net.Http;
using System.Threading.Tasks;
using iText.Html2pdf;
using System.IO;
class Program
{
static async Task Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = await pdfGenerator.GeneratePdfFromUrlWithIText7Async();
// Save the PDF to a file
File.WriteAllBytes("itext7-from-url.pdf", pdfBytes);
}
}
class PdfGenerator
{
public async Task<byte[]> GeneratePdfFromUrlWithIText7Async()
{
using var httpClient = new HttpClient();
string html = await httpClient.GetStringAsync("https://www.apple.com");
using var stream = new MemoryStream();
HtmlConverter.ConvertToPdf(html, stream);
return stream.ToArray();
}
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports System.Net.Http
Imports System.Threading.Tasks
Imports iText.Html2pdf
Imports System.IO
Friend Class Program
Shared Async Function Main(ByVal args() As String) As Task
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = Await pdfGenerator.GeneratePdfFromUrlWithIText7Async()
' Save the PDF to a file
File.WriteAllBytes("itext7-from-url.pdf", pdfBytes)
End Function
End Class
Friend Class PdfGenerator
Public Async Function GeneratePdfFromUrlWithIText7Async() As Task(Of Byte())
Dim httpClient As New HttpClient()
Dim html As String = Await httpClient.GetStringAsync("https://www.apple.com")
Dim stream = New MemoryStream()
HtmlConverter.ConvertToPdf(html, stream)
Return stream.ToArray()
End Function
End Class
輸出

iText 使用 HttpClient 獲取原始 HTML 並使用 HtmlConverter 渲染它,但它不支持 JavaScript 執行(由 iText 的官方文件確認,建議使用 Selenium 或類似的瀏覽器自動化進行 JavaScript 預處理)並且CSS 樣式支持有限。 雖然 iText 在 7.1.15 版(2021年)中新增了部分 flexbox 支持,但許多 CSS3 屬性仍不受支持,特別是對於複雜的現代佈局。
2. 使用 Byte Array 從圖像建立新的 PDF 文件
IronPDF
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreatePdfWithImage();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-with-image.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreatePdfWithImage()
{
var pdf = ImageToPdfConverter.ImageToPdf("example.png");
return pdf.BinaryData;
}
}
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreatePdfWithImage();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-with-image.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreatePdfWithImage()
{
var pdf = ImageToPdfConverter.ImageToPdf("example.png");
return pdf.BinaryData;
}
}
Imports IronPdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.CreatePdfWithImage()
' Save the PDF to a file
File.WriteAllBytes("ironpdf-with-image.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function CreatePdfWithImage() As Byte()
Dim pdf = ImageToPdfConverter.ImageToPdf("example.png")
Return pdf.BinaryData
End Function
End Class
輸出
using IronPDF 的ImageToPdfConverter 工具輕鬆生成圖像到 PDF。 利用此工具,您可以輕鬆地從圖像(如 PNG 文件或 JPG)建立 PDF 文件。
iText
using iText.Kernel.Pdf;
using iText.Layout;
using iText.IO.Image;
using iText.Layout.Element;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreatePdfWithImage();
// Save the PDF to a file
File.WriteAllBytes("iText-with-image.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreatePdfWithImage()
{
using var ms = new MemoryStream();
using var writer = new PdfWriter(ms);
using var pdfDoc = new iText.Kernel.Pdf.PdfDocument(writer);
var document = new Document(pdfDoc);
var img = new Image(ImageDataFactory.Create("https://itextpdf.com/sites/default/files/2018-11/iText%207%20Product%20software%20-%20webimages_509x339px_V2_iText%207%20Core.png"));
document.Add(img);
document.Close();
return ms.ToArray();
}
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.IO.Image;
using iText.Layout.Element;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreatePdfWithImage();
// Save the PDF to a file
File.WriteAllBytes("iText-with-image.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreatePdfWithImage()
{
using var ms = new MemoryStream();
using var writer = new PdfWriter(ms);
using var pdfDoc = new iText.Kernel.Pdf.PdfDocument(writer);
var document = new Document(pdfDoc);
var img = new Image(ImageDataFactory.Create("https://itextpdf.com/sites/default/files/2018-11/iText%207%20Product%20software%20-%20webimages_509x339px_V2_iText%207%20Core.png"));
document.Add(img);
document.Close();
return ms.ToArray();
}
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.IO.Image
Imports iText.Layout.Element
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.CreatePdfWithImage()
' Save the PDF to a file
File.WriteAllBytes("iText-with-image.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function CreatePdfWithImage() As Byte()
Dim ms = New MemoryStream()
Dim writer = New PdfWriter(ms)
Dim pdfDoc = New iText.Kernel.Pdf.PdfDocument(writer)
Dim document As New Document(pdfDoc)
Dim img = New Image(ImageDataFactory.Create("https://itextpdf.com/sites/default/files/2018-11/iText%207%20Product%20software%20-%20webimages_509x339px_V2_iText%207%20Core.png"))
document.Add(img)
document.Close()
Return ms.ToArray()
End Function
End Class
輸出
using ImageDataFactory 手動建立文件佈局和顯式圖像插入。
3. 使用 Byte Array 將樣式化的 HTML 內容轉換為 PDF
IronPDF
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreateStyledPdf();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-styled-html.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreateStyledPdf()
{
string html = @"
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to IronPDF</h1>
<p>This is a simple PDF document generated using IronPDF.</p>
</body>
</html>";
var pdf = new ChromePdfRenderer().RenderHtmlAsPdf(html);
return pdf.BinaryData;
}
}
using IronPdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreateStyledPdf();
// Save the PDF to a file
File.WriteAllBytes("ironpdf-styled-html.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreateStyledPdf()
{
string html = @"
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to IronPDF</h1>
<p>This is a simple PDF document generated using IronPDF.</p>
</body>
</html>";
var pdf = new ChromePdfRenderer().RenderHtmlAsPdf(html);
return pdf.BinaryData;
}
}
Imports IronPdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.CreateStyledPdf()
' Save the PDF to a file
File.WriteAllBytes("ironpdf-styled-html.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function CreateStyledPdf() As Byte()
Dim html As String = "
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to IronPDF</h1>
<p>This is a simple PDF document generated using IronPDF.</p>
</body>
</html>"
Dim pdf = (New ChromePdfRenderer()).RenderHtmlAsPdf(html)
Return pdf.BinaryData
End Function
End Class
輸出

IronPDF 完全支持 CSS 標籤或外部樣式表,這要歸功於其 Chromium 引擎。
iText + pdfHTML
using iText.Kernel.Pdf;
using iText.Html2pdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreateStyledPdf();
// Save the new document to the specified file location
File.WriteAllBytes("iText-styled-html.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreateStyledPdf()
{
string html = @"
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to iText 7</h1>
<p>This is a simple PDF document generated using iText 7 and pdfHTML.</p>
</body>
</html>";
using var ms = new MemoryStream();
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), ms, properties);
return ms.ToArray();
}
}
using iText.Kernel.Pdf;
using iText.Html2pdf;
using System.IO;
class Program
{
static void Main(string[] args)
{
var pdfGenerator = new PdfGenerator();
byte[] pdfBytes = pdfGenerator.CreateStyledPdf();
// Save the new document to the specified file location
File.WriteAllBytes("iText-styled-html.pdf", pdfBytes);
}
}
class PdfGenerator
{
public byte[] CreateStyledPdf()
{
string html = @"
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to iText 7</h1>
<p>This is a simple PDF document generated using iText 7 and pdfHTML.</p>
</body>
</html>";
using var ms = new MemoryStream();
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), ms, properties);
return ms.ToArray();
}
}
Imports iText.Kernel.Pdf
Imports iText.Html2pdf
Imports System.IO
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim pdfGenerator As New PdfGenerator()
Dim pdfBytes() As Byte = pdfGenerator.CreateStyledPdf()
' Save the new document to the specified file location
File.WriteAllBytes("iText-styled-html.pdf", pdfBytes)
End Sub
End Class
Friend Class PdfGenerator
Public Function CreateStyledPdf() As Byte()
Dim html As String = "
<html>
<head>
<style>
body {
background-color: #f0f0f0;
margin: 20px;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: navy;
font-size: 32px;
text-align: center;
}
p {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Welcome to iText 7</h1>
<p>This is a simple PDF document generated using iText 7 and pdfHTML.</p>
</body>
</html>"
Dim ms = New MemoryStream()
Dim properties As New ConverterProperties()
HtmlConverter.ConvertToPdf(New MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)), ms, properties)
Return ms.ToArray()
End Function
End Class
輸出

需要安裝付費插件 pdfHTML 以處理 HTML 轉換任務。
比較總結
| 特點 | IronPDF | iText(含 pdfHTML) |
|---|---|---|
| 從 URL 渲染為 PDF | 完整的 Chromium 渲染 | 獲取 HTML,無原生 JS 支持 |
| 新增圖像 | 通過 HTML 嵌入或專用圖像印章工具 | 手動圖像工廠 |
| 渲染樣式化 HTML | 完整的 CSS 支持 | 只有通過 pdfHTML 支持 CSS |
| 回傳位元組陣列 | 有 | 有 |
| 設置複雜性 | 簡單 | 中等(手動佈局) |
| 輸出質量 | 像素完美 | 良好但靜態 |
結論:應選擇哪個 .NET 函式庫?
選擇IronPDF和iText取決於您的項目需求——但在開發者體驗、易用性和現代渲染準確性方面,IronPDF 明顯脫穎而出。
如果您在處理動態HTML 內容、網頁渲染,或需要建立 PDF 文件從 URL獲得完整的 JavaScript 和 CSS 支持,IronPDF 的 Chromium 引擎提供無與倫比的保真度。 其直觀的 API 和快速設置使其成為快速開發和實際生產使用的理想選擇——特別是在處理位元組陣列、文件流或基於 Web 的 PDF 生成時。
另一方面,iText 是一個強大且受尊重的函式庫,採用更傳統的、佈局驅動的方法。 它提供了對文件結構的堅實控制,非常適合需要細粒度操作的開發人員,但它需要一個更陡的學習過程,並且缺乏現代 HTML 渲染能力。
這是底線:
- 想要像素完美的來自現代網頁內容、樣式化 HTML 或快速原型的輸出? 選擇 IronPDF。
- 需要具有細粒度控制的低級 PDF 建立工具? iText 可能是合適的選擇。
準備好開始使用 IronPDF 嗎? 下載免費試用版,看看使用 C# 僅需幾行程式碼建立專業的基於位元組陣列的 PDF 有多簡單。
常見問題
如何在 C# 中將字節陣列轉換為 PDF?
您可以使用 IronPDF 將字節陣列轉換為 C# 中的 PDF。只需使用 `PdfDocument.FromBytes` 方法將字節陣列載入到 IronPDF 文件中,該方法將解析資料並生成 PDF 文件。
using IronPDF 進行 HTML 到 PDF 轉換有什麼好處?
IronPDF 在將 HTML 轉換為 PDF 方面表現出色,因為它使用無頭 Chromium 引擎,支持現代 CSS 和 JavaScript。這使其非常適合將動態網頁內容渲染為像素完美的 PDF 文件。
using IronPDF 生成 PDF 比使用 iText 7 有什麼主要優勢?
IronPDF 提供更簡單的 API 和更快速的專案設置,適合需要將 HTML 轉換為 PDF 的專案,並且完全支持 CSS 和 JavaScript。它特別適合需要快速開發和網頁內容整合的應用程式。
iText 7 如何處理 PDF 合規性?
iText 7 為合規性要求高的行業設計,支持 PDF/A、PDF/UA 和 PDF/X 等標準。它提供對 PDF 建立的強大控制,適合需要合規至關重要的應用程式。
將 IronPDF 安裝在 .NET 專案中的過程是什麼?
要安裝 IronPDF,您可以使用 Visual Studio 內的 NuGet 封裝管理器。在封裝管理控制台中運行命令 `Install-Package IronPdf` 以將其新增到您的專案。
IronPDF 能從 ASP.NET 視圖建立 PDF 嗎?
是的,IronPDF 可以將 ASP.NET 視圖直接呈現為 PDF 文件。此功能允許開發者輕鬆地將複雜佈局和樣式的網頁轉換為 PDF。
using IronPDF 的主要收益在哪些應用程式中最為明顯?
需要將動態網頁內容(如報告和發票)轉換為 PDF 的應用程式從使用 IronPDF 中獲得最多收益。其快速設置和網路技術支持使其非常適合需要頻繁更新和現代設計的專案。
iText 7 的模塊化架構如何影響其使用?
iText 7 的模塊化架構允許根據需要新增特定的 PDF 功能,例如 HTML 轉換或數位簽名。這提供靈活性,但可能需要學習和安裝每個模塊。
IronPDF 和 iText 7 之間在授權方面有何不同?
IronPDF 提供適合商業應用的永久授權,沒有 AGPL 的限制。相比之下,iText 7 在 AGPL 許可下可用於開源項目,商業用途需要付費授權。



