IronPDF 與 PDFSharpCore:2025 年您應該選擇哪個 .NET PDF 函式庫?
在開發需要 PDF 生成和操作功能的現代 .NET 應用程式時,選擇合適的程式庫會對專案的成功產生重大影響。 .NET 生態系中兩個突出的選擇是IronPDF和PDFSharpCore ,它們各自提供了不同的 PDF 處理方法。 這份全面的比較報告將幫助您根據自身俱體需求、預算和技術要求做出明智的決定。
快速對比概覽
在深入探討技術細節之前,這裡有一個全面的比較表格,總結了 IronPDF 和 PDFSharpCore 之間的主要差異:
| 類別 | 特徵/方面 | IronPDF | PDFSharpCore | 主要優勢 |
|---|---|---|---|---|
| 核心架構 | 設計理念 | 基於 Chrome 的渲染,直覺的 API | 輕量級手動 PDF 構建 | IronPDF:更快的開發速度 |
| API複雜度 | 像RenderHtmlAsPdf()這樣的簡單方法 | 使用 XGraphics 進行手動繪圖 | IronPDF:代碼量減少 70%。 | |
| 學習曲線 | 通常需要1-2天。 | 通常需要 3-5 天。 | IronPDF:更快的採用 | |
| 平台支援 | 跨平台 | 原生支持,無需額外軟體包 | 完全跨平台支援 | 兩者:現代部署 |
| .NET 版本 | .NET 10、9、8、7、6、5,Core 3.1+,Framework 4.6.2+ | .NET 6+,.NET Standard 2.0 | IronPDF:更廣泛的相容性 | |
| 作業系統 | Windows、Linux、macOS、Docker、Azure、AWS | Windows、Linux、macOS | IronPDF:雲端最佳化版 | |
| HTML 轉 PDF | 渲染引擎 | 全鍍鉻V8發動機 | 不支援原生 HTML | IronPDF:HTML 功能 |
| CSS3/HTML5 支持 | 全面支持 | 需要第三方函式庫 | IronPDF:現代網路標準 | |
| JavaScript 執行 | 完全支援 JavaScript | 不適用 | IronPDF:動態內容 | |
| 核心功能 | 文字擷取 | 內建ExtractAllText() | 支持有限 | IronPDF:卓越的萃取性能 |
| 水印 | 基於 HTML/CSS 的完整樣式 | 需要手繪 | IronPDF:豐富的浮水印 | |
| 數位簽名 | 整合式視覺簽名 | Not supported | IronPDF:企業安全 | |
| 加密 | AES-256,自訂處理程序 | 基本加密支援 | IronPDF:進階安全 | |
| 頁首/頁尾 | 基於 HTML 的動態內容 | 手動定位 | IronPDF:動態標題 | |
| 表現 | HTML渲染速度 | 0.8-2秒(典型值,Chrome引擎) | 不適用 | IronPDF:HTML渲染 |
| 大型文件處理 | 針對規模進行了最佳化 | 記憶體高效 | PDFSharpCore:佔用記憶體更少 | |
| 螺紋支援 | 原生 async/await,平行處理 | 線程安全操作 | IronPDF:更好的擴充性 | |
| 開發者體驗 | 文件 | 豐富的教學、API 文件和視頻 | 基本文檔 | IronPDF:更好的資源 |
| 程式碼範例 | 100 多個可直接運作的樣品 | 社區範例 | IronPDF:大量樣本 | |
| IntelliSense 支援 | 完整的智慧感知功能,XML 文檔 | 標準 IntelliSense | 兩者:IDE 集成 | |
| Licensing & Pricing | 許可證類型 | 商業性的,永久的 | MIT許可證(免費) | PDFSharpCore:免費 |
| 入門價格 | Lite: $799 (1 dev, 1 project) | 自由的 | PDFSharpCore:零成本 | |
| 支援 | 包含 24/5 全天候工程支持 | 僅社區支持 | IronPDF:專業支持 | |
| 最適合 | 用例 | Web應用程式、報表、企業 | 簡單的PDF文件,預算項目 | 上下文相關 |
IronPDF 和 PDFSharpCore 簡介
IronPDF是什麼?
IronPDF是一個功能全面的商業 .NET 程式庫,旨在讓開發人員輕鬆產生、編輯和操作 PDF 文件。 它基於 Chrome 渲染引擎構建,擅長將 HTML、CSS 和 JavaScript 內容轉換為像素級完美的 PDF。 該庫提供了一套豐富的功能,包括HTML 轉 PDF 、數位簽章、浮水印、 PDF 加密和表單管理。
IronPDF 支援現代 .NET 版本,包括 .NET 10、9、8、7、6、5、Core 3.1+ 和 Framework 4.6.2+,使其能夠靈活地用於新的和傳統的應用程式。 其雲端最佳化架構可確保在Azure 、 AWS和Docker環境中無縫部署。
什麼是 PDFSharpCore?
PDFSharpCore是一個開源函式庫,它是原始 PDFsharp 函式庫的 .NET Core 移植版。 該軟體以 MIT 許可證發布,專注於以程式設計方式建立和基本操作 PDF 文件,而無需依賴 Windows 特定的程式庫。 這使其成為在 Linux、macOS 和 Windows 上運行的跨平台專案的絕佳選擇。
雖然 PDFSharpCore 不提供原生 HTML 到 PDF 的轉換功能,但它透過其繪圖 API 提供了對 PDF 文件建立的精確控制。 開發人員可以使用基於座標的繪圖命令,透過定位文字、圖像和圖形來手動建立 PDF 文件。
安裝和設定
安裝 IronPDF
要開始在您的專案中使用 IronPDF,您可以透過 NuGet 套件管理器輕鬆安裝它。 請依照以下步驟操作:
- 在 Visual Studio 中開啟你的專案。
- 導覽至"工具" > "NuGet 套件管理器" > "管理解決方案的 NuGet 套件" 。
- 在 NuGet 管理器中搜尋IronPdf 。
- 選擇您的項目,然後按一下"安裝"將 IronPDF 新增到您的項目中。
IronPDF 與 PDFSharpCore 的比較:圖 1 - 透過 NuGet 套件管理器安裝 IronPDF 透過 Visual Studio 的 NuGet 套件管理器介面安裝 IronPDF
或者,您可以使用軟體包管理器控制台,透過以下命令安裝 IronPDF:
Install-Package IronPdf
安裝 PDFSharpCore
若要使用 NuGet 安裝 PDFSharpCore,請依照以下說明操作:
- 確保您的 Visual Studio 專案已開啟。
- 前往"工具" > "NuGet 套件管理員" > "管理解決方案的 NuGet 套件" 。
- 在 NuGet 套件管理器中,搜尋PDFSharpCore 。
- 選擇您的項目,然後按一下"安裝"以整合 PDFSharpCore。
IronPDF 與 PDFSharpCore 的比較:圖 2 - 透過 NuGet 安裝 PDFSharpCore 透過 NuGet 套件管理器安裝 PDFSharpCore
對於喜歡使用程式包管理器控制台的開發者,可以使用以下命令安裝 PDFSharpCore:
Install-Package PdfSharpCore
建立 PDF 檔案:IronPDF 與 PDFSharpCore
IronPDF:一種基於現代 HTML 的方法
IronPDF 利用開發人員已經熟悉的網路技術,徹底革新了 PDF 創建方式。 其基於 Chrome 的渲染引擎可確保您的 HTML、CSS 和 JavaScript 程式碼能夠像在現代瀏覽器中一樣渲染。
使用進階功能將 HTML 字串轉換為 PDF
IronPDF 的HTML 轉 PDF功能遠遠超越簡單的文字渲染。 以下是一個更強大的範例:
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main(string[] args)
{
// Apply your license key (required for production)
License.LicenseKey = "Your-License-Key";
// Create renderer with optimized settings
var renderer = new ChromePdfRenderer()
{
RenderingOptions = new ChromePdfRenderOptions()
{
// Set margins for professional appearance
MarginTop = 25,
MarginBottom = 25,
MarginLeft = 20,
MarginRight = 20,
// Enable JavaScript execution for dynamic content
EnableJavaScript = true,
// Wait for AJAX/animations to complete
RenderDelay = 500,
// Set paper orientation and size
PaperOrientation = PdfPaperOrientation.Portrait,
PaperSize = PdfPaperSize.A4,
// Enable printing of background colors and images
PrintHtmlBackgrounds = true
}
};
// HTML with Bootstrap styling and charts
string htmlContent = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
<script src='https://cdn.jsdelivr.net/npm/chart.js'></script>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; }
.chart-container { width: 100%; height: 300px; margin: 20px 0; }
</style>
</head>
<body>
<div class='header text-center'>
<h1>2024 Sales Performance Report</h1>
<p class='lead'>Comprehensive Analysis & Insights</p>
</div>
<div class='container mt-4'>
<div class='row'>
<div class='col-md-6'>
<div class='card'>
<div class='card-body'>
<h5 class='card-title'>Q1 Performance Metrics</h5>
<table class='table table-striped'>
<thead>
<tr>
<th>Month</th>
<th>Revenue</th>
<th>Growth</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$50,000</td>
<td><span class='badge bg-success'>+12%</span></td>
</tr>
<tr>
<td>February</td>
<td>$55,000</td>
<td><span class='badge bg-success'>+10%</span></td>
</tr>
<tr>
<td>March</td>
<td>$60,000</td>
<td><span class='badge bg-success'>+9%</span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class='col-md-6'>
<div class='card'>
<div class='card-body'>
<h5 class='card-title'>Revenue Trend</h5>
<canvas id='revenueChart'></canvas>
</div>
</div>
</div>
</div>
<div class='alert alert-info mt-4'>
<strong>Key Insight:</strong> Q1 showed consistent growth across all months,
with total revenue reaching $165,000, representing a 31% increase YoY.
</div>
</div>
<script>
// Create an interactive chart
const ctx = document.getElementById('revenueChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: ['January', 'February', 'March'],
datasets: [{
label: 'Revenue',
data: [50000, 55000, 60000],
borderColor: '#667eea',
backgroundColor: 'rgba(102, 126, 234, 0.1)',
tension: 0.4
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
</script>
</body>
</html>";
// Render the HTML to PDF
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Add metadata for better document management
pdf.MetaData.Author = "Sales Department";
pdf.MetaData.Title = "Q1 2024 Sales Report";
pdf.MetaData.Subject = "Quarterly Performance Analysis";
pdf.MetaData.Keywords = "sales, performance, Q1, 2024";
pdf.MetaData.CreationDate = DateTime.Now;
// Save the PDF
pdf.SaveAs("sales-report-q1-2024.pdf");
Console.WriteLine("Professional sales report generated successfully!");
}
}using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main(string[] args)
{
// Apply your license key (required for production)
License.LicenseKey = "Your-License-Key";
// Create renderer with optimized settings
var renderer = new ChromePdfRenderer()
{
RenderingOptions = new ChromePdfRenderOptions()
{
// Set margins for professional appearance
MarginTop = 25,
MarginBottom = 25,
MarginLeft = 20,
MarginRight = 20,
// Enable JavaScript execution for dynamic content
EnableJavaScript = true,
// Wait for AJAX/animations to complete
RenderDelay = 500,
// Set paper orientation and size
PaperOrientation = PdfPaperOrientation.Portrait,
PaperSize = PdfPaperSize.A4,
// Enable printing of background colors and images
PrintHtmlBackgrounds = true
}
};
// HTML with Bootstrap styling and charts
string htmlContent = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
<script src='https://cdn.jsdelivr.net/npm/chart.js'></script>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; }
.chart-container { width: 100%; height: 300px; margin: 20px 0; }
</style>
</head>
<body>
<div class='header text-center'>
<h1>2024 Sales Performance Report</h1>
<p class='lead'>Comprehensive Analysis & Insights</p>
</div>
<div class='container mt-4'>
<div class='row'>
<div class='col-md-6'>
<div class='card'>
<div class='card-body'>
<h5 class='card-title'>Q1 Performance Metrics</h5>
<table class='table table-striped'>
<thead>
<tr>
<th>Month</th>
<th>Revenue</th>
<th>Growth</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$50,000</td>
<td><span class='badge bg-success'>+12%</span></td>
</tr>
<tr>
<td>February</td>
<td>$55,000</td>
<td><span class='badge bg-success'>+10%</span></td>
</tr>
<tr>
<td>March</td>
<td>$60,000</td>
<td><span class='badge bg-success'>+9%</span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class='col-md-6'>
<div class='card'>
<div class='card-body'>
<h5 class='card-title'>Revenue Trend</h5>
<canvas id='revenueChart'></canvas>
</div>
</div>
</div>
</div>
<div class='alert alert-info mt-4'>
<strong>Key Insight:</strong> Q1 showed consistent growth across all months,
with total revenue reaching $165,000, representing a 31% increase YoY.
</div>
</div>
<script>
// Create an interactive chart
const ctx = document.getElementById('revenueChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: ['January', 'February', 'March'],
datasets: [{
label: 'Revenue',
data: [50000, 55000, 60000],
borderColor: '#667eea',
backgroundColor: 'rgba(102, 126, 234, 0.1)',
tension: 0.4
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
</script>
</body>
</html>";
// Render the HTML to PDF
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Add metadata for better document management
pdf.MetaData.Author = "Sales Department";
pdf.MetaData.Title = "Q1 2024 Sales Report";
pdf.MetaData.Subject = "Quarterly Performance Analysis";
pdf.MetaData.Keywords = "sales, performance, Q1, 2024";
pdf.MetaData.CreationDate = DateTime.Now;
// Save the PDF
pdf.SaveAs("sales-report-q1-2024.pdf");
Console.WriteLine("Professional sales report generated successfully!");
}
}本範例展示了 IronPDF 的幾項進階功能:
- Bootstrap 整合:利用流行的 CSS 框架實現專業樣式
- JavaScript 圖表:在 PDF 中渲染動態 Chart.js 視覺化圖表 -響應式設計:智慧處理響應式佈局 -元資料管理:為文件管理系統新增可搜尋的元數據 -渲染延遲:確保 JavaScript 內容在渲染之前完全載入完畢
ChromePdfRenderer類別提供了對渲染過程的全面控制。 主要選項包括:
EnableJavaScript:在渲染之前執行 JavaScript 程式碼RenderDelay:等待非同步內容載入完成PrintHtmlBackgrounds:保留背景顏色和影像PaperOrientation和PaperSize:控制頁面佈局- 為實現專業文件外觀而設定的頁邊距
轉換 HTML 文件和 URL
IronPDF在轉換現有HTML文件和即時網頁方面也表現出色:
using IronPdf;
class Program
{
static async Task Main(string[] args)
{
License.LicenseKey = "Your-License-Key";
var renderer = new ChromePdfRenderer();
// Convert a local HTML file with external resources
var filePdf = renderer.RenderHtmlFileAsPdf(@"C:\Reports\template.html");
filePdf.SaveAs("from-file.pdf");
// Convert a URL with authentication
renderer.LoginCredentials = new ChromeHttpLoginCredentials()
{
Username = "user@example.com",
Password = "secure-password"
};
// Render a password-protected page
var urlPdf = await renderer.RenderUrlAsPdfAsync("https://secure.example.com/reports");
urlPdf.SaveAs("secure-report.pdf");
}
}using IronPdf;
class Program
{
static async Task Main(string[] args)
{
License.LicenseKey = "Your-License-Key";
var renderer = new ChromePdfRenderer();
// Convert a local HTML file with external resources
var filePdf = renderer.RenderHtmlFileAsPdf(@"C:\Reports\template.html");
filePdf.SaveAs("from-file.pdf");
// Convert a URL with authentication
renderer.LoginCredentials = new ChromeHttpLoginCredentials()
{
Username = "user@example.com",
Password = "secure-password"
};
// Render a password-protected page
var urlPdf = await renderer.RenderUrlAsPdfAsync("https://secure.example.com/reports");
urlPdf.SaveAs("secure-report.pdf");
}
}PDFSharpCore:手動文件構建
PDFSharpCore 採用了一種截然不同的方法,它要求開發人員使用繪圖命令手動建立 PDF 文件。 雖然這種方法可以實現精確控制,但對於複雜的佈局,需要編寫更多的程式碼。
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System;
class Program
{
static void Main()
{
// Create a new PDF document
var document = new PdfDocument();
document.Info.Title = "Sales Report Q1 2024";
document.Info.Author = "Sales Department";
// Add a page
var page = document.AddPage();
page.Size = PdfSharpCore.PageSize.A4;
// Create graphics object for drawing
var gfx = XGraphics.FromPdfPage(page);
// Define fonts
var titleFont = new XFont("Arial", 24, XFontStyle.Bold);
var headingFont = new XFont("Arial", 14, XFontStyle.Bold);
var normalFont = new XFont("Arial", 11, XFontStyle.Regular);
// Draw title with gradient-like effect (manual implementation)
var titleBrush = new XLinearGradientBrush(
new XPoint(0, 0),
new XPoint(page.Width, 0),
XColors.DarkBlue,
XColors.Purple
);
gfx.DrawRectangle(titleBrush, 0, 0, page.Width, 80);
gfx.DrawString("2024 Sales Performance Report",
titleFont, XBrushes.White,
new XRect(0, 20, page.Width, 40),
XStringFormats.TopCenter);
// Draw table manually
double yPosition = 120;
double margin = 50;
double columnWidth = (page.Width - 2 * margin) / 3;
// Table header
gfx.DrawRectangle(XBrushes.LightGray, margin, yPosition, page.Width - 2 * margin, 25);
gfx.DrawString("Month", headingFont, XBrushes.Black,
new XRect(margin, yPosition, columnWidth, 25),
XStringFormats.Center);
gfx.DrawString("Revenue", headingFont, XBrushes.Black,
new XRect(margin + columnWidth, yPosition, columnWidth, 25),
XStringFormats.Center);
gfx.DrawString("Growth", headingFont, XBrushes.Black,
new XRect(margin + 2 * columnWidth, yPosition, columnWidth, 25),
XStringFormats.Center);
// Table data
string[,] data = {
{ "January", "$50,000", "+12%" },
{ "February", "$55,000", "+10%" },
{ "March", "$60,000", "+9%" }
};
yPosition += 25;
for (int i = 0; i < 3; i++)
{
// Alternate row colors
if (i % 2 == 0)
{
gfx.DrawRectangle(XBrushes.WhiteSmoke, margin, yPosition, page.Width - 2 * margin, 20);
}
for (int j = 0; j < 3; j++)
{
gfx.DrawString(data[i, j], normalFont, XBrushes.Black,
new XRect(margin + j * columnWidth, yPosition, columnWidth, 20),
XStringFormats.Center);
}
yPosition += 20;
}
// Draw a simple line chart (very basic implementation)
yPosition += 40;
gfx.DrawString("Revenue Trend", headingFont, XBrushes.Black,
new XRect(margin, yPosition, page.Width - 2 * margin, 25),
XStringFormats.TopLeft);
// Chart area
yPosition += 30;
double chartHeight = 150;
double chartWidth = page.Width - 2 * margin;
// Draw axes
gfx.DrawLine(XPens.Black, margin, yPosition + chartHeight, margin + chartWidth, yPosition + chartHeight);
gfx.DrawLine(XPens.Black, margin, yPosition, margin, yPosition + chartHeight);
// Plot points (simplified)
double[] revenues = { 50000, 55000, 60000 };
double maxRevenue = 65000;
double xStep = chartWidth / 3;
for (int i = 0; i < revenues.Length; i++)
{
double x = margin + (i + 0.5) * xStep;
double y = yPosition + chartHeight - (revenues[i] / maxRevenue * chartHeight);
// Draw point
gfx.DrawEllipse(XBrushes.Blue, x - 3, y - 3, 6, 6);
// Draw connecting lines
if (i > 0)
{
double prevX = margin + (i - 0.5) * xStep;
double prevY = yPosition + chartHeight - (revenues[i - 1] / maxRevenue * chartHeight);
gfx.DrawLine(new XPen(XColors.Blue, 2), prevX, prevY, x, y);
}
// Labels
gfx.DrawString($"${revenues[i]:N0}", normalFont, XBrushes.Black,
new XRect(x - 30, y - 20, 60, 15),
XStringFormats.TopCenter);
}
// Save the document
document.Save("pdfsharp-sales-report.pdf");
Console.WriteLine("PDF created with PDFSharpCore");
}
}using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System;
class Program
{
static void Main()
{
// Create a new PDF document
var document = new PdfDocument();
document.Info.Title = "Sales Report Q1 2024";
document.Info.Author = "Sales Department";
// Add a page
var page = document.AddPage();
page.Size = PdfSharpCore.PageSize.A4;
// Create graphics object for drawing
var gfx = XGraphics.FromPdfPage(page);
// Define fonts
var titleFont = new XFont("Arial", 24, XFontStyle.Bold);
var headingFont = new XFont("Arial", 14, XFontStyle.Bold);
var normalFont = new XFont("Arial", 11, XFontStyle.Regular);
// Draw title with gradient-like effect (manual implementation)
var titleBrush = new XLinearGradientBrush(
new XPoint(0, 0),
new XPoint(page.Width, 0),
XColors.DarkBlue,
XColors.Purple
);
gfx.DrawRectangle(titleBrush, 0, 0, page.Width, 80);
gfx.DrawString("2024 Sales Performance Report",
titleFont, XBrushes.White,
new XRect(0, 20, page.Width, 40),
XStringFormats.TopCenter);
// Draw table manually
double yPosition = 120;
double margin = 50;
double columnWidth = (page.Width - 2 * margin) / 3;
// Table header
gfx.DrawRectangle(XBrushes.LightGray, margin, yPosition, page.Width - 2 * margin, 25);
gfx.DrawString("Month", headingFont, XBrushes.Black,
new XRect(margin, yPosition, columnWidth, 25),
XStringFormats.Center);
gfx.DrawString("Revenue", headingFont, XBrushes.Black,
new XRect(margin + columnWidth, yPosition, columnWidth, 25),
XStringFormats.Center);
gfx.DrawString("Growth", headingFont, XBrushes.Black,
new XRect(margin + 2 * columnWidth, yPosition, columnWidth, 25),
XStringFormats.Center);
// Table data
string[,] data = {
{ "January", "$50,000", "+12%" },
{ "February", "$55,000", "+10%" },
{ "March", "$60,000", "+9%" }
};
yPosition += 25;
for (int i = 0; i < 3; i++)
{
// Alternate row colors
if (i % 2 == 0)
{
gfx.DrawRectangle(XBrushes.WhiteSmoke, margin, yPosition, page.Width - 2 * margin, 20);
}
for (int j = 0; j < 3; j++)
{
gfx.DrawString(data[i, j], normalFont, XBrushes.Black,
new XRect(margin + j * columnWidth, yPosition, columnWidth, 20),
XStringFormats.Center);
}
yPosition += 20;
}
// Draw a simple line chart (very basic implementation)
yPosition += 40;
gfx.DrawString("Revenue Trend", headingFont, XBrushes.Black,
new XRect(margin, yPosition, page.Width - 2 * margin, 25),
XStringFormats.TopLeft);
// Chart area
yPosition += 30;
double chartHeight = 150;
double chartWidth = page.Width - 2 * margin;
// Draw axes
gfx.DrawLine(XPens.Black, margin, yPosition + chartHeight, margin + chartWidth, yPosition + chartHeight);
gfx.DrawLine(XPens.Black, margin, yPosition, margin, yPosition + chartHeight);
// Plot points (simplified)
double[] revenues = { 50000, 55000, 60000 };
double maxRevenue = 65000;
double xStep = chartWidth / 3;
for (int i = 0; i < revenues.Length; i++)
{
double x = margin + (i + 0.5) * xStep;
double y = yPosition + chartHeight - (revenues[i] / maxRevenue * chartHeight);
// Draw point
gfx.DrawEllipse(XBrushes.Blue, x - 3, y - 3, 6, 6);
// Draw connecting lines
if (i > 0)
{
double prevX = margin + (i - 0.5) * xStep;
double prevY = yPosition + chartHeight - (revenues[i - 1] / maxRevenue * chartHeight);
gfx.DrawLine(new XPen(XColors.Blue, 2), prevX, prevY, x, y);
}
// Labels
gfx.DrawString($"${revenues[i]:N0}", normalFont, XBrushes.Black,
new XRect(x - 30, y - 20, 60, 15),
XStringFormats.TopCenter);
}
// Save the document
document.Save("pdfsharp-sales-report.pdf");
Console.WriteLine("PDF created with PDFSharpCore");
}
}如您所見,即使使用 PDFSharpCore 建立中等複雜程度的文檔,也需要:
- 手動定位每個元素
- 複雜的佈局計算
- 不支援HTML或CSS
- 手動實現圖表和圖形
- 同樣的結果,卻需要寫更多的程式碼
使用 PDFSharpCore 將 HTML 轉換為 PDF
由於 PDFSharpCore 本身不支援 HTML 到 PDF 的轉換,開發人員必須使用第三方函式庫。 常見的選擇是將 PDFSharpCore 與 HtmlRenderer 結合使用:
using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
var document = new PdfDocument();
string htmlContent = File.ReadAllText("template.html");
// Note: HtmlRenderer has limited CSS support
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("from-html.pdf");using PdfSharpCore.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
var document = new PdfDocument();
string htmlContent = File.ReadAllText("template.html");
// Note: HtmlRenderer has limited CSS support
PdfGenerator.AddPdfPages(document, htmlContent, PdfSharpCore.PageSize.A4);
document.Save("from-html.pdf");然而,這種方法有明顯的限制:
- CSS3 支援有限(相容性約 70-80%)
- 不執行 JavaScript 對現代網路功能的處理不佳 與瀏覽器相比,渲染效果不一致。
現代 CSS 框架支援:Bootstrap 及其他
在開發需要從 Web 內容產生 PDF 的應用程式時,Bootstrap 和現代 CSS 框架的支援至關重要。 大多數 Web 應用程式都使用這些框架來實現一致的設計,並且無需修改即可將這些介面轉換為 PDF,從而顯著縮短了開發時間。
IronPDF:完全支援 Bootstrap 和 CSS 框架
Bootstrap 5:完整的 Flexbox 佈局引擎、CSS Grid、實用類別和所有元件系統 Bootstrap 4:完整的卡片元件、導航、彈性佈局工具和響應式類 Tailwind CSS:所有實用類別都能準確渲染 -基礎:完整的網格系統和元件庫 現代CSS3: Flexbox、CSS Grid、自訂屬性、動畫、轉換與變換
實際應用證明:IronPDF 可以以像素等級的精確度渲染Bootstrap 首頁和所有官方範本。
程式碼範例:專案進度儀錶板
using IronPdf;
// Set your IronPDF license key
IronPdf.License.LicenseKey = "License-Key goes here";
var renderer = new ChromePdfRenderer();
string bootstrapProgress = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
<style>
.project-card {
transition: all 0.3s ease;
border-left: 4px solid transparent;
}
.project-card.active { border-left-color: #0d6efd; }
.progress-label { font-size: 0.75rem; font-weight: 600; }
</style>
</head>
<body>
<div class='container my-5'>
<div class='d-flex justify-content-between align-items-center mb-4'>
<div>
<h1 class='display-6 mb-1'>Project Portfolio Status</h1>
<p class='text-muted mb-0'>Q1 2025 Development Pipeline</p>
</div>
<div>
<span class='badge bg-success fs-6'>8 Active Projects</span>
</div>
</div>
<div class='row g-4 mb-4'>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-primary mb-2'>72%</div>
<h6 class='text-muted text-uppercase mb-0'>Overall Progress</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-success mb-2'>5</div>
<h6 class='text-muted text-uppercase mb-0'>On Track</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-warning mb-2'>2</div>
<h6 class='text-muted text-uppercase mb-0'>At Risk</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-danger mb-2'>1</div>
<h6 class='text-muted text-uppercase mb-0'>Delayed</h6>
</div>
</div>
</div>
</div>
<div class='card shadow-sm mb-4'>
<div class='card-header bg-primary text-white'>
<h5 class='mb-0'>Active Projects</h5>
</div>
<div class='card-body p-0'>
<div class='list-group list-group-flush'>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>E-Commerce Platform Redesign</h6>
<small class='text-muted'>Lead: Sarah Chen | Due: Apr 15, 2025</small>
</div>
<span class='badge bg-success'>On Track</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-primary'>85%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-primary' role='progressbar' style='width: 85%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Frontend: 90%</span>
<span class='badge bg-light text-dark'>Backend: 80%</span>
<span class='badge bg-light text-dark'>Testing: 85%</span>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Mobile App Integration</h6>
<small class='text-muted'>Lead: Marcus Johnson | Due: Mar 30, 2025</small>
</div>
<span class='badge bg-success'>On Track</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-success'>92%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-success' role='progressbar' style='width: 92%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>iOS: 95%</span>
<span class='badge bg-light text-dark'>Android: 90%</span>
<span class='badge bg-light text-dark'>API: 100%</span>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Customer Analytics Dashboard</h6>
<small class='text-muted'>Lead: Emily Rodriguez | Due: Apr 22, 2025</small>
</div>
<span class='badge bg-warning text-dark'>At Risk</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-warning'>58%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-warning' role='progressbar' style='width: 58%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Data Pipeline: 65%</span>
<span class='badge bg-light text-dark'>UI: 50%</span>
<span class='badge bg-light text-dark'>Reports: 45%</span>
</div>
<div class='alert alert-warning mt-2 mb-0 py-2'>
<small><strong>Issue:</strong> Resource constraints affecting timeline. Review scheduled for next week.</small>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Security Infrastructure Upgrade</h6>
<small class='text-muted'>Lead: David Kim | Due: Mar 25, 2025</small>
</div>
<span class='badge bg-danger'>Delayed</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-danger'>42%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-danger' role='progressbar' style='width: 42%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Authentication: 60%</span>
<span class='badge bg-light text-dark'>加密: 40%</span>
<span class='badge bg-light text-dark'>Audit Logs: 25%</span>
</div>
<div class='alert alert-danger mt-2 mb-0 py-2'>
<small><strong>Critical:</strong> 7 days behind schedule. Additional resources allocated. Daily standup required.</small>
</div>
</div>
</div>
</div>
</div>
<div class='row g-4'>
<div class='col-md-6'>
<div class='card shadow-sm'>
<div class='card-header bg-white'>
<h5 class='mb-0'>Sprint Velocity</h5>
</div>
<div class='card-body'>
<div class='mb-3'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Current Sprint</span>
<strong>42 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-success' style='width: 84%'>84% Complete</div>
</div>
</div>
<div class='mb-3'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Previous Sprint</span>
<strong>38 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-info' style='width: 100%'>Completed</div>
</div>
</div>
<div class='mb-0'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Average Velocity</span>
<strong>40 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-primary' style='width: 100%'>Baseline</div>
</div>
</div>
</div>
</div>
</div>
<div class='col-md-6'>
<div class='card shadow-sm'>
<div class='card-header bg-white'>
<h5 class='mb-0'>Team Capacity</h5>
</div>
<div class='card-body'>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>Frontend Team</h6>
<small class='text-muted'>6 developers</small>
</div>
<span class='badge bg-success'>92% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>Backend Team</h6>
<small class='text-muted'>5 developers</small>
</div>
<span class='badge bg-success'>88% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>QA Team</h6>
<small class='text-muted'>3 testers</small>
</div>
<span class='badge bg-warning text-dark'>105% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-0'>
<div>
<h6 class='mb-0'>DevOps Team</h6>
<small class='text-muted'>2 engineers</small>
</div>
<span class='badge bg-danger'>110% Utilized</span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(bootstrapProgress);
pdf.SaveAs("project-progress.pdf");using IronPdf;
// Set your IronPDF license key
IronPdf.License.LicenseKey = "License-Key goes here";
var renderer = new ChromePdfRenderer();
string bootstrapProgress = @"
<!DOCTYPE html>
<html>
<head>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
<style>
.project-card {
transition: all 0.3s ease;
border-left: 4px solid transparent;
}
.project-card.active { border-left-color: #0d6efd; }
.progress-label { font-size: 0.75rem; font-weight: 600; }
</style>
</head>
<body>
<div class='container my-5'>
<div class='d-flex justify-content-between align-items-center mb-4'>
<div>
<h1 class='display-6 mb-1'>Project Portfolio Status</h1>
<p class='text-muted mb-0'>Q1 2025 Development Pipeline</p>
</div>
<div>
<span class='badge bg-success fs-6'>8 Active Projects</span>
</div>
</div>
<div class='row g-4 mb-4'>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-primary mb-2'>72%</div>
<h6 class='text-muted text-uppercase mb-0'>Overall Progress</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-success mb-2'>5</div>
<h6 class='text-muted text-uppercase mb-0'>On Track</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-warning mb-2'>2</div>
<h6 class='text-muted text-uppercase mb-0'>At Risk</h6>
</div>
</div>
</div>
<div class='col-md-3'>
<div class='card text-center shadow-sm'>
<div class='card-body'>
<div class='display-4 text-danger mb-2'>1</div>
<h6 class='text-muted text-uppercase mb-0'>Delayed</h6>
</div>
</div>
</div>
</div>
<div class='card shadow-sm mb-4'>
<div class='card-header bg-primary text-white'>
<h5 class='mb-0'>Active Projects</h5>
</div>
<div class='card-body p-0'>
<div class='list-group list-group-flush'>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>E-Commerce Platform Redesign</h6>
<small class='text-muted'>Lead: Sarah Chen | Due: Apr 15, 2025</small>
</div>
<span class='badge bg-success'>On Track</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-primary'>85%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-primary' role='progressbar' style='width: 85%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Frontend: 90%</span>
<span class='badge bg-light text-dark'>Backend: 80%</span>
<span class='badge bg-light text-dark'>Testing: 85%</span>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Mobile App Integration</h6>
<small class='text-muted'>Lead: Marcus Johnson | Due: Mar 30, 2025</small>
</div>
<span class='badge bg-success'>On Track</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-success'>92%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-success' role='progressbar' style='width: 92%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>iOS: 95%</span>
<span class='badge bg-light text-dark'>Android: 90%</span>
<span class='badge bg-light text-dark'>API: 100%</span>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Customer Analytics Dashboard</h6>
<small class='text-muted'>Lead: Emily Rodriguez | Due: Apr 22, 2025</small>
</div>
<span class='badge bg-warning text-dark'>At Risk</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-warning'>58%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-warning' role='progressbar' style='width: 58%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Data Pipeline: 65%</span>
<span class='badge bg-light text-dark'>UI: 50%</span>
<span class='badge bg-light text-dark'>Reports: 45%</span>
</div>
<div class='alert alert-warning mt-2 mb-0 py-2'>
<small><strong>Issue:</strong> Resource constraints affecting timeline. Review scheduled for next week.</small>
</div>
</div>
<div class='list-group-item'>
<div class='d-flex justify-content-between align-items-start mb-3'>
<div>
<h6 class='mb-1'>Security Infrastructure Upgrade</h6>
<small class='text-muted'>Lead: David Kim | Due: Mar 25, 2025</small>
</div>
<span class='badge bg-danger'>Delayed</span>
</div>
<div class='mb-2'>
<div class='d-flex justify-content-between align-items-center mb-1'>
<span class='progress-label text-muted'>COMPLETION</span>
<span class='progress-label text-danger'>42%</span>
</div>
<div class='progress' style='height: 8px;'>
<div class='progress-bar bg-danger' role='progressbar' style='width: 42%'></div>
</div>
</div>
<div class='d-flex gap-2 flex-wrap'>
<span class='badge bg-light text-dark'>Authentication: 60%</span>
<span class='badge bg-light text-dark'>加密: 40%</span>
<span class='badge bg-light text-dark'>Audit Logs: 25%</span>
</div>
<div class='alert alert-danger mt-2 mb-0 py-2'>
<small><strong>Critical:</strong> 7 days behind schedule. Additional resources allocated. Daily standup required.</small>
</div>
</div>
</div>
</div>
</div>
<div class='row g-4'>
<div class='col-md-6'>
<div class='card shadow-sm'>
<div class='card-header bg-white'>
<h5 class='mb-0'>Sprint Velocity</h5>
</div>
<div class='card-body'>
<div class='mb-3'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Current Sprint</span>
<strong>42 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-success' style='width: 84%'>84% Complete</div>
</div>
</div>
<div class='mb-3'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Previous Sprint</span>
<strong>38 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-info' style='width: 100%'>Completed</div>
</div>
</div>
<div class='mb-0'>
<div class='d-flex justify-content-between mb-1'>
<span class='text-muted'>Average Velocity</span>
<strong>40 Story Points</strong>
</div>
<div class='progress' style='height: 20px;'>
<div class='progress-bar bg-primary' style='width: 100%'>Baseline</div>
</div>
</div>
</div>
</div>
</div>
<div class='col-md-6'>
<div class='card shadow-sm'>
<div class='card-header bg-white'>
<h5 class='mb-0'>Team Capacity</h5>
</div>
<div class='card-body'>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>Frontend Team</h6>
<small class='text-muted'>6 developers</small>
</div>
<span class='badge bg-success'>92% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>Backend Team</h6>
<small class='text-muted'>5 developers</small>
</div>
<span class='badge bg-success'>88% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-3'>
<div>
<h6 class='mb-0'>QA Team</h6>
<small class='text-muted'>3 testers</small>
</div>
<span class='badge bg-warning text-dark'>105% Utilized</span>
</div>
<div class='d-flex justify-content-between align-items-center mb-0'>
<div>
<h6 class='mb-0'>DevOps Team</h6>
<small class='text-muted'>2 engineers</small>
</div>
<span class='badge bg-danger'>110% Utilized</span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(bootstrapProgress);
pdf.SaveAs("project-progress.pdf");輸出:專業的專案管理 PDF,採用 Bootstrap 5 的 flexbox 佈局、進度條元件、徽章工具、響應式卡片系統和警報元件——所有元件均以完美的對齊、間距和色彩準確性進行渲染。
PDFSharpCore:不支援原生 HTML
PDFSharpCore沒有原生 HTML 渲染引擎。 該庫專為底層 PDF 處理和繪圖操作而設計:
-不支援 Bootstrap:完全無法處理 HTML/CSS 框架 -不支援 Flexbox 或 CSS Grid:完全不具備任何 CSS 渲染功能。 -僅限手動建立:需要使用基於座標的 API 繪製文字、形狀和圖像 -需要外部工具:必須使用第三方 HTML 渲染器或轉換服務
HtmlRenderer 整合(第三方)僅提供基本的 HTML 支持,存在嚴重的限制——CSS3 相容性約為 70-80%,不支援 flexbox、CSS Grid 和現代框架。
開發影響:團隊要麼完全放棄基於 HTML 的工作流程,要麼整合多個工具(PDFSharpCore + 外部 HTML 渲染器),這會增加複雜性、維護負擔,並可能導致庫版本之間的相容性問題。
有關 Bootstrap 框架相容性和 CSS3 渲染功能的詳細說明,請參閱Bootstrap 和 Flexbox CSS 指南。
進階功能對比
文字擷取
PDF 最常見的操作之一是提取文字以進行索引、分析或轉換。 以下是這兩個庫處理此任務的方式:
IronPDF文字擷取
IronPDF 提供強大的文字擷取功能和簡單的 API:
using IronPdf;
// Extract all text from a PDF
var pdf = PdfDocument.FromFile("report.pdf");
string allText = pdf.ExtractAllText();
// Extract text from specific pages
for (int i = 0; i < pdf.PageCount; i++)
{
string pageText = pdf.ExtractTextFromPage(i);
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, Math.Min(100, pageText.Length))}...");
}
// Extract text from a specific region
var pageIndex = 0;
var region = new Rectangle(50, 50, 200, 100); // x, y, width, height
string regionText = pdf.ExtractTextFromPage(pageIndex, region);using IronPdf;
// Extract all text from a PDF
var pdf = PdfDocument.FromFile("report.pdf");
string allText = pdf.ExtractAllText();
// Extract text from specific pages
for (int i = 0; i < pdf.PageCount; i++)
{
string pageText = pdf.ExtractTextFromPage(i);
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, Math.Min(100, pageText.Length))}...");
}
// Extract text from a specific region
var pageIndex = 0;
var region = new Rectangle(50, 50, 200, 100); // x, y, width, height
string regionText = pdf.ExtractTextFromPage(pageIndex, region);PDFSharpCore文本提取
PDFSharpCore 的原生文字擷取功能有限。 如對比文章所述,它經常產生零散或不完整的結果:
// PDFSharpCore doesn't have reliable text extraction
// This is a significant limitation for many use cases// PDFSharpCore doesn't have reliable text extraction
// This is a significant limitation for many use cases水印
在PDF文件上新增浮水印對於品牌推廣和文件安全至關重要。
IronPDF浮水印
using IronPdf;
var pdf = PdfDocument.FromFile("document.pdf");
// HTML-based watermark with full CSS support
string watermarkHtml = @"
<div style='
font-size: 48px;
color: rgba(255, 0, 0, 0.3);
transform: rotate(-45deg);
text-align: center;
font-weight: bold;
'>
CONFIDENTIAL
</div>";
pdf.ApplyWatermark(watermarkHtml, 50, VerticalAlignment.Middle, HorizontalAlignment.Center);
// Image watermark
pdf.ApplyWatermark("logo.png", 30, VerticalAlignment.Bottom, HorizontalAlignment.Right);
pdf.SaveAs("watermarked.pdf");using IronPdf;
var pdf = PdfDocument.FromFile("document.pdf");
// HTML-based watermark with full CSS support
string watermarkHtml = @"
<div style='
font-size: 48px;
color: rgba(255, 0, 0, 0.3);
transform: rotate(-45deg);
text-align: center;
font-weight: bold;
'>
CONFIDENTIAL
</div>";
pdf.ApplyWatermark(watermarkHtml, 50, VerticalAlignment.Middle, HorizontalAlignment.Center);
// Image watermark
pdf.ApplyWatermark("logo.png", 30, VerticalAlignment.Bottom, HorizontalAlignment.Right);
pdf.SaveAs("watermarked.pdf");PDFSharpCore浮水印
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
var document = PdfReader.Open("document.pdf", PdfDocumentOpenMode.Modify);
foreach (var page in document.Pages)
{
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
// Create watermark font
var font = new XFont("Arial", 48);
// Calculate rotation
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-45);
// Draw watermark
var size = gfx.MeasureString("CONFIDENTIAL", font);
gfx.DrawString("CONFIDENTIAL", font,
new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)),
new XRect(-size.Width / 2, -size.Height / 2, size.Width, size.Height),
XStringFormats.Center);
}
document.Save("watermarked-pdfsharp.pdf");using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
var document = PdfReader.Open("document.pdf", PdfDocumentOpenMode.Modify);
foreach (var page in document.Pages)
{
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
// Create watermark font
var font = new XFont("Arial", 48);
// Calculate rotation
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-45);
// Draw watermark
var size = gfx.MeasureString("CONFIDENTIAL", font);
gfx.DrawString("CONFIDENTIAL", font,
new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)),
new XRect(-size.Width / 2, -size.Height / 2, size.Width, size.Height),
XStringFormats.Center);
}
document.Save("watermarked-pdfsharp.pdf");數位簽名
數位簽章可確保文件的真實性和完整性。
IronPDF 數位簽名
using IronPdf;
using IronPdf.Signing;
var pdf = PdfDocument.FromFile("contract.pdf");
// Create a signature with certificate
var signature = new PdfSignature("certificate.pfx", "password")
{
SigningContact = "legal@company.com",
SigningLocation = "New York, NY",
SigningReason = "Contract Approval"
};
// Add visual signature
var signatureImage = new PdfSignature("certificate.pfx", "password")
{
SignatureImage = new PdfSignatureImage("signature.png", 0, 0, 200, 100)
};
// Apply signature to the last page
pdf.Sign(signature);
pdf.SaveAs("signed-contract.pdf");using IronPdf;
using IronPdf.Signing;
var pdf = PdfDocument.FromFile("contract.pdf");
// Create a signature with certificate
var signature = new PdfSignature("certificate.pfx", "password")
{
SigningContact = "legal@company.com",
SigningLocation = "New York, NY",
SigningReason = "Contract Approval"
};
// Add visual signature
var signatureImage = new PdfSignature("certificate.pfx", "password")
{
SignatureImage = new PdfSignatureImage("signature.png", 0, 0, 200, 100)
};
// Apply signature to the last page
pdf.Sign(signature);
pdf.SaveAs("signed-contract.pdf");PDFSharpCore 數位簽名
PDFSharpCore 本身不支援數位簽名,這對於需要文件安全性的商業應用程式來說是一個重大限制。
表單處理
使用PDF表單對於互動式文件至關重要。
IronPDF 表單處理
using IronPdf;
// Create a form from HTML
var html = @"
<form>
<label>Name: <input type='text' name='fullName' required></label><br>
<label>Email: <input type='email' name='email' required></label><br>
<label>Subscribe: <input type='checkbox' name='subscribe' value='yes'></label><br>
<label>
Plan:
<select name='plan'>
<option value='basic'>Basic</option>
<option value='pro'>Professional</option>
<option value='enterprise'>Enterprise</option>
</select>
</label>
</form>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
// Fill existing form
var filledPdf = PdfDocument.FromFile("application-form.pdf");
filledPdf.Form.FindFormField("fullName").Value = "John Doe";
filledPdf.Form.FindFormField("email").Value = "john@example.com";
filledPdf.Form.FindFormField("subscribe").Value = "yes";
filledPdf.SaveAs("completed-application.pdf");using IronPdf;
// Create a form from HTML
var html = @"
<form>
<label>Name: <input type='text' name='fullName' required></label><br>
<label>Email: <input type='email' name='email' required></label><br>
<label>Subscribe: <input type='checkbox' name='subscribe' value='yes'></label><br>
<label>
Plan:
<select name='plan'>
<option value='basic'>Basic</option>
<option value='pro'>Professional</option>
<option value='enterprise'>Enterprise</option>
</select>
</label>
</form>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
// Fill existing form
var filledPdf = PdfDocument.FromFile("application-form.pdf");
filledPdf.Form.FindFormField("fullName").Value = "John Doe";
filledPdf.Form.FindFormField("email").Value = "john@example.com";
filledPdf.Form.FindFormField("subscribe").Value = "yes";
filledPdf.SaveAs("completed-application.pdf");性能基準
根據廣泛的測試和社群回饋,以下是表現對比:
HTML 轉 PDF 渲染
| 測試用例 | IronPDF | PDFSharpCore(附 HtmlRenderer) |
|---|---|---|
| 簡單HTML(1頁) | 0.8-1.2s | 0.3-0.5s |
| 使用 CSS3 的複雜 HTML | 1.5-2秒 | 經常失敗或渲染錯誤 |
| JavaScript圖表 | 2-3秒 | 不支援 |
| 一份100頁的報告 | 15-20歲 | 45-60歲 |
| 記憶體使用情況 | 150-200MB | 80-120MB |
關鍵績效洞察
IronPDF 的優勢在於:
- 複雜的 HTML 渲染,並完全相容於所有瀏覽器
- 批量操作的平行處理
- 在不同類型的內容中表現一致 -非同步操作以提高可擴展性
- PDFSharpCore在以下方面表現較佳:
- 簡易的程序化 PDF 創建
- 更低的記憶體佔用
- 基本文件修改
實際效能範例
// IronPDF - Batch processing with parallel execution
using IronPdf;
using System.Threading.Tasks;
class BatchProcessor
{
public static async Task ProcessInvoicesAsync(List<string> htmlInvoices)
{
var renderer = new ChromePdfRenderer();
// Process multiple PDFs in parallel
var tasks = htmlInvoices.Select(async (html, index) =>
{
var pdf = await renderer.RenderHtmlAsPdfAsync(html);
await pdf.SaveAsAsync($"invoice-{index}.pdf");
});
await Task.WhenAll(tasks);
}
}// IronPDF - Batch processing with parallel execution
using IronPdf;
using System.Threading.Tasks;
class BatchProcessor
{
public static async Task ProcessInvoicesAsync(List<string> htmlInvoices)
{
var renderer = new ChromePdfRenderer();
// Process multiple PDFs in parallel
var tasks = htmlInvoices.Select(async (html, index) =>
{
var pdf = await renderer.RenderHtmlAsPdfAsync(html);
await pdf.SaveAsAsync($"invoice-{index}.pdf");
});
await Task.WhenAll(tasks);
}
}實際應用案例
何時選擇 IronPDF
IronPDF 非常適合用於:
- Web應用程式集成 將動態網頁內容轉換為 PDF
- 從網頁儀錶板產生報告
- 使用 HTML 範本建立發票
2.企業文件管理
- 實現具有數位簽章的文件工作流程
- 建立安全加密的敏感資料 PDF 文件
- 批量處理大量文檔
- SaaS 應用
- 多租戶 PDF 生成
- 在 Azure/AWS 上進行雲端原生部署
- 高效能異步處理
4.複雜報告 包含圖表和圖形的財務報表
- 包含豐富媒體的行銷資料
- 帶有程式碼高亮顯示的技術文檔
何時選擇 PDFSharpCore
PDFSharpCore 適用於:
1.注重預算的項目
- 沒有許可預算的開源項目
- 簡單的 PDF 生成需求
- 學術或個人項目
- PDF基本操作
- 建立簡單的基於文字的 PDF 文件
- 基本文件合併 添加簡單的圖形或形狀
3.輕量級應用
- 記憶體受限的嵌入式系統
- 簡單的命令列工具
- 依賴項最少的微服務
授權和定價
IronPDF 許可
IronPDF 提供靈活的許可選項(價格截至 2025 年):
-精簡版授權: $799 (1 位開發者,1 個地點,1 個專案)
- Plus License: $1,199 (3 位開發者,3 個地點,3 個專案) -專業授權: $2,399 (10 位開發人員,10 個地點,10 個專案) -免費試用: 30 天完整功能試用
其他福利:
- 永久授權(一次性購買)
- 可免版稅再分發
- 包含 24/5 全天候工程支持
- 一年免費更新
- Iron Suite 套餐可享額外優惠
PDFSharpCore 許可
PDFSharpCore 完全免費,採用 MIT 許可證:
- 無許可證費用
- 商業用途無限制 僅限社區支持
- 不保證更新或修復漏洞。
開發者體驗
文件和資源
IronPDF提供:
-全面文檔
PDFSharpCore提供:
- GitHub 基本文檔
- 社區範例 官方教程有限
支援比較
| 支援類型 | IronPDF | PDFSharpCore |
|---|---|---|
| 專業支援 | 24/5 包含 | 沒有任何 |
| 回應時間 | 24-48小時 | 社區依賴 |
| 直接工程通道 | 是的 | 不 |
| 漏洞修復保證 | 是的 | 不 |
結論
IronPDF 和 PDFSharpCore 在 .NET PDF 生態系統中都扮演著重要的角色,但它們針對的是不同的需求和用例。
在以下情況下選擇 IronPDF:
您需要功能強大的 HTML 轉 PDF 轉換器,並完全支援 CSS3 和 JavaScript。 您的專案需要數位簽章、加密或表單處理等進階功能。
- 您正在建立企業或商業應用程式 您重視全面的文件和專業的支持 性能和可靠性至關重要 您需要雲端就緒部署選項
選擇 PDFSharpCore 的情況:
- 你正在參與一個預算受限或開源項目 您的 PDF 需求很簡單,不需要 HTML 渲染。 您喜歡手動控制 PDF 建置。 記憶體佔用是一個至關重要的問題。
- 你樂於接受社群支持
IronPDF 採用現代化的 PDF 生成方法,並結合其豐富的功能集和出色的支持,使其成為大多數專業應用程式的首選。 雖然需要購買許可證,但節省的開發時間和獲得的可靠性通常足以抵消商業專案的成本。
準備好體驗其中的不同了嗎? 立即開始 IronPDF 的 30 天免費試用,看看它如何改變您的 PDF 生成工作流程。 IronPDF 提供全面的文件、快速回應的支援和功能豐富的 API,使開發人員能夠以最少的努力創建專業的 PDF 文件。
!{--01001100010010010100001001010010010000010101001001011001010 111110100011101000101010101010001011111010100110101010001000001 010100100101010001000101010001000101111101010111010010010101010 001001000010111110101000001010101000010010000101111101010000010 1001001001111010001000101010101000011010101010001011111010101000101001001001001010101010001010010010010010100001010101010101 010101011000010101000100010101001110010001000101010001000101111101000010010011000100111110100010010011000100111100
常見問題解答
如何在C#中將HTML轉換為PDF?
您可以使用 IronPDF 的RenderHtmlAsPdf方法將 HTML 字串轉換為 PDF。此方法支援完整的 CSS3 和 JavaScript 執行,確保高保真渲染效果。
IronPDF 和 PDFSharpCore 的主要差異是什麼?
IronPDF 是一個商業庫,具備 HTML 轉 PDF、數位簽章和雲端平台優化等進階功能。 PDFSharpCore 是開源的,專注於透過手動繪製命令來建立基本的 PDF 文件,並且不具備原生 HTML 轉 PDF 功能。
我可以使用這些庫從PDF中提取文字嗎?
IronPDF 提供強大的文字擷取功能,例如ExtractAllText()和ExtractTextFromPage()方法,並能保持文件結構。 PDFSharpCore 的文字擷取功能有限,通常會導致輸出結果零散。
在 C# 中,向 PDF 添加數位簽章的最佳方法是什麼?
IronPDF 提供全面的數位簽名支持,包括視覺化簽名和證書簽名。它提供可自訂的 PDF 簽名選項,使其適用於安全的文件工作流程。
這些庫在HTML渲染速度方面表現如何?
IronPDF 通常能在 0.8-2 秒內將複雜的 HTML 渲染成 PDF,並完全支援 CSS3 和 JavaScript。 PDFSharpCore 在簡單的程式化 PDF 建立方面速度更快,但不支援現代 Web 技術,這會影響其渲染能力。
是否有可用於評估 PDF 庫的試用版?
IronPDF 提供 30 天免費試用,讓您可以體驗所有功能,包括 HTML 轉 PDF。 PDFSharpCore 採用 MIT 許可證,免費提供基本功能,無需試用期。
哪個庫比較適合處理PDF表單?
IronPDF 在表單處理方面表現出色,能夠從 HTML 建立和填寫表單並提取表單資料。 PDFSharpCore 支援基本的表單處理,但需要手動建立字段,自動化程度不如 IronPDF。
各個函式庫如何支援跨平台開發?
IronPDF 和 PDFSharpCore 都支援 Windows、Linux 和 macOS 系統。 IronPDF 針對 Azure 和 AWS 等雲端平台進行了最佳化配置,而 PDFSharpCore 可能需要在雲端環境中進行額外的設定。
我可以從這些庫中獲得哪些方面的支援?
IronPDF 提供 24/5 全天候專業工程支持,回應時間為 24-48 小時,並提供詳細的文件。 PDFSharpCore 則依賴社群透過論壇和 GitHub 提供的支持,無法保證回應時間。
各個函式庫如何處理複雜 PDF 操作的效能問題?
IronPDF 在處理複雜的 HTML 渲染方面表現出色,並支援並行處理大量操作,從而提供更高的吞吐量。 PDFSharpCore 在處理簡單文件時記憶體效率更高,但缺乏高級渲染功能。






