XFINIUM.PDF與IronPDF:技術比較指南
當.NET開發者評估用於文件生成和操作的PDF程式庫時,XFINIUM.PDF出現為一個跨平台選擇,擁有全面的低層級PDF工具。 然而,其基於座標的圖形程式設計模型與HTML為中心的方法有顯著的差異。 本技術比較將XFINIUM.PDF與IronPDF進行比較,以幫助建築師和開發者了解在PDF生成理念、API設計和現代網頁內容支援方面的基本區別。
了解XFINIUM.PDF
XFINIUM.PDF是一個完全用C#開發的商業跨平台PDF程式庫,旨在為初學者和專業PDF開發者服務。 該程式庫提供了一系列豐富的功能,包括PDF生成、表單填寫、PDF作品集構建、內容編輯和多頁面TIFF轉換。
XFINIUM.PDF提供兩個版本:用於PDF創建和編輯的Generator版和新增渲染和顯示功能的Viewer版。 該程式庫在各平台提供全面的PDF操作工具。
然而,XFINIUM.PDF運行於與網頁為中心的PDF程式庫完全不同的範例:
- 基於座標的API:需要用像素座標手動定位,例如
DrawString("text", font, brush, 50, 100) - 無原生HTML支援:無法將HTML/CSS轉換為PDF——依賴於低層級繪圖原語
- 手動字體管理:必須顯式創建和管理字體對象
- 無CSS樣式:不支援現代網頁樣式——顏色、字體和佈局必須手動處理
- 無JavaScript渲染:僅限靜態內容——不能渲染動態網頁內容
- 複雜的文本佈局:需要手動文本測量和換行計算
圖形程式設計模型
XFINIUM.PDF要求開發者像圖形程式設計師一樣思考而非文件設計師:
// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, new XPoint(50, 50));
page.Graphics.DrawString("Customer:", labelFont, brush, new XPoint(50, 80));
page.Graphics.DrawString(customer.Name, valueFont, brush, new XPoint(120, 80));
page.Graphics.DrawLine(pen, 50, 100, 550, 100);
// ... hundreds of lines for a simple document// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, new XPoint(50, 50));
page.Graphics.DrawString("Customer:", labelFont, brush, new XPoint(50, 80));
page.Graphics.DrawString(customer.Name, valueFont, brush, new XPoint(120, 80));
page.Graphics.DrawLine(pen, 50, 100, 550, 100);
// ... hundreds of lines for a simple document' XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, New XPoint(50, 50))
page.Graphics.DrawString("Customer:", labelFont, brush, New XPoint(50, 80))
page.Graphics.DrawString(customer.Name, valueFont, brush, New XPoint(120, 80))
page.Graphics.DrawLine(pen, 50, 100, 550, 100)
' ... hundreds of lines for a simple document隨著需求變化,這種方法成為維護的挑戰,因為當內容移動時,必須重新計算每個元素的位置。
了解IronPDF
IronPDF採用一種以網頁為中心的PDF生成方法,使用Chromium渲染引擎將HTML、CSS和JavaScript直接轉換為PDF文件。 而不是基於座標的繪圖,IronPDF使開發者可以使用熟悉的網頁技術進行文件創建。
主要特徵包括:
- 基於HTML/CSS的設計:使用標準的網頁技術進行文件佈局
- Chromium渲染引擎:通過現代瀏覽器技術全麵支援CSS3和JavaScript
- 自動佈局:無需手動座標計算——內容自然流動
- 現代網頁標準:CSS Grid、Flexbox、網頁字體和ES2024 JavaScript
- URL到PDF轉換:將線上網頁直接渲染到PDF
- 龐大的社區:豐富的文檔、教程和支援資源
功能比較
下表突出了XFINIUM.PDF和IronPDF之間的基本差異:
| 功能 | XFINIUM.PDF | IronPDF |
|---|---|---|
| HTML到PDF | 有限的HTML支援,專注於程式化的PDF創建 | 完整的HTML到PDF轉換,提供全面的支援 |
| 社區與支持 | 較小的社區,線上資源較少 | 龐大的社區,擁有豐富的文檔和教程 |
| 授權 | 商業,基於開發者的授權 | 商業 |
| 跨平台支持 | 強大的跨平台能力 | 也支持跨平台操作 |
| 專用功能 | 全面的PDF編輯工具 | 先進的HTML渲染及PDF功能 |
詳細功能比較
| 功能 | XFINIUM.PDF | IronPDF |
|---|---|---|
| 內容創建 | ||
| HTML到PDF | 有限(PdfHtmlTextElement) | 完整的Chromium渲染 |
| URL到PDF | 沒有 | 有 |
| CSS支持 | 沒有 | 完整的CSS3 |
| JavaScript | 沒有 | 完整的ES2024 |
| Flexbox/Grid | 沒有 | 有 |
| Web 字體 | 沒有 | 有 |
| SVG 支援 | 有限 | 完整 |
| 佈局 | ||
| 自動佈局 | 沒有 | 有 |
| 自動分頁 | 沒有 | 有 |
| 手動定位 | 必需 | 可選(CSS定位) |
| 表格 | 手動繪製 | HTML <table> |
| PDF 操作 | ||
| 合併PDF | 有 | 有 |
| 分割PDF | 有 | 有 |
| 水印 | 手動繪製 | 內建 |
| 頁首/頁尾 | 手動每頁 | 自動 |
| 開發 | ||
| 學習曲線 | 高(座標系統) | 低(HTML/CSS) |
| 代碼冗長性 | 非常高 | Low |
| 維護 | 困難 | 簡單 |
| 跨平台 | 有 | 有 |
API架構差異
XFINIUM.PDF和IronPDF之間的架構差異代表了PDF生成的根本不同的方法。
XFINIUM.PDF流程文檔範式
XFINIUM.PDF使用多步驟流程與PdfFlowContent對象:
// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Actions;
using Xfinium.Pdf.FlowDocument;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfFlowDocument flowDocument = new PdfFlowDocument();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
PdfFlowContent content = new PdfFlowContent();
content.AppendHtml(html);
flowDocument.AddContent(content);
flowDocument.RenderDocument(document);
document.Save("output.pdf");
}
}// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Actions;
using Xfinium.Pdf.FlowDocument;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfFlowDocument flowDocument = new PdfFlowDocument();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
PdfFlowContent content = new PdfFlowContent();
content.AppendHtml(html);
flowDocument.AddContent(content);
flowDocument.RenderDocument(document);
document.Save("output.pdf");
}
}Imports Xfinium.Pdf
Imports Xfinium.Pdf.Actions
Imports Xfinium.Pdf.FlowDocument
Imports System.IO
Module Program
Sub Main()
Dim document As New PdfFixedDocument()
Dim flowDocument As New PdfFlowDocument()
Dim html As String = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>"
Dim content As New PdfFlowContent()
content.AppendHtml(html)
flowDocument.AddContent(content)
flowDocument.RenderDocument(document)
document.Save("output.pdf")
End Sub
End Module這種範式要求創建多個文檔對象,將內容添加到流容器,渲染到固定文檔,然後保存。 通過AppendHtml的HTML支援相比於完整瀏覽器渲染有限。
IronPDF精簡範式
IronPDF提供直接的HTML到PDF轉換使用ChromePdfRenderer類別:
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End ClassChromePdfRenderer類使用Chromium渲染引擎處理HTML,具備完整的CSS3和JavaScript支援。 欲獲取完整的HTML轉換指南,請參閱HTML轉PDF指南。
PDF合併操作
結合多個PDF文檔顯示了兩者API複雜性的顯著差異。
XFINIUM.PDF手動頁循環
XFINIUM.PDF需要打開文件流,遍歷頁面,並手動將每個頁面添加到輸出文檔:
// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument output = new PdfFixedDocument();
FileStream file1 = File.OpenRead("document1.pdf");
PdfFixedDocument pdf1 = new PdfFixedDocument(file1);
FileStream file2 = File.OpenRead("document2.pdf");
PdfFixedDocument pdf2 = new PdfFixedDocument(file2);
for (int i = 0; i < pdf1.Pages.Count; i++)
{
output.Pages.Add(pdf1.Pages[i]);
}
for (int i = 0; i < pdf2.Pages.Count; i++)
{
output.Pages.Add(pdf2.Pages[i]);
}
output.Save("merged.pdf");
file1.Close();
file2.Close();
}
}// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument output = new PdfFixedDocument();
FileStream file1 = File.OpenRead("document1.pdf");
PdfFixedDocument pdf1 = new PdfFixedDocument(file1);
FileStream file2 = File.OpenRead("document2.pdf");
PdfFixedDocument pdf2 = new PdfFixedDocument(file2);
for (int i = 0; i < pdf1.Pages.Count; i++)
{
output.Pages.Add(pdf1.Pages[i]);
}
for (int i = 0; i < pdf2.Pages.Count; i++)
{
output.Pages.Add(pdf2.Pages[i]);
}
output.Save("merged.pdf");
file1.Close();
file2.Close();
}
}Imports Xfinium.Pdf
Imports System.IO
Module Program
Sub Main()
Dim output As New PdfFixedDocument()
Dim file1 As FileStream = File.OpenRead("document1.pdf")
Dim pdf1 As New PdfFixedDocument(file1)
Dim file2 As FileStream = File.OpenRead("document2.pdf")
Dim pdf2 As New PdfFixedDocument(file2)
For i As Integer = 0 To pdf1.Pages.Count - 1
output.Pages.Add(pdf1.Pages(i))
Next
For i As Integer = 0 To pdf2.Pages.Count - 1
output.Pages.Add(pdf2.Pages(i))
Next
output.Save("merged.pdf")
file1.Close()
file2.Close()
End Sub
End Module這種方法需要顯式的流管理,手動遍歷每個文檔的頁面,以及仔細清理文件句柄。
IronPDF靜態合併方法
IronPDF提供一個靜態Merge方法,內部處理所有複雜性:
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}Imports IronPdf
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)
merged.SaveAs("merged.pdf")
End Sub
End ClassPdfDocument.Merge方法消除了手動頁面遍歷和流管理,顯著減少了代碼複雜性。
創建帶有文本和圖像的文檔
構建包含混合內容的文檔展示了基於座標的圖形和基於HTML的設計範例之間的差異。
XFINIUM.PDF圖形方法
XFINIUM.PDF需要創建字體對象、筆刷對象、將圖像加載到特定的圖像類中,並在精確座標繪製每個元素:
// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Core;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
PdfBrush brush = new PdfBrush(PdfRgbColor.Black);
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
FileStream imageStream = File.OpenRead("image.jpg");
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
imageStream.Close();
document.Save("output.pdf");
}
}// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Core;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
PdfBrush brush = new PdfBrush(PdfRgbColor.Black);
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
FileStream imageStream = File.OpenRead("image.jpg");
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
imageStream.Close();
document.Save("output.pdf");
}
}Imports Xfinium.Pdf
Imports Xfinium.Pdf.Graphics
Imports Xfinium.Pdf.Core
Imports System.IO
Class Program
Shared Sub Main()
Dim document As New PdfFixedDocument()
Dim page As PdfPage = document.Pages.Add()
Dim font As New PdfStandardFont(PdfStandardFontFace.Helvetica, 24)
Dim brush As New PdfBrush(PdfRgbColor.Black)
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50)
Dim imageStream As FileStream = File.OpenRead("image.jpg")
Dim image As New PdfJpegImage(imageStream)
page.Graphics.DrawImage(image, 50, 100, 200, 150)
imageStream.Close()
document.Save("output.pdf")
End Sub
End Class這需要管理PdfJpegImage對象,對文本和圖像進行顯式座標定位。
IronPDFHTML方法
IronPDF使用標準的HTML嵌入圖像:
// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string imageBase64 = Convert.ToBase64String(File.ReadAllBytes("image.jpg"));
string html = $@"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string imageBase64 = Convert.ToBase64String(File.ReadAllBytes("image.jpg"));
string html = $@"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}Imports IronPdf
Imports System.IO
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim imageBase64 As String = Convert.ToBase64String(File.ReadAllBytes("image.jpg"))
Dim html As String = $"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End ClassHTML處理佈局自動化——無需座標計算,無需字體對象管理,並且可以使用標準的<img>標籤或base64編碼自然地嵌入圖像。
API對映參考
評估從XFINIUM.PDF轉換到IronPDF的團隊會發現這種映射有助於理解概念等價性:
| XFINIUM.PDF | IronPDF |
|---|---|
PdfFixedDocument | ChromePdfRenderer |
PdfPage | 自動 |
page.Graphics.DrawString() | HTML文本元素 |
page.Graphics.DrawImage() | <img>標籤 |
page.Graphics.DrawLine() | CSS <hr> |
page.Graphics.DrawRectangle() | border的CSS行為 |
PdfUnicodeTrueTypeFont | CSS font-family |
PdfRgbColor | CSS color |
PdfBrush | CSS 屬性 |
PdfPen | CSS border |
PdfHtmlTextElement | RenderHtmlAsPdf() |
document.Save(stream) | pdf.BinaryData |
PdfStringAppearanceOptions | CSS 樣式 |
PdfStringLayoutOptions | CSS 布局 |
當團隊考慮從XFINIUM.PDF轉向IronPDF
幾種情況常常促使開發團隊將IronPDF作為XFINIUM.PDF的替代方案進行評估:
現代網頁內容需求
對於從基於網頁的模板、儀表板或報告生成PDF的應用程式,團隊會發現XFINIUM.PDF的基於座標範例有局限性。 IronPDF的Chromium引擎渲染現代的CSS Grid、Flexbox和JavaScript驅動的內容,這是XFINIUM.PDF無法支援的。
維護和代碼複雜性
XFINIUM.PDF的基於座標的API生成冗長的代碼,隨著文件佈局的發展,維護變得困難。 在XFINIUM.PDF中,一個簡單的發票文件可能需要數百行的定位代碼,而在IronPDF中僅需要幾十行HTML/CSS代碼。
URL到PDF功能
XFINIUM.PDF無法將線上網頁渲染為PDF——此功能需要外部HTML解析程式庫。 IronPDF提供原生的URL到PDF轉換,具備完整的JavaScript執行和動態內容支援。
開發者熟悉度
擁有強大網頁開發技能的團隊發現基於座標的PDF生成學習曲線陡峭。HTML和CSS是廣為人知的技術,使用IronPDF的方法對於不具專門PDF專業知識的開發者而言更加易於理解。
自動佈局和分頁
XFINIUM.PDF需要手動跟蹤Y位置,並在內容超出頁面邊界時顯式創建頁面。 IronPDF根據CSS規則和內容流自動處理分頁。
常見遷移考量
從XFINIUM.PDF轉向IronPDF的團隊應考慮這些技術差異:
從基於座標到流佈局
XFINIUM.PDF需要每個元素的準確X,Y座標。 IronPDF預設使用HTML流佈局。 對於需要絕對定位的情況,CSS提供了這種功能:
.positioned-element {
position: absolute;
top: 100px;
left: 50px;
}字體對象到CSS字體系列
XFINIUM.PDF顯式創建PdfUnicodeTrueTypeFont對象。 IronPDF使用CSS字體系列,具備自動字體管理:
<style>
body { font-family: Arial, sans-serif; }
h1 { font-family: 'Times New Roman', serif; font-size: 24px; }
</style><style>
body { font-family: Arial, sans-serif; }
h1 { font-family: 'Times New Roman', serif; font-size: 24px; }
</style>顏色對象到CSS顏色
XFINIUM.PDF需要PdfBrush對象。 IronPDF使用標準的CSS顏色:
.header { color: navy; background-color: #f5f5f5; }
.warning { color: rgb(255, 0, 0); }
.info { color: rgba(0, 0, 255, 0.8); }手動到自動分頁
XFINIUM.PDF需要跟蹤Y位置並手動創建新頁面。 IronPDF提供自動分頁,並具CSS控制:
.section { page-break-after: always; }
.keep-together { page-break-inside: avoid; }額外的IronPDF功能
除了核心比較點,IronPDF還提供文件操作功能,補充其HTML渲染:
.NET兼容性和未來準備
兩個程式庫均支援跨平台.NET開發。 IronPDF保持活躍開發,定期更新,確保與.NET 8、.NET 9和2026年預期推出包含.NET 10的未來版本兼容。該程式庫的HTML/CSS方法與現代網頁開發實踐相符,利用.NET開發者已有的技能。
結論
XFINIUM.PDF和IronPDF在.NET中的PDF生成代表了根本不同的方法。 XFINIUM.PDF的基於座標的圖形API提供低層級的控制,但需要顯著的代碼來進行文件佈局——每個元素都需要顯式定位、字體對象和顏色管理。 隨著文件複雜性的增加,這種方法越來越難以維護。
IronPDF的基於HTML/ CSS的方法利用Chromium渲染引擎將PDF生成作為網頁渲染來處理。 開發者使用熟悉的HTML、CSS和JavaScript,而不是學習基於座標的圖形API。自動佈局、頁面中斷和現代網頁標準的支援(CSS Grid、Flexbox、ES2024 JavaScript)顯著降低代碼複雜度。
對於構建基於網頁的報告、儀表板或動態文檔的應用程式的團隊而言,IronPDF的方法與現代開發實踐自然對接。 對於要求對每個文件元素進行像素級控制並願意投資於基於座標的編程模型的團隊而言,XFINIUM.PDF的圖形API提供了這種能力。
最終的選擇取決於您的團隊需求:如果您的PDF來自網頁內容,或者您偏愛HTML/CSS進行文件設計,IronPDF的方法提供了顯著的生產力優勢。 如果您正在構建具有精確定位需求的高專業PDF內容,並且您的團隊擁有圖形程式設計專家,XFINIUM.PDF的低層API可能滿足這些需求。
欲瞭解更多實施指導,請探索IronPDF文檔和教程,涵蓋具體使用案例和高級功能。
