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、安卓、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 | 渲染引擎 | 完整的 Chrome V8 引擎 | 不支持内置 HTML* | IronPDF:将本地 HTML 转换为 PDF |
| 支持 CSS3/HTML5 | 全面支持 | 需要 GemBox.Document | IronPdf:现代网络标准 | |
| JavaScript 执行 | 完全支持 JavaScript | 不支持 JavaScript | IronPdf:动态内容 | |
| 渲染速度 | ~125ms 典型值 | 不适用(不支持 HTML) | IronPdf:快速渲染 | |
| 开发人员经验 | 代码示例 | 100 多个可直接运行的示例 | 提供 100 多个示例 | 两方面:广泛的资源 |
| 文档 | 教程、操作方法、视频 | API 文档重点 | IronPdf:多种学习途径 | |
| 内容处理 | 合并/拆分 PDF | 内置方法 | 内置方法 | 两者:标准功能 |
| 文本提取 | 支持 Unicode,集成 OCR | 支持 Unicode,内置 OCR | GemBox:内置 OCR | |
| 表单支持 | HTML 表单到 PDF 表单 | 创建、填充、扁平化表单 | 与上下文相关 | |
| 性能指标 | 内存使用 | 一般在 10MB 以下 | 低内存占用 | 两者:高效 |
| 线程支持 | 本地异步/等待优化 | 线程安全操作 | IronPdf:更好的异步支持 | |
| Security & Encryption | 加密级别 | AES-256、自定义处理程序 | AES-256 标准 | 两者:行业标准 |
| 数字签名 | 集成的可视化签名 | 数字签名支持 | 两者:签名支持 | |
| Licensing & Pricing | 入门级 | Lite:<代码>$liteLicense代码> | 单个开发人员: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 | 与上下文相关 |
什么是IronPDF库?
IronPDF作为一款全面的.NET PDF库脱颖而出,专为使HTML到PDF转换对于开发人员无缝进行而设计。 由Chrome渲染引擎为核心构建,IronPDF改变了开发人员在C#,F#和VB.NET应用程序中处理PDF生成的方式。
该库的哲学中心在于利用现有的网页开发技能。 开发人员可以使用熟悉的HTML、CSS和JavaScript来创建复杂的PDF文档,而不是学习复杂的PDF API。 这种方法大大缩短了开发时间,同时确保像素级别匹配现代网页标准的渲染。
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处理包含渐变、表格、动态JavaScript内容和高级PDF功能(如安全设置和元数据)的复杂HTML布局的能力。 库无缝处理一切,生成的专业PDF与HTML设计完全一致。
什么是GemBox.Pdf库?
GemBox.Pdf代表了一种不同的.NET中PDF操作方法。 与其专注于HTML到PDF转换,GemBox.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渲染引擎,它可以像网页浏览器一样精确处理HTML、CSS和JavaScript。 这意味着: -**真正的浏览器渲染:**使用与 Google Chrome 相同的 Blink 引擎 -**完全符合 Web 标准:**全面支持 CSS3、HTML5、Flexbox、Grid 和现代 JavaScript -**动态内容:**在渲染之前执行 JavaScript,捕获 AJAX 内容和动态元素 -**响应式设计:**处理媒体查询和响应式布局 -**外部资源:**自动从 URL 获取图像、样式表和字体 ### GemBox对HTML支持的局限 GemBox.Pdf自身**不支持HTML到PDF的转换**。 要用GemBox将HTML转换为PDF,开发人员必须: 1. 分别购买GemBox.Document(额外$890成本) 2. 使用GemBox.Document将HTML转换为DOCX 3. 然后将DOCX转换为PDF 这种方法有显著的限制: -**不支持 JavaScript:**无法处理动态内容 - **CSS 支持有限:**不支持许多现代 CSS 特性。 -**样式问题:**已知边框和复杂布局存在问题,详情请参阅[Stack Overflow 讨论。](https://stackoverflow.com/questions/53113688/gembox-document-convert-html-to-pdf-border-css-does-not-work) -**仅限打印优化:**需要专门针对打印进行格式化的 HTML 代码 -**额外费用:**需要购买两个独立的图书馆 ## 现代CSS框架支持:关键差异 当使用现代CSS框架如Bootstrap、Tailwind CSS和Foundation时,IronPDF基于Chrome的渲染的最大优势显而易见。 这些框架已成为现代Web应用程序的主干,其在PDF中正确渲染的能力对许多用例至关重要。 ### IronPDF:完整的现代框架支持 IronPDF的Chrome渲染引擎提供了对所有现代CSS框架的完整、原生支持: - **Bootstrap 5:**全面支持 flexbox 和 CSS Grid,实现响应式布局 - **Tailwind CSS:**完全支持实用性优先的 CSS 框架 -**实际应用示例:**完美渲染[Bootstrap 首页](https://getbootstrap.com/)和[Bootstrap 模板](https://getbootstrap.com/docs/5.3/examples/)。 -**现代 CSS3 特性:**动画、变换、过渡、自定义属性 -**响应式设计:**媒体查询和基于视口的布局无缝协作 **示例代码:Bootstrap表单到PDF** ```csharp using IronPdf; var renderer = new ChromePdfRenderer(); string bootstrapForm = @"Customer Registration
"; 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)引入了不一致性 根据[开发者讨论](https://stackoverflow.com/questions/53113688/gembox-document-convert-html-to-pdf-border-css-does-not-work),即使像边框和布局这样的基本CSS功能在GemBox的HTML转换方法中也可能有问题。 **实际影响:**如果您的应用程序使用 Bootstrap 作为其 UI,并且您需要生成与 Web 界面匹配的 PDF 报告或导出文件,IronPDF 提供了一个简单的解决方案,而 GemBox 则需要进行大量的重新设计或手动变通。 有关 CSS 框架兼容性的更多详细信息,请参见 [Bootstrap & Flexbox CSS 指南](/troubleshooting/bootstrap-flex-css/)。 ## 哪个库在不同的PDF任务中表现出色? 了解每个库的优点有助于开发人员根据其具体需求选择合适的工具。 ### IronPDF表现出色的地方 IronPDF在以下场景中表现卓越: #### 1. **Web应用程序PDF生成** 非常适合SaaS应用程序、Web门户以及任何需要将Web内容转换为PDF的系统。 Chrome渲染引擎确保了复杂的Web布局,包括使用Bootstrap、Tailwind CSS或自定义框架的布局能完美呈现。 #### 2. **动态报告生成** 当报告包含图表(Chart.js, D3.js)、动态数据可视化或JavaScript渲染的内容时,IronPDF能准确捕获一切。 这使其成为商业智能仪表板和数据驱动应用程序的理想选择。 #### 3. **发票和文档模板** 使用HTML/CSS模板用于发票、收据和商业文档,允许设计师在不学习特定的PDF API的情况下创建模板。可以通过熟悉的网页技术快速进行更改。 #### 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需求** 凭借内置的OCR功能,GemBox.Pdf能够从扫描文档中提取文本,而无需额外的库,适用于文档数字化项目。 #### 4. **移动开发** 原生对Android和iOS平台的支持使得GemBox.Pdf成为需要PDF功能的移动应用程序的不错选择。 #### 5. **现有PDF的修改** 当主要工作是处理现有PDF而不是生成新的PDF时,GemBox.Pdf的提取、操作和修改工具非常适合这个任务。 ## 安装和设置的对比如何? 两者都通过NuGet提供简单的安装,但复杂程度不同。 ### 安装 IronPDF。 IronPDF的安装简单得惊人: ```shell :ProductInstall ``` 或者通过.NET CLI: ```shell :InstallCmd dotnet add package IronPdf ``` 该包包含所有必要的东西,包括Chrome渲染引擎。对于大多数用例,无需额外配置或依赖。 对于特定的部署场景,IronPDF提供: **`IronPdf.Slim` :**轻量级软件包,可在运行时下载特定于平台的组件。 - **`IronPdf.Linux` :**已预配置用于 Linux 部署 - **`IronPdf.MacOs` :**针对 macOS 环境进行了优化 ### 安装GemBox.Pdf GemBox.Pdf的基本PDF操作安装同样简单: ```shell :InstallCmd Install-Package GemBox.Pdf ``` 然而,要进行HTML到PDF的转换,您需要: ```shell :InstallCmd Install-Package GemBox.Document ``` 两者都需要许可证密钥配置: ```csharp // IronPDF License.LicenseKey = "YOUR-LICENSE-KEY"; // GemBox.Pdf ComponentInfo.SetLicense("YOUR-LICENSE-KEY"); ```许可和定价有何不同? 了解许可证模型有助于确定每个解决方案的总拥有成本。 ### IronPDF许可 IronPDF提供透明的分级许可(截至2025年的价格): - **Lite License:** $799 - 1位开发者 - 1个项目 - 1年的支持和更新 - **Plus License:** $1,199 - 3位开发者 - 3个项目 - 1年的支持和更新 -**专业许可证:** $2,399 - 10位开发者 - 10个项目 - 1年的支持和更新 **附加选项:** - 免版税再分发:+$2,399 - 提供SaaS/OEM许可 - [Iron Suite捆绑包](https://ironsoftware.com/suite/):$1,498,包含9个产品 **主要优势:** - 30天退款保证 - 永久许可(一次性购买) - 免费开发和测试 - 包含开发/登台/生产部署 ### GemBox.Pdf许可 GemBox提供每开发者许可: -**单个开发商:** 890 美元 - 1位开发者 - 无限项目 - 1年的支持和更新 - 包括免版税的部署 **重要注意事项:** - HTML到PDF需要GemBox.Document:额外$890 - HTML到PDF的总成本:至少$1,780 - 续费时有40%折扣(如果在到期前续费则为60%) - GemBox捆绑包:$2,200,包含7个产品 **主要优势:** - 30天退款保证 - 永久许可 - 包括无限部署 - 不需要服务器或OEM许可 ### 常见场景的成本对比 | 情景 | IronPDF成本 | GemBox成本 | |----------|--------------|-------------| | 单一开发者,HTML到PDF |<代码>$liteLicense代码>[开始使用IronPDF的免费试用版](trial-license),体验在.NET应用程序中Chrome品质渲染的强大功能。 凭借全面的文档、丰富的代码示例和快速响应的支持,您将在几分钟内而不是几个小时内生成PDF。请注意GemBox.Pdf 和 GemBox.Document 是其各自所有者的注册商标。 本网站与GemBox.Pdf或GemBox.Document无关联、未经认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。
Customer Registration
常见问题解答
如何在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执行、Web字体和响应式设计。
对于一个小型开发团队,哪个库更具成本效益?
对于一个需要HTML到PDF转换的三人开发团队,IronPDF的Plus许可证费用为$1,499,而GemBox则需要$5,340购买3个GemBox.Pdf许可证和3个GemBox.Document许可证。IronPDF为团队提供了显著更好的价值。
IronPDF适合大批量PDF生成吗?
是的,IronPDF提供了卓越的性能,HTML到PDF的渲染时间约为125ms,优化的异步/等待支持,以及高效的内存使用,低于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提供了一个免费的评估选项,允许在没有许可证密钥的情况下进行无限制的开发和测试,尽管输出上会出现水印。这提供了在购买前评估产品的足够功能。






