使用 iTextSharp 從位元組陣列建立 PDF(C# 比較 IronPDF)
在現代 .NET 應用程式中,建立和管理 PDF 文件是一項常見需求——無論是產生報告、發票還是數位記錄。 開發人員經常使用第三方 PDF 函式庫來完成這項任務,而 .NET 生態系統中最受歡迎的兩個選擇是IronPDF和iText 7 (iTextSharp 的繼任者)。
每個庫都針對不同的使用場景提供了一套強大的工具集。 但是,在 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 檔案。 它們適用於多種應用場景,例如:
- 匯出報表和發票
- 從網頁表單產生動態內容 將HTML頁面或範本轉換為PDF
- 在 PDF 檔案中添加視覺元素,例如頁碼、圖表、圖像等 合併或分割文檔
- 對PDF檔案進行數位簽名
它們在資料可攜性和符合 PDF/A 等歸檔或無障礙要求等標準方面也發揮著至關重要的作用。
iTextSharp 和 IronPDF:兩大競爭者
在現有的 .NET PDF 庫中, iTextSharp和IronPDF已成為領先的解決方案——它們各自具有獨特的優勢:
- iTextSharp是一個成熟的開源程式庫,基於 Java 的 iText,提供強大的 PDF 控制功能,但學習曲線陡峭,並且存在許可方面的限制。
- IronPDF是一款現代化的商業庫,專注於簡潔性、速度和 Web 集成,可讓您直接將 HTML 和ASP.NET視圖轉換為 PDF 文件。
為什麼選擇合適的圖書館很重要
在這兩者之間做出選擇不僅僅是個人偏好的問題——它還會影響生產力、維護、性能,甚至法律許可合規性。 對於需要快速週轉、頻繁格式變更或從 HTML 範本渲染 PDF 的項目,快速開發是有益的;而企業級應用程式則可能優先考慮標準合規性和長期可維護性。
功能對比
iText 7 for .NET(iTextSharp 的後續版本)
iText 7是iTextSharp的正式繼任者,並提供了完全重新設計的架構。 這是一個功能強大、可擴展的程式庫,適用於在法律、金融和政府等合規性要求高的行業中建立、編輯和驗證 PDF 文件。 iText 7 套件包括對 PDF/A、PDF/UA、數位簽章、編輯和表單建立的支援。
雖然它仍然是根據 AGPL 許可證開源的,但專有專案可以獲得商業許可。
iText 7 主要功能
- 現代化的 API,取代了 iTextSharp 的舊結構
- 模組化支援:HTML 轉 PDF、PDF/A、表單、內容編輯、數位簽名
- 為企業應用程式提供高效能
- 非常適合 PDF/A、無障礙存取和合規性
您需要使用 iText7 進行核心 PDF 操作,並可能單獨包含 html2pdf 等可選外掛程式。
安裝(NuGet)
下載 iText 7 的 PDF 產生核心軟體包:
Install-Package itext7
您也可以透過"解決方案的套件管理器"畫面安裝 iText 7。 為此,您首先需要前往"工具"下拉式選單,然後找到"NuGet 套件管理員 > 管理解決方案的 NuGet 套件"。
然後,只需搜尋 iText 7,然後點擊"安裝"即可。
程式碼範例:使用 iText 7 從位元組數組建立 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();
}
}
}輸出 PDF 文件
解釋
PdfWriter將內容寫入MemoryStream。PdfDocument管理 PDF 的內部結構。Document用於新增進階內容(文字、圖像、表格)。- 呼叫
doc.Close()後,PDF 內容已完全寫入,可以作為位元組數組傳回。
此範例展示了 iText 7 相較於 iTextSharp更模組化、更易讀的 API 。然而,除非您引入單獨授權的 pdfhtml 庫,否則它仍然缺乏對 HTML/CSS 渲染的原生支援。
iText 7 的優缺點
優點:
*全面的 PDF 控制 iText 7 提供對 PDF 元素(例如表格、表單和數位簽章)的完全控制。 這使其成為對合規性要求很高的應用的理想選擇,這些應用需要特定的 PDF 標準,例如 PDF/A 或 PDF/UA。
*模組化和可擴展 iText 7 是模組化的,這意味著您可以只安裝您需要的特定模組(例如,pdfhtml 用於 HTML 到 PDF 的轉換)。 如果您不使用所有功能,這樣可以實現更輕量級的方案。
*支援複雜的 PDF 標準 iText 7 支援 PDF/A(存檔)、PDF/UA(輔助功能)和 PDF/X(列印)等 ISO 標準,使其適用於合規性至關重要的專業和法律環境。
*豐富的文件和支援 iText 7 擁有全面的文檔和龐大的使用者社群。 該公司還提供專業支持,確保開發人員在需要時能夠獲得幫助。
*提供免費版本(AGPL) 開發者可以根據 AGPL 授權免費使用 iText 7,這非常適合開源專案或個人使用。
缺點:
- AGPL 商業用途許可 雖然 iText 7 提供免費版本,但商業用戶必須遵守 AGPL 許可,該許可要求發布任何使用 iText 7 的軟體的源代碼,或支付商業許可費用。
*學習曲線陡峭 iText 7 的 API 更複雜、更豐富,與 IronPDF 等更簡單的函式庫相比,其學習曲線可能更陡峭。 開發人員需要熟悉其底層文件結構和基於模組的架構。
*重量級,適用於簡單任務\ iText 7 在處理基本的 PDF 任務時可能會顯得笨重,例如簡單的文件創建或基本的 HTML 到 PDF 轉換,尤其是與 IronPDF 等簡化流程的庫相比。
*需要外部模組才能將 HTML 轉換為 PDF iText 7 中的 HTML 轉 PDF 功能只能透過額外的 pdfhtml 模組來實現,該模組需要單獨安裝,並且可能無法像 IronPDF 那樣無縫地處理現代 Web 內容。
IronPDF for .NET:功能強大的 PDF 庫
IronPDF 是一個高級 .NET 庫,旨在簡化 PDF 文件生成,重點在於提高開發人員的效率。 它在渲染 HTML 內容和樣式方面尤其有效,使其成為現代網頁到 PDF 工作流程的理想選擇。
主要特點:
- 從位元組數組建立 PDF 文件,無需安裝 Adobe Reader 即可處理 PDF 文檔
- 使用完整的 Chromium 引擎直接渲染 HTML 到 PDF,從 HTML 內容建立 PDF 文檔
- 支援 MVC 視圖、Razor 頁面以及本機/遠端 URL
- 開箱即用,支援圖像檔案、JavaScript、CSS 和響應式佈局
- 語法簡單易用,設定簡單
- 永久授權,不受AGPL限制
安裝 IronPDF
IronPDF 也可以透過 NuGet 安裝,只需在 NuGet 套件管理器控制台中執行以下命令:
Install-Package IronPdf
或者,您也可以透過解決方案畫面的 NuGet 套件管理器進行安裝。 為此,請導覽至"工具 > NuGet 套件管理員 > 管理解決方案的 NuGet 套件"。
然後,搜尋 IronPDF,點擊"安裝"。
安裝完成後,即可在幾秒鐘內將完整的 HTML 頁面渲染成 PDF——無需額外的模組。 它支援現代 CSS、JavaScript,甚至無需額外配置即可支援互動式 Web 內容。
程式碼範例:使用 IronPDF 從位元組數組建立 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;
}
}輸出 PDF 文件
解釋
using IronPdf語句匯入 IronPDF 庫,以便存取所有與 PDF 相關的類別。var renderer = new ChromePdfRenderer()建立一個新的 HTML 到 PDF 渲染器,該渲染器由無頭 Chromium 引擎驅動。renderer.RenderHtmlAsPdf(...)將給定的 HTML 字串轉換為 PDF 文件。 您也可以傳入檔案路徑或網址。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 7 不同,IronPDF 提供靈活的商業許可,不受開源 AGPL 義務的限制。
*非常適合 MVC 和 Razor 視圖\ 將ASP.NET應用程式中的 .cshtml Razor 視圖無縫轉換為可列印的 PDF。
缺點:
*商業用途需獲得許可 雖然提供免費試用,但 IronPDF 不是開源軟體。 預算緊張的項目可能需要評估許可證費用。
*更大的初始包裝尺寸 由於它捆綁了一個無頭 Chromium 引擎,因此 NuGet 套件比某些替代方案更重。
實用程式碼範例對比
本節中的以下程式碼範例示範了這些函式庫的實際應用,我們將使用相同的任務來比較IronPDF和iText 7 。 我們將對這兩個庫進行相同的測試:從 URL 生成 PDF、將圖像渲染為 PDF 以及將樣式化的 HTML 轉換為 PDF,所有這些都將使用位元組數組來處理我們的 PDF 內容。 這將使開發人員能夠評估每個庫如何處理這些常見用例。
1. 使用位元組數組從 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;
}
}輸出 PDF
IronPDF使用無頭 Chromium 引擎,可實現網頁的像素級完美渲染,並完全支援 JavaScript 和 CSS。
iText 7
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();
}
}輸出
iText 7使用 HttpClient 取得原始 HTML,並使用 HtmlConverter 進行渲染,但它不支援 JavaScript 執行(iText 的官方文件證實了這一點,該文件建議使用 Selenium 或類似的瀏覽器自動化進行 JavaScript 預處理),並且CSS 樣式有限。 雖然 iText7 在 7.1.15 版本(2021 年)中添加了部分 flexbox 支持,但許多 CSS3 屬性仍然不受支持,特別是對於複雜的現代佈局。
2. 使用位元組數組從圖像建立新的 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;
}
}輸出
使用 IronPDF 的ImageToPdfConverter 工具輕鬆產生影像到 PDF 檔案。 有了它,您可以輕鬆地從 PNG 檔案或 JPG 檔案等圖像建立 PDF 檔案。
iText 7
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();
}
}輸出
使用 ImageDataFactory 手動建立文件佈局並明確插入影像。
3. 使用位元組數組將樣式化的 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;
}
}輸出
IronPDF 由於採用了 Chromium 引擎,因此完全支援標籤或外部樣式表中的 CSS。
iText 7 + 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();
}
}輸出
需要安裝付費外掛程式 pdfHTML 才能處理 HTML 轉換任務。
比較匯總
| 特徵 | IronPDF | iText 7(附 pdfHTML) |
|---|---|---|
| 將 URL 渲染為 PDF | 全鉻渲染 | 取得 HTML,不支援原生 JS |
| 新增圖片 | 透過 HTML 或其專用圖像浮水印工具嵌入 | 手動圖像工廠 |
| 渲染樣式化的 HTML | 完全支援 CSS | 僅透過 pdfHTML 支援 CSS |
| 返回位元組數組 | 是的 | 是的 |
| 設定複雜度 | 簡單的 | 中等(手動佈局) |
| 輸出品質 | 像素級完美 | 不錯,但略顯呆板 |
結論:你該選擇哪個.NET函式庫?
在IronPDF和iText 7之間進行選擇取決於您的專案需求——但就開發者體驗、易用性和現代渲染精度而言,IronPDF 顯然更勝一籌。
如果您正在處理動態HTML 內容、網頁渲染,或者需要從 URL建立具有完整 JavaScript 和 CSS 支援的 PDF 文件,IronPDF 基於 Chromium 的引擎可提供無與倫比的保真度。 其直覺的 API 和快速設定使其成為快速開發和實際生產使用的理想選擇——尤其是在處理位元組數組、文件流或基於 Web 的 PDF 生成時。
另一方面,iText 7 是一個功能強大且備受推崇的函式庫,它採用更傳統的、以佈局為驅動的方法。 它能夠對文件結構進行可靠的控制,非常適合需要精細操作的開發人員,但學習曲線較為陡峭,並且缺乏現代 HTML 渲染功能。
結論就是:
- 想要獲得像素級完美的現代網頁內容、樣式化的 HTML 輸出或快速原型設計? 選擇 IronPDF。 需要具有精細控制功能的底層 PDF 建立工具嗎? iText 7 或許正合適。
準備好開始使用 IronPDF 了嗎? 下載免費試用版,看看用幾行程式碼在 C# 中建立基於位元組數組的專業 PDF 是多麼容易。
常見問題解答
如何在 C# 中將位元組數組轉換為 PDF?
您可以使用 IronPDF 在 C# 中將位元組數組轉換為 PDF。只需使用 `PdfDocument.FromBytes` 方法將位元組陣列載入到 IronPDF 文件中,該方法將解析資料並產生 PDF 文件。
使用 IronPDF 進行 HTML 轉 PDF 轉換有哪些好處?
IronPDF之所以能在將HTML轉換為PDF方面表現出色,是因為它使用了無頭Chromium引擎,並支援現代CSS和JavaScript。這使其成為將動態網頁內容渲染成像素級完美PDF文件的理想選擇。
與 iText 7 相比,使用 IronPDF 產生 PDF 的主要優勢是什麼?
IronPDF 為需要將 HTML 轉換為 PDF 的專案提供更簡潔的 API 和更快捷的設置,並完全支援 CSS 和 JavaScript。它尤其適用於需要快速開發和整合 Web 內容的應用程式。
iText 7 如何處理 PDF 合規性問題?
iText 7 專為對合規性要求極高的行業而設計,支援 PDF/A、PDF/UA 和 PDF/X 等標準。它提供強大的 PDF 創建控制功能,使其適用於對合規性要求極高的應用場景。
如何在.NET專案中安裝IronPDF?
若要安裝 IronPDF,您可以使用 Visual Studio 中的 NuGet 套件管理器。在套件管理器控制台中執行命令 `Install-Package IronPdf` 將其新增至您的專案。
IronPDF 能否根據 ASP.NET 視圖建立 PDF 檔案?
是的,IronPDF 可以直接將 ASP.NET 視圖渲染成 PDF 文件。此功能使開發人員能夠輕鬆地將具有複雜佈局和樣式的網頁轉換為 PDF。
哪些類型的應用程式最能從使用 IronPDF 中受益?
對於需要將動態網頁內容(例如報表和發票)轉換為 PDF 的應用程式來說,IronPDF 是最佳選擇。其快速的設定和對 Web 技術支援使其成為需要頻繁更新和採用現代設計的專案的理想選擇。
iText 7 的模組化架構對其使用有何影響?
iText 7 的模組化架構允許根據需要添加特定的 PDF 功能,例如 HTML 轉換或數位簽章。這提供了靈活性,但每個模組可能需要額外的學習和安裝。
IronPDF 和 iText 7 的許可協議有何不同?
IronPDF 提供永久許可,適用於商業應用,不受 AGPL 協議的限制。相較之下,iText 7 採用 AGPL 開源許可,商業用途需要付費許可。






