IronPDF 與 GemBox.Pdf:.NET PDF 庫比較指南(HTML 轉 PDF)
在開發需要產生和處理 PDF 的 .NET 應用程式時,選擇合適的 C# PDF 程式庫會對專案的成功產生重大影響。 這篇全面的比較文章檢視了IronPDF 和 GemBox.Pdf這兩個著名的 .NET PDF 函式庫,旨在幫助開發人員根據功能、效能、定價和實際用例做出明智的決定。
快速對比概覽
| 類別 | 特徵/方面 | IronPDF | GemBox.Pdf | 主要優勢 |
|---|---|---|---|---|
| 核心架構 | 設計理念 | HTML優先的Chrome渲染 | PDF原生操作 | 上下文相關 |
| API複雜度 | 像RenderHtmlAsPdf()這樣的簡單方法 | 底層 PDF 對象 | IronPDF:更快的開發速度 | |
| 學習曲線 | 通常需要1-2天。 | 通常需要 3-5 天。 | IronPDF:更快的採用 | |
| 平台支援 | 跨平台 | Windows、Linux、macOS、Docker | Windows、Linux、macOS、Android、iOS | GemBox:行動支持 |
| .NET 版本 | .NET 10、9、8、7、6、5,Core 3.1+,Framework 4.6.2+ | .NET 6、Standard 2.0、Framework 3.5+ | IronPDF:最新 .NET 支持 | |
| 雲端平台 | Azure/AWS 最佳化 | 標準雲端支援 | IronPDF:雲端就緒 | |
| HTML 轉 PDF | 渲染引擎 | 全鍍鉻V8發動機 | 不支援內建 HTML* | IronPDF:原生 HTML 轉 PDF |
| CSS3/HTML5 支持 | 全面支持 | 需要 GemBox.Document | IronPDF:現代網路標準 | |
| JavaScript 執行 | 完全支援 JavaScript | 不支援 JavaScript | IronPDF:動態內容 | |
| 渲染速度 | 典型值約 125 毫秒 | 不適用(不支援HTML) | IronPDF:快速渲染 | |
| 開發者體驗 | 程式碼範例 | 100 多個可直接運作的樣品 | 提供 100 多個範例 | 兩者:豐富的資源 |
| 文件 | 教程、操作指南、視頻 | API 文件重點 | IronPDF:多種學習路徑 | |
| 內容操縱 | 合併/拆分PDF | 內建方法 | 內建方法 | 兩者:標準功能 |
| 文字擷取 | 支援Unicode,整合OCR | 支援Unicode編碼,內建OCR功能 | GemBox:內建OCR | |
| 表單支援 | HTML表單轉PDF表單 | 建立、填寫、展平表單 | 上下文相關 | |
| 績效指標 | 記憶體使用情況 | 通常小於 10MB | 記憶體佔用低 | 兩者:高效 |
| 螺紋支援 | 原生 async/await 優化 | 線程安全操作 | IronPDF:更好的非同步支持 | |
| Security & Encryption | 加密等級 | AES-256,自訂處理程序 | AES-256 標準 | 兩者:業界標準 |
| 數位簽名 | 整合式視覺簽名 | 數位簽名支持 | 兩者:簽名支持 | |
| Licensing & Pricing | 入門級 | Lite: $799 (1 dev, 1 project) | 單一開發者:890 美元(1 名開發者,專案數量不限) | IronPDF:更低的進入門檻 |
| 重新分配 | +1,999 美元免版稅 | 包含無限次部署 | GemBox:更優的部署條款 | |
| 套房選項 | Iron Suite: $1,498 (9 products) | GemBox套裝:2,200美元(7件產品) | IronPDF:更佳的套件價值 | |
| 支援 | 包含支持 | 是的,我們提供 24/5 全天候工程支援。 | 是的,提供 1 年專業支持 | 雙方:良好的支持 |
| 回應時間 | 通常需要 24-48 小時。 | 1個工作天內 | 兩者:快速響應 | |
| 最適合 | 用例 | HTML轉PDF、網頁應用程式、報表 | PDF處理、表單、OCR | 上下文相關 |
GemBox.Pdf 需要單獨的 GemBox.Document 庫(需額外支付 890 美元)才能將 HTML 轉換為 PDF。
什麼是 IronPDF 庫?
IronPDF 是一款功能全面的 .NET PDF 程式庫,專為開發人員無縫進行HTML 到 PDF 的轉換而設計,因此脫穎而出。 IronPDF 以 Chrome 渲染引擎為核心,改變了開發人員在 C#、F# 和 VB.NET 應用程式中產生 PDF 的方式。
該圖書館的理念是充分利用現有的網頁開發技能。 開發者無需學習複雜的 PDF API,即可使用熟悉的 HTML、CSS 和 JavaScript 建立複雜的 PDF 文件。 這種方法可以顯著縮短開發時間,同時確保像素級完美渲染,符合現代網路標準。
IronPDF的核心功能
IronPDF 提供了一套全面的功能集,幾乎涵蓋了開發人員可能遇到的所有與 PDF 相關的任務:
HTML 轉 PDF 轉換卓越性
- Chrome V8 引擎:使用與 Google Chrome 相同的引擎渲染 HTML,確保 98% 以上的瀏覽器顯示效果。 -完全支援 CSS3/HTML5:全面支援現代 Web 標準,包括 flexbox、grid 和動畫。
- JavaScript 執行:在渲染之前處理 JavaScript,完美捕捉動態內容 -響應式設計:支援響應式佈局和可自訂的視口設置 -網頁字體支援:能夠準確渲染 Google 字體、自訂字體和圖示字體。
進階PDF操作
-合併和拆分 PDF :透過簡單的方法呼叫合併多個 PDF 或提取特定頁面。 -數位簽名:應用支援證書的加密簽名 -表單管理:從 HTML 建立可填寫表單或修改現有 PDF 表單 -浮水印和圖章:添加可控制透明度的文字或圖像浮水印 -頁首和頁尾:帶有頁碼的動態頁首/頁尾
安全與合規
-加密:採用自訂權限設定的 AES-256 加密
IronPDF 程式碼範例:使用進階功能將 HTML 轉換為 PDF
using IronPdf;
using IronPdf.Rendering;
// Configure the Chrome renderer with advanced options
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
// Set paper orientation and size
PaperOrientation = PdfPaperOrientation.Portrait,
PaperSize = PdfPaperSize.A4,
// Configure margins (in millimeters)
MarginTop = 25,
MarginBottom = 25,
MarginLeft = 20,
MarginRight = 20,
// Enable JavaScript execution
EnableJavaScript = true,
RenderDelay = 1000, // Wait 1 second for JS to complete
// Print background colors and images
PrintHtmlBackgrounds = true,
// Create PDF forms from HTML form elements
CreatePdfFormsFromHtml = true,
// Custom header and footer
HtmlHeader = new HtmlHeaderFooter
{
Height = 20,
HtmlFragment = "<div style='text-align: center; font-size: 12px;'>Company Report - {page} of {total-pages}</div>",
DrawDividerLine = true
},
HtmlFooter = new HtmlHeaderFooter
{
Height = 15,
HtmlFragment = "<div style='text-align: center; font-size: 10px;'>© 2025 Company Name. Confidential.</div>"
}
}
};
// Convert complex HTML with CSS and JavaScript
string htmlContent = @"
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; }
.invoice-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
border-radius: 10px;
margin-bottom: 30px;
}
.data-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.data-table th {
background-color: #f8f9fa;
padding: 12px;
text-align: left;
border-bottom: 2px solid #dee2e6;
}
.data-table td {
padding: 10px;
border-bottom: 1px solid #dee2e6;
}
.total-section {
margin-top: 30px;
text-align: right;
font-size: 18px;
font-weight: bold;
}
@media print {
.no-print { display: none; }
}
</style>
</head>
<body>
<div class='invoice-header'>
<h1>Invoice #INV-2025-001</h1>
<p>Date: <span id='current-date'></span></p>
</div>
<table class='data-table'>
<thead>
<tr>
<th>Item Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Professional PDF Library License</td>
<td>1</td>
<td>$799.00</td>
<td>$799.00</td>
</tr>
<tr>
<td>Priority Support (1 Year)</td>
<td>1</td>
<td>$299.00</td>
<td>$299.00</td>
</tr>
</tbody>
</table>
<div class='total-section'>
<p>Subtotal: $1,048.00</p>
<p>Tax (8%): $83.84</p>
<p style='color: #667eea; font-size: 24px;'>Total: $1,131.84</p>
</div>
<script>
// Dynamic date insertion
document.getElementById('current-date').textContent = new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
</script>
</body>
</html>";
// Render the HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Apply additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserFormData = true;
// Add metadata
pdf.MetaData.Author = "Invoice System";
pdf.MetaData.Title = "Invoice INV-2025-001";
pdf.MetaData.Subject = "Customer Invoice";
pdf.MetaData.Keywords = "invoice, payment, 2025";
pdf.MetaData.CreationDate = DateTime.Now;
// Save the PDF
pdf.SaveAs("invoice-2025-001.pdf");
// Optional: Save as PDF/A for archival
pdf.SaveAsPdfA("invoice-2025-001-archive.pdf", PdfAVersions.PdfA3);using IronPdf;
using IronPdf.Rendering;
// Configure the Chrome renderer with advanced options
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
// Set paper orientation and size
PaperOrientation = PdfPaperOrientation.Portrait,
PaperSize = PdfPaperSize.A4,
// Configure margins (in millimeters)
MarginTop = 25,
MarginBottom = 25,
MarginLeft = 20,
MarginRight = 20,
// Enable JavaScript execution
EnableJavaScript = true,
RenderDelay = 1000, // Wait 1 second for JS to complete
// Print background colors and images
PrintHtmlBackgrounds = true,
// Create PDF forms from HTML form elements
CreatePdfFormsFromHtml = true,
// Custom header and footer
HtmlHeader = new HtmlHeaderFooter
{
Height = 20,
HtmlFragment = "<div style='text-align: center; font-size: 12px;'>Company Report - {page} of {total-pages}</div>",
DrawDividerLine = true
},
HtmlFooter = new HtmlHeaderFooter
{
Height = 15,
HtmlFragment = "<div style='text-align: center; font-size: 10px;'>© 2025 Company Name. Confidential.</div>"
}
}
};
// Convert complex HTML with CSS and JavaScript
string htmlContent = @"
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; }
.invoice-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
border-radius: 10px;
margin-bottom: 30px;
}
.data-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.data-table th {
background-color: #f8f9fa;
padding: 12px;
text-align: left;
border-bottom: 2px solid #dee2e6;
}
.data-table td {
padding: 10px;
border-bottom: 1px solid #dee2e6;
}
.total-section {
margin-top: 30px;
text-align: right;
font-size: 18px;
font-weight: bold;
}
@media print {
.no-print { display: none; }
}
</style>
</head>
<body>
<div class='invoice-header'>
<h1>Invoice #INV-2025-001</h1>
<p>Date: <span id='current-date'></span></p>
</div>
<table class='data-table'>
<thead>
<tr>
<th>Item Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>Professional PDF Library License</td>
<td>1</td>
<td>$799.00</td>
<td>$799.00</td>
</tr>
<tr>
<td>Priority Support (1 Year)</td>
<td>1</td>
<td>$299.00</td>
<td>$299.00</td>
</tr>
</tbody>
</table>
<div class='total-section'>
<p>Subtotal: $1,048.00</p>
<p>Tax (8%): $83.84</p>
<p style='color: #667eea; font-size: 24px;'>Total: $1,131.84</p>
</div>
<script>
// Dynamic date insertion
document.getElementById('current-date').textContent = new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
</script>
</body>
</html>";
// Render the HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Apply additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserFormData = true;
// Add metadata
pdf.MetaData.Author = "Invoice System";
pdf.MetaData.Title = "Invoice INV-2025-001";
pdf.MetaData.Subject = "Customer Invoice";
pdf.MetaData.Keywords = "invoice, payment, 2025";
pdf.MetaData.CreationDate = DateTime.Now;
// Save the PDF
pdf.SaveAs("invoice-2025-001.pdf");
// Optional: Save as PDF/A for archival
pdf.SaveAsPdfA("invoice-2025-001-archive.pdf", PdfAVersions.PdfA3);此範例展示了 IronPDF 處理複雜 HTML 佈局(包括漸層、表格、動態 JavaScript 內容)以及進階 PDF 功能(如安全設定和元資料)的能力。 該程式庫能夠無縫處理所有內容,產生與 HTML 設計完全匹配的專業 PDF 文件。
GemBox.Pdf庫是什麼?
GemBox.Pdf 代表了 .NET 中處理 PDF 的一種不同方法。 GemBox.Pdf 並非專注於 HTML 到 PDF 的轉換,而是專注於底層 PDF 操作和直接 PDF 處理。 這使其特別適用於開發人員需要精確控制 PDF 結構和內容的場景。
該庫擅長處理現有的 PDF 文檔,提供強大的功能,可以精細地讀取、編輯和操作 PDF 文件。 GemBox.Pdf 無需依賴 Adobe Acrobat 即可運行,因此可以輕鬆部署到各種環境中。
GemBox 的核心功能.pdf
GemBox.Pdf 提供全面的 PDF 處理功能:
PDF 文件操作
-讀取和寫入 PDF:直接存取 PDF 結構和內容 -合併與分割:合併多個 PDF 檔案或提取特定頁面 -複製頁面:在不同的 PDF 文件之間匯入頁面 -頁面管理:以程式設計方式新增、刪除或重新排序頁面
內容擷取與處理
-文本提取:提取帶有定位訊息的Unicode文本 -影像擷取:從 PDF 文件中擷取嵌入的影像
- OCR 支援:內建光學字元辨識功能,用於識別掃描文檔 內容編輯:永久刪除敏感訊息
表單和互動性
-互動式表單:建立、填寫、展平並匯出 PDF 表單 -表單欄位管理:以程式設計方式存取所有表單欄位類型 -註釋:新增超連結和其他 PDF 註釋
圖形和視覺元素
-繪圖操作:新增文字、圖像和向量圖形 -形狀和路徑:建立複雜的向量圖 -浮水印:添加文字和圖片浮水印 -內容分組:將內容組織成邏輯分組
GemBox.Pdf 程式碼範例:PDF 操作和表單
using GemBox.Pdf;
using GemBox.Pdf.Content;
using GemBox.Pdf.Forms;
using GemBox.Pdf.Security;
// Set license (use FREE-LIMITED-KEY for evaluation)
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Create a new PDF document from scratch
using (var document = new PdfDocument())
{
// Add a page with custom size
var page = document.Pages.Add();
// Create formatted text with multiple styles
using (var formattedText = new PdfFormattedText())
{
formattedText.FontSize = 24;
formattedText.FontFamily = new PdfFontFamily("Arial");
formattedText.Color = PdfColor.FromRgb(0.2, 0.2, 0.6);
formattedText.AppendLine("GemBox.Pdf Document Example");
formattedText.FontSize = 12;
formattedText.Color = PdfColor.FromRgb(0, 0, 0);
formattedText.AppendLine("This document demonstrates PDF creation and manipulation features.");
// Draw text to the page
page.Content.DrawText(formattedText, new PdfPoint(50, 700));
}
// Add a form to the document
var form = document.Form;
// Create text field
var textField = form.Fields.AddText(page, 50, 600, 200, 30);
textField.Name = "CustomerName";
textField.Value = "Enter your name";
textField.Format.Fill.Color = PdfColor.FromRgb(0.95, 0.95, 0.95);
// Create checkbox
var checkBox = form.Fields.AddCheck(page, 50, 550, 20, 20);
checkBox.Name = "Agreement";
checkBox.Checked = false;
// Add descriptive text next to checkbox
using (var checkboxLabel = new PdfFormattedText())
{
checkboxLabel.Append("I agree to the terms and conditions");
page.Content.DrawText(checkboxLabel, new PdfPoint(80, 555));
}
// Create submit button
var submitButton = form.Fields.AddButton(page, 50, 500, 100, 30);
submitButton.Name = "Submit";
submitButton.Actions.Activate = PdfActionType.Named;
// Add button appearance
using (var buttonText = new PdfFormattedText())
{
buttonText.Append("Submit Form");
buttonText.FontSize = 14;
buttonText.Color = PdfColor.FromRgb(1, 1, 1);
// Create button appearance stream
var appearance = submitButton.Appearance.Normal;
appearance.Fill.Color = PdfColor.FromRgb(0.2, 0.4, 0.8);
appearance.Stroke.Color = PdfColor.FromRgb(0.1, 0.2, 0.4);
appearance.Stroke.Width = 1;
// Draw button background
appearance.DrawRectangle(new PdfPoint(0, 0), new PdfSize(100, 30), true, true);
// Draw button text
appearance.DrawText(buttonText, new PdfPoint(20, 10));
}
// Add a table using low-level graphics
var tableTop = 400;
var tableLeft = 50;
var cellWidth = 150;
var cellHeight = 30;
// Draw table headers
page.Content.DrawRectangle(new PdfPoint(tableLeft, tableTop),
new PdfSize(cellWidth * 3, cellHeight), true, true);
using (var headerText = new PdfFormattedText())
{
headerText.FontSize = 12;
headerText.Color = PdfColor.FromRgb(1, 1, 1);
headerText.Append("Product");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + 10, tableTop + 10));
headerText.Clear();
headerText.Append("Quantity");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + cellWidth + 10, tableTop + 10));
headerText.Clear();
headerText.Append("Price");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + cellWidth * 2 + 10, tableTop + 10));
}
// Draw table data rows
var rowData = new[]
{
new { Product = "PDF Library", Quantity = "1", Price = "$890" },
new { Product = "Support Plan", Quantity = "1", Price = "$299" },
new { Product = "Training", Quantity = "2", Price = "$500" }
};
var currentY = tableTop - cellHeight;
foreach (var row in rowData)
{
// Draw cell borders
page.Content.DrawRectangle(new PdfPoint(tableLeft, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
page.Content.DrawRectangle(new PdfPoint(tableLeft + cellWidth, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
page.Content.DrawRectangle(new PdfPoint(tableLeft + cellWidth * 2, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
// Draw cell content
using (var cellText = new PdfFormattedText())
{
cellText.FontSize = 11;
cellText.Append(row.Product);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + 10, currentY + 10));
cellText.Clear();
cellText.Append(row.Quantity);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + cellWidth + 10, currentY + 10));
cellText.Clear();
cellText.Append(row.Price);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + cellWidth * 2 + 10, currentY + 10));
}
currentY -= cellHeight;
}
// Apply security settings
var securitySettings = document.SecuritySettings;
securitySettings.DocumentOpenPassword = "user123";
securitySettings.PermissionsPassword = "owner123";
securitySettings.Permissions = PdfPermissions.AllowPrint | PdfPermissions.AllowFormFill;
// Save the document
document.Save("gembox-example.pdf");
}
// Example: Manipulating existing PDF
using (var existingDoc = PdfDocument.Load("existing-document.pdf"))
{
// Extract text from first page
var page = existingDoc.Pages[0];
var text = page.Content.GetText();
Console.WriteLine($"Extracted text: {text}");
// Add watermark to all pages
foreach (var p in existingDoc.Pages)
{
using (var watermark = new PdfFormattedText())
{
watermark.Append("CONFIDENTIAL");
watermark.FontSize = 50;
watermark.Color = PdfColor.FromRgb(0.5, 0.5, 0.5);
watermark.Opacity = 0.3;
// Calculate center position
var pageWidth = p.MediaBox.Width;
var pageHeight = p.MediaBox.Height;
var textWidth = watermark.Width;
var textHeight = watermark.Height;
var x = (pageWidth - textWidth) / 2;
var y = (pageHeight - textHeight) / 2;
// Draw watermark diagonally
p.Content.SaveGraphicsState();
p.Content.SetTransform(1, 0, 0, 1, x, y);
p.Content.SetTransform(0.7071, 0.7071, -0.7071, 0.7071, 0, 0);
p.Content.DrawText(watermark, new PdfPoint(0, 0));
p.Content.RestoreGraphicsState();
}
}
existingDoc.Save("watermarked-document.pdf");
}using GemBox.Pdf;
using GemBox.Pdf.Content;
using GemBox.Pdf.Forms;
using GemBox.Pdf.Security;
// Set license (use FREE-LIMITED-KEY for evaluation)
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Create a new PDF document from scratch
using (var document = new PdfDocument())
{
// Add a page with custom size
var page = document.Pages.Add();
// Create formatted text with multiple styles
using (var formattedText = new PdfFormattedText())
{
formattedText.FontSize = 24;
formattedText.FontFamily = new PdfFontFamily("Arial");
formattedText.Color = PdfColor.FromRgb(0.2, 0.2, 0.6);
formattedText.AppendLine("GemBox.Pdf Document Example");
formattedText.FontSize = 12;
formattedText.Color = PdfColor.FromRgb(0, 0, 0);
formattedText.AppendLine("This document demonstrates PDF creation and manipulation features.");
// Draw text to the page
page.Content.DrawText(formattedText, new PdfPoint(50, 700));
}
// Add a form to the document
var form = document.Form;
// Create text field
var textField = form.Fields.AddText(page, 50, 600, 200, 30);
textField.Name = "CustomerName";
textField.Value = "Enter your name";
textField.Format.Fill.Color = PdfColor.FromRgb(0.95, 0.95, 0.95);
// Create checkbox
var checkBox = form.Fields.AddCheck(page, 50, 550, 20, 20);
checkBox.Name = "Agreement";
checkBox.Checked = false;
// Add descriptive text next to checkbox
using (var checkboxLabel = new PdfFormattedText())
{
checkboxLabel.Append("I agree to the terms and conditions");
page.Content.DrawText(checkboxLabel, new PdfPoint(80, 555));
}
// Create submit button
var submitButton = form.Fields.AddButton(page, 50, 500, 100, 30);
submitButton.Name = "Submit";
submitButton.Actions.Activate = PdfActionType.Named;
// Add button appearance
using (var buttonText = new PdfFormattedText())
{
buttonText.Append("Submit Form");
buttonText.FontSize = 14;
buttonText.Color = PdfColor.FromRgb(1, 1, 1);
// Create button appearance stream
var appearance = submitButton.Appearance.Normal;
appearance.Fill.Color = PdfColor.FromRgb(0.2, 0.4, 0.8);
appearance.Stroke.Color = PdfColor.FromRgb(0.1, 0.2, 0.4);
appearance.Stroke.Width = 1;
// Draw button background
appearance.DrawRectangle(new PdfPoint(0, 0), new PdfSize(100, 30), true, true);
// Draw button text
appearance.DrawText(buttonText, new PdfPoint(20, 10));
}
// Add a table using low-level graphics
var tableTop = 400;
var tableLeft = 50;
var cellWidth = 150;
var cellHeight = 30;
// Draw table headers
page.Content.DrawRectangle(new PdfPoint(tableLeft, tableTop),
new PdfSize(cellWidth * 3, cellHeight), true, true);
using (var headerText = new PdfFormattedText())
{
headerText.FontSize = 12;
headerText.Color = PdfColor.FromRgb(1, 1, 1);
headerText.Append("Product");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + 10, tableTop + 10));
headerText.Clear();
headerText.Append("Quantity");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + cellWidth + 10, tableTop + 10));
headerText.Clear();
headerText.Append("Price");
page.Content.DrawText(headerText, new PdfPoint(tableLeft + cellWidth * 2 + 10, tableTop + 10));
}
// Draw table data rows
var rowData = new[]
{
new { Product = "PDF Library", Quantity = "1", Price = "$890" },
new { Product = "Support Plan", Quantity = "1", Price = "$299" },
new { Product = "Training", Quantity = "2", Price = "$500" }
};
var currentY = tableTop - cellHeight;
foreach (var row in rowData)
{
// Draw cell borders
page.Content.DrawRectangle(new PdfPoint(tableLeft, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
page.Content.DrawRectangle(new PdfPoint(tableLeft + cellWidth, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
page.Content.DrawRectangle(new PdfPoint(tableLeft + cellWidth * 2, currentY),
new PdfSize(cellWidth, cellHeight), false, true);
// Draw cell content
using (var cellText = new PdfFormattedText())
{
cellText.FontSize = 11;
cellText.Append(row.Product);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + 10, currentY + 10));
cellText.Clear();
cellText.Append(row.Quantity);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + cellWidth + 10, currentY + 10));
cellText.Clear();
cellText.Append(row.Price);
page.Content.DrawText(cellText, new PdfPoint(tableLeft + cellWidth * 2 + 10, currentY + 10));
}
currentY -= cellHeight;
}
// Apply security settings
var securitySettings = document.SecuritySettings;
securitySettings.DocumentOpenPassword = "user123";
securitySettings.PermissionsPassword = "owner123";
securitySettings.Permissions = PdfPermissions.AllowPrint | PdfPermissions.AllowFormFill;
// Save the document
document.Save("gembox-example.pdf");
}
// Example: Manipulating existing PDF
using (var existingDoc = PdfDocument.Load("existing-document.pdf"))
{
// Extract text from first page
var page = existingDoc.Pages[0];
var text = page.Content.GetText();
Console.WriteLine($"Extracted text: {text}");
// Add watermark to all pages
foreach (var p in existingDoc.Pages)
{
using (var watermark = new PdfFormattedText())
{
watermark.Append("CONFIDENTIAL");
watermark.FontSize = 50;
watermark.Color = PdfColor.FromRgb(0.5, 0.5, 0.5);
watermark.Opacity = 0.3;
// Calculate center position
var pageWidth = p.MediaBox.Width;
var pageHeight = p.MediaBox.Height;
var textWidth = watermark.Width;
var textHeight = watermark.Height;
var x = (pageWidth - textWidth) / 2;
var y = (pageHeight - textHeight) / 2;
// Draw watermark diagonally
p.Content.SaveGraphicsState();
p.Content.SetTransform(1, 0, 0, 1, x, y);
p.Content.SetTransform(0.7071, 0.7071, -0.7071, 0.7071, 0, 0);
p.Content.DrawText(watermark, new PdfPoint(0, 0));
p.Content.RestoreGraphicsState();
}
}
existingDoc.Save("watermarked-document.pdf");
}此範例展示了 GemBox.Pdf 在底層 PDF 操作方面的強大功能,示範了表單建立、圖形繪製和文件安全功能。 雖然程式碼比 IronPDF 的 HTML 方法更冗長,但它可以對 PDF 的各個方面進行精確控制。
這些庫如何處理 HTML 到 PDF 的轉換?
HTML 轉換 PDF 的轉換方法是 IronPDF 和 GemBox.Pdf 之間最顯著的差異之一。
IronPDF 的 HTML 轉 PDF 方法
IronPDF 從一開始就以 HTML 轉 PDF 為主要目標而建置。 該程式庫包含一個完整的 Chrome 渲染引擎,可以像 Web 瀏覽器一樣處理 HTML、CSS 和 JavaScript。 這意味著:
-真正的瀏覽器渲染:使用與 Google Chrome 相同的 Blink 引擎 -完全符合 Web 標準:全面支援 CSS3、HTML5、Flexbox、Grid 和現代 JavaScript -動態內容:在渲染之前執行 JavaScript,捕捉 AJAX 內容和動態元素 -響應式設計:處理媒體查詢和響應式佈局 -外部資源:自動從 URL 取得圖像、樣式表和字體
GemBox 的 HTML 支援有限
GemBox.Pdf 本身不支援 HTML 到 PDF 的轉換。 要使用 GemBox 將 HTML 轉換為 PDF,開發人員必須:
- 單獨購買 GemBox.Document(需額外支付 890 美元)
- 使用 GemBox.Document 將 HTML 轉換為 DOCX
- 然後將 DOCX 轉換為 PDF
這種方法有許多限制:
-不支援 JavaScript:無法處理動態內容
- CSS 支援有限:不支援許多現代 CSS 特性。 -樣式問題:已知邊框和複雜佈局有問題,詳情請參閱Stack Overflow 討論。 -僅限列印最佳化:需要專門針對列印進行格式化的 HTML 程式碼 -額外費用:需要購買兩個獨立的圖書館
現代 CSS 框架支援:一個關鍵區別
IronPDF 基於 Chrome 的渲染方式最顯著的優勢之一在使用 Bootstrap、Tailwind CSS 和 Foundation 等現代 CSS 框架時就顯現出來了。 這些框架已成為現代 Web 應用程式的支柱,它們在 PDF 中正確渲染的能力對於許多用例至關重要。
IronPDF:全面支持現代框架
IronPDF 的 Chrome 渲染引擎為所有現代 CSS 框架提供完整的原生支援:
- Bootstrap 5:全面支援 flexbox 和 CSS Grid,實現響應式佈局
- Tailwind CSS:完全支援實用性優先的 CSS 框架 -實際應用範例:完美渲染Bootstrap 首頁和Bootstrap 範本。 -現代 CSS3 特性:動畫、變換、轉換、自訂屬性 -響應式設計:媒體查詢和基於視窗的佈局無縫協作
程式碼範例:Bootstrap 表單轉 PDF
using IronPdf;
var renderer = new ChromePdfRenderer();
string bootstrapForm = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body>
<div class='container mt-5'>
<h1 class='mb-4'>Customer Registration</h1>
<form class='needs-validation'>
<div class='row g-3'>
<div class='col-md-6'>
<label class='form-label'>First Name</label>
<input type='text' class='form-control' value='John'>
</div>
<div class='col-md-6'>
<label class='form-label'>Last Name</label>
<input type='text' class='form-control' value='Smith'>
</div>
</div>
<div class='mt-4'>
<button class='btn btn-primary' type='submit'>Submit</button>
</div>
</form>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(bootstrapForm);
pdf.SaveAs("bootstrap-form.pdf");using IronPdf;
var renderer = new ChromePdfRenderer();
string bootstrapForm = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body>
<div class='container mt-5'>
<h1 class='mb-4'>Customer Registration</h1>
<form class='needs-validation'>
<div class='row g-3'>
<div class='col-md-6'>
<label class='form-label'>First Name</label>
<input type='text' class='form-control' value='John'>
</div>
<div class='col-md-6'>
<label class='form-label'>Last Name</label>
<input type='text' class='form-control' value='Smith'>
</div>
</div>
<div class='mt-4'>
<button class='btn btn-primary' type='submit'>Submit</button>
</div>
</form>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(bootstrapForm);
pdf.SaveAs("bootstrap-form.pdf");GemBox:不支援現代框架
由於 GemBox.Pdf 缺乏原生 HTML 渲染功能,必須依賴 GemBox.Document 進行 HTML 轉換,因此現代 CSS 框架面臨嚴重的限制:
-不支援 Bootstrap: Flexbox 和 CSS Grid 功能將無法正確渲染。 -不支援 Tailwind CSS:不支援實用類別和現代佈局 -需要手動調整:必須建立針對列印最佳化的 HTML 版本。
- CSS3 功能受限:許多現代 CSS 功能根本無法使用。 -額外的複雜性:兩步驟轉換過程(HTML → DOCX → PDF)引入了不一致性
根據開發者的討論,即使是邊框和佈局等基本的 CSS 功能,使用 GemBox 的 HTML 轉換方法也可能會遇到問題。
實際影響:如果您的應用程式使用 Bootstrap 作為其 UI,並且您需要產生與 Web 介面相符的 PDF 報告或匯出文件,IronPDF 提供了一個簡單的解決方案,而 GemBox 則需要進行大量的重新設計或手動變通。
有關 CSS 框架相容性的更多詳細信息,請參閱Bootstrap 和 Flexbox CSS 指南。
哪個庫擅長處理不同的PDF任務?
了解每個函式庫的優勢所在,有助於開發者根據自身需求選擇合適的工具。
IronPDF 的優勢所在
IronPDF 在以下場景中表現出優異的效能:
1. Web應用程式PDF生成
非常適合 SaaS 應用程式、入口網站以及任何需要將網頁內容轉換為 PDF 的系統。 Chrome 渲染引擎可確保複雜的網頁佈局(包括使用 Bootstrap、Tailwind CSS 或自訂框架的佈局)完美渲染。
2.動態報表生成
當報告包含圖表(Chart.js、D3.js)、動態資料視覺化或 JavaScript 渲染的內容時,IronPDF 可以準確地擷取所有內容。 這使其成為商業智慧儀表板和數據驅動型應用程式的理想選擇。
3.發票和文件模板
使用 HTML/CSS 範本建立發票、收據和商務文檔,可讓設計人員無需學習 PDF 特有的 API 即可建立範本。而且,使用熟悉的 Web 技術即可快速進行修改。
4.快速發展項目
簡潔的API和基於HTML的方法顯著縮短了開發時間。開發人員只需幾分鐘即可建立複雜的PDF文件,而無需像以前那樣花費數小時。
5.跨平台部署
IronPDF 原生支援 Windows、Linux、macOS、Docker 和雲端平台,簡化了在各種環境中的部署。
GemBox.Pdf 的優勢所在
GemBox.Pdf 在以下幾個方面展現了其優勢:
1.低階 PDF 操作
當需要精確控制 PDF 結構時,GemBox.Pdf 的物件導向方法提供了對 PDF 元素、流和字典的直接存取。
2.表單多的應用
對於大量使用 PDF 表單的應用程序,GemBox.Pdf 提供全面的表單欄位操作功能,包括以程式設計方式建立複雜的互動式表單。
3. OCR 要求
GemBox.Pdf 具有內建的 OCR 功能,無需額外的庫即可從掃描文件中提取文本,因此適用於文件數位化專案。
4.行動開發
GemBox.Pdf 原生支援 Android 和 iOS 平台,是需要 PDF 功能的行動應用程式的理想選擇。
5.現有PDF文件的修改
如果主要處理現有的 PDF 文件而不是產生新的 PDF 文件,那麼 GemBox.Pdf 的提取、操作和修改工具非常適合這項任務。
安裝和設定有何異同?
這兩個函式庫都可以透過 NuGet 輕鬆安裝,但複雜程度不同。
安裝 IronPDF
IronPDF的安裝非常簡單:
Install-Package IronPdf
或透過 .NET CLI:
dotnet add package IronPdf
該軟體包包含所有必需元件,包括 Chrome 渲染引擎。大多數情況下無需額外配置或依賴項。
針對特定部署場景,IronPDF 提供:
IronPdf.Slim :輕量級軟體包,可在運行時下載特定於平台的元件。
IronPdf.Linux:已預先配置用於 Linux 部署IronPdf.MacOs:針對 macOS 環境進行了最佳化
安裝 GemBox.Pdf
GemBox.Pdf 的安裝對於基本的 PDF 操作同樣簡單:
Install-Package GemBox.Pdf
但是,要將 HTML 轉換為 PDF,您需要:
Install-Package GemBox.Document
這兩個庫都需要配置許可證密鑰:
// IronPDF
License.LicenseKey = "YOUR-LICENSE-KEY";
// GemBox.Pdf
ComponentInfo.SetLicense("YOUR-LICENSE-KEY");// IronPDF
License.LicenseKey = "YOUR-LICENSE-KEY";
// GemBox.Pdf
ComponentInfo.SetLicense("YOUR-LICENSE-KEY");許可證和定價方面有哪些區別?
了解授權模式有助於確定每種解決方案的總擁有成本。
IronPDF 許可
IronPDF 提供透明的分級許可(定價截至 2025 年):
-精簡版授權: $799
- 1 位開發者
- 1 個項目
- 1 年的支持與更新
- Plus License: $1,199
- 3 位開發者
- 3 個項目
- 1 年的支持與更新
-專業授權: $2,399
- 10 位開發者
- 10 個項目
- 1 年的支持與更新
其他選項:
- 免版稅再分發:+ $2,399
- 提供 SaaS/OEM 許可
- Iron Suite 套裝:9 件產品,售價$1,498
主要優勢:
- 30 天退款保證
- 永久許可證(一次購買) 免費開發和測試
- 包括部署到開發/測試/生產環境
GemBox.pdf 許可
GemBox提供以開發者計費的授權模式:
-單一開發商: 890 美元
- 1 位開發者
- 項目數量不限
- 1 年的支持與更新
- 包含免版稅部署
重要注意事項:
- HTML 轉 PDF 需要 GemBox.Document:額外收費 890 美元
- HTML 轉 PDF 總費用:最低 1,780 美元
- 續訂享40%折扣(到期前續訂享60%折扣)
- GemBox 套裝:7 件產品,售價 2,200 美元
主要優勢:
- 30 天退款保證 永久許可
- 包含無限次部署 無需伺服器或 OEM 許可證
常見場景的成本比較
| 設想 | IronPDF 成本 | 寶石盒費用 |
|---|---|---|
| 單人開發,HTML 轉 PDF | $799 | $1,780 |
| 3名開發人員,僅負責PDF處理 | $1,499 | $2,670 |
| 3 位開發人員,HTML 轉 PDF | $1,199 | $5,340 |
| 企業版(10 位開發人員),完整功能 | $2,399 | 8900美元以上 |
你該選擇哪家圖書館?
IronPDF 和 GemBox.Pdf 之間的選擇取決於您的特定需求:
選擇 IronPDF 的情況:
-將 HTML 轉換為 PDF 是首要需求: IronPDF 基於 Chrome 的渲染功能無可匹敵。 -您需要現代 Web 支援:完整的 CSS3、HTML5 和 JavaScript 功能 快速開發至關重要:簡潔的 API 可縮短產品上市時間 -您需要處理動態內容: JavaScript 執行會擷取即時數據 -需要跨平台部署:對雲端和容器化環境提供卓越的支持 預算也是一個考量: HTML 轉 PDF 方案的入門成本更低,性價比更高。
選擇 GemBox.Pdf 時:
-底層 PDF 控制至關重要:直接存取 PDF 物件和結構 -需要行動平台支援:原生相容 Android 和 iOS
- OCR 是一項核心要求:內建 OCR,無需額外依賴項 -您主要處理現有 PDF 文件:強大的操作和提取功能 -無需使用 HTML 轉 PDF:避免為未使用的 GemBox.Document 付費 -無限制部署至關重要:包含免版稅再分發。
在以下情況下考慮同時使用這兩個函式庫:
有些組織發現,在應用程式的不同部分同時使用這兩個庫很有價值:
- IronPDF 用於報告產生和 HTML 轉 PDF 轉換
- GemBox.Pdf 用於表單處理和 PDF 操作
開始使用您選擇的圖書館
兩家圖書館都提供免費試用,供使用者評估其功能:
試試 IronPDF
- 安裝 NuGet 套件
- 開發無需許可證密鑰
- 試用模式下,PDF 文件上會顯示浮水印
- 評估期間可完全使用所有功能
試試 GemBox.Pdf
- 安裝 NuGet 套件
- 使用
ComponentInfo.SetLicense("FREE-LIMITED-KEY") - 免費模式下限2頁
- 升級以解除限制
可視化安裝指南
對於喜歡使用 Visual Studio 圖形介面的開發人員,以下是安裝這兩個程式庫的逐步指南:
建立新的 ASP.NET 項目
Visual Studio 新專案對話方塊顯示 ASP.NET Web 應用程式選擇以及 .NET Framework 選項 Visual Studio 的"新專案"對話方塊用於建立 ASP.NET Web 應用程式-PDF 產生專案的基礎
! ASP.NET 專案範本選擇畫面突出顯示用於 PDF 庫整合的 Web Forms 選項 選擇 Web Forms 作為專案範本 - 適用於 IronPDF 和 GemBox.Pdf 實現
透過 NuGet 安裝 PDF 庫
解決方案資源管理器上下文功能表顯示"管理 NuGet 套件"選項,用於新增 PDF 庫 在解決方案資源管理器中以滑鼠右鍵按一下您的項目,即可存取 NuGet 套件管理器以安裝程式庫。
Visual Studio 專案選單中顯示"管理 NuGet 套件"選項,用於安裝 PDF 庫 另一種透過 Visual Studio 中的"專案"功能表存取 NuGet 套件管理器的方法
NuGet 套件管理器介面顯示 IronPDF 安裝信息,包括版本詳情和描述。 透過 NuGet 套件管理器安裝 IronPDF - 請注意完整的功能描述和下載次數
其他安裝方法
IronPDF 官方網站下載頁面顯示可直接下載 DLL 檔案進行手動安裝。 IronPDF 網站提供直接 DLL 下載,適用於 NuGet 安裝不適用的情況。
結論
IronPDF 和 GemBox.Pdf 都是 .NET 開發人員的高品質 PDF 庫,各有其獨特優勢。 IronPDF 憑藉其 Chrome 渲染引擎,在 HTML 到 PDF 的轉換方面表現出色,使其成為現代 Web 應用程式和動態內容生成的理想選擇。 GemBox.Pdf 在底層 PDF 操作和行動支援方面表現出色,非常適合需要精確 PDF 控制的應用。
對於大多數建立現代應用程式的 Web 開發人員和團隊而言,IronPDF 提供了功能、易用性和價值的最佳組合。 它能夠利用熟悉的網頁技術,從 HTML 渲染出像素級完美的 PDF,從而顯著減少開發時間和複雜性。
然而,GemBox.Pdf 仍然是特定場景下的可靠選擇,特別是行動開發和專注於 PDF 表單處理或 OCR 要求的應用程式。
準備好將您的 HTML 內容轉換為專業的 PDF 檔案了嗎? 立即開始使用 IronPDF 的免費試用版,體驗在 .NET 應用程式中實現 Chrome 品質渲染的強大功能。 憑藉全面的文件、豐富的程式碼範例和快速回應的支持,您只需幾分鐘而不是幾小時即可產生 PDF 文件。
常見問題解答
如何在C#中將HTML轉換為PDF?
您可以使用 IronPDF 的RenderHtmlAsPdf方法將 HTML 字串轉換為 PDF。您也可以使用RenderHtmlFileAsPdf將 HTML 檔案轉換為 PDF。
我可以使用這些庫來修改現有的PDF檔案嗎?
是的,IronPDF 和 GemBox.Pdf 都可以用來編輯現有的 PDF 檔案。 IronPDF 簡化了合併、分割和添加浮水印等常見任務。 GemBox.Pdf 則提供了對 PDF 物件更精細的控制,這對於複雜的編輯任務非常有用。
使用 IronPDF 進行 HTML 轉 PDF 轉換有哪些優勢?
IronPDF採用Chrome V8渲染引擎,提供卓越的HTML轉PDF轉換質量,瀏覽器相容性高達98%以上。它支援現代CSS3、HTML5、JavaScript執行、網頁字體和響應式設計。
對於小型開發團隊來說,哪個函式庫更具成本效益?
對於一個需要將 HTML 轉換為 PDF 的 3 人開發團隊來說,IronPDF 的 Plus 授權價格為 1,499 美元,而 GemBox 的 3 個 GemBox.Pdf 授權和 3 個 GemBox.Document 授權則需要 5,340 美元。 IronPDF 為團隊提供了更高的性價比。
IronPDF 是否適用於大批量 PDF 生成?
是的,IronPDF 性能卓越,HTML 轉 PDF 渲染時間約為 125 毫秒,優化了非同步/等待支持,記憶體佔用低於 10MB。它非常適合大量生成基於 Web 的 PDF 文件。
IronPDF包含哪些類型的支援?
IronPDF 提供 24/5 全天候工程支持,可直接聯繫開發團隊,通常 24-48 小時回應,並提供全面的文件和程式碼範例。
我可以用這些函式庫建立符合 PDF/A 標準的文件嗎?
是的,IronPDF 可以透過類似SaveAsPdfA()這樣的簡單方法呼叫來產生符合 PDF/A 標準的文檔,並支援多種 PDF/A 版本。 GemBox.Pdf 可以讀取 PDF/A 文檔,並在修改時保持其合規性。
這些函式庫與現代 .NET 框架相容嗎?
是的,這兩個函式庫都支援最新的.NET版本。 IronPDF支援.NET Framework 4.6.2+、.NET Core 3.1+以及.NET 5至10。 GemBox.Pdf支援.NET Framework 3.5+、.NET Standard 2.0以及.NET 6+。
哪個函式庫針對雲端部署進行了最佳化?
IronPDF 針對雲端部署進行了最佳化,特別支援 Azure、AWS、Docker 容器和無伺服器環境。其基於 HTML 的方法與 Web 應用程式天然契合,使其成為 SaaS 應用程式的更佳選擇。
IronPDF 是否有免費版本可供測試?
IronPDF 提供免費評估選項,無需許可證金鑰即可進行無限次開發和測試,但輸出結果上會帶有浮水印。這足以讓您在購買前評估產品功能。






