如何在 C# 中將 XFINIUM.PDF 遷移到 IronPDF
XFINIUM.PDF 是一個跨平台的 PDF 庫,它提供了全面的工具,用於在 C# 中以程式設計方式建立和編輯 PDF。 雖然它提供了生成器和檢視器兩個版本,但該程式庫對基於座標的圖形程式設計的依賴給建立文件密集型應用程式的開發團隊帶來了巨大的挑戰。 每個元素都必須使用像素座標手動定位,這使得原本簡單的文件變成了複雜的繪圖練習。
本指南提供了從 XFINIUM.PDF 到 IronPDF 的完整遷移路徑,包括逐步說明、程式碼比較和實用範例,供正在評估此過渡的專業 .NET 開發人員參考。
為什麼要從 XFINIUM 遷移.PDF
XFINIUM.PDF 是一個底層 PDF 庫,它依賴基於座標的圖形編程,迫使開發人員手動定位頁面上的每個元素。 隨著需求的變化,這種方法會變成維護上的惡夢。 開發團隊考慮遷移的主要原因包括:
不支援HTML: XFINIUM.PDF無法直接將HTML/CSS轉換為PDF。 它專注於使用底層繪圖圖元進行程式化 PDF 創建,但這對於需要強大的 HTML 轉 PDF 功能的專案來說可能不夠用。
基於座標的 API:頁面上的每個元素都需要使用像素座標進行手動定位,例如DrawString("text", font, brush, 50, 100) 。
手動字體管理:必須使用PdfStandardFont和PdfBrush等類別明確建立和管理字體物件。
不支援 CSS 樣式:不支援現代網頁樣式。 顏色、字體和佈局必須透過程式方法呼叫手動處理。
不使用 JavaScript 渲染:僅顯示靜態內容。 XFINIUM.PDF 無法渲染動態網頁內容或執行 JavaScript。
複雜文本佈局:對於除簡單單行文本以外的任何文本,都需要手動測量文本尺寸和換行計算。
社群資源有限:與主流解決方案相比,社群提供的資源(例如範例和教學)較少,這可能會使新用戶更難入門。
核心問題:圖形 API 與 HTML
XFINIUM.PDF 迫使你像圖形程式設計師一樣思考,而不是像文件設計師一樣思考:
// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, 50, 50);
page.Graphics.DrawString("Customer:", labelFont, brush, 50, 80);
page.Graphics.DrawString(customer.Name, valueFont, brush, 120, 80);
// ... hundreds of lines for a simple document// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, 50, 50);
page.Graphics.DrawString("Customer:", labelFont, brush, 50, 80);
page.Graphics.DrawString(customer.Name, valueFont, brush, 120, 80);
// ... hundreds of lines for a simple documentIronPDF 使用常見的 HTML/CSS:
// IronPDF: Declarative HTML
var html = @"<h1>Invoice</h1><p><b>Customer:</b> " + customer.Name + "</p>";
var pdf = renderer.RenderHtmlAsPdf(html);// IronPDF: Declarative HTML
var html = @"<h1>Invoice</h1><p><b>Customer:</b> " + customer.Name + "</p>";
var pdf = renderer.RenderHtmlAsPdf(html);IronPDF 與 XFINIUM.PDF:功能對比
了解架構差異有助於技術決策者評估遷移投資:
| 特徵 | XFINIUM.PDF | IronPDF |
|---|---|---|
| HTML 轉 PDF | HTML 支援有限,主要著重於程式化 PDF 創建。 | 提供全面的 HTML 轉 PDF 轉換功能及支持 |
| 社區與支持 | 社群規模較小,可用的線上資源也較少。 | 擁有龐大用戶群、豐富文件和教程 |
| 執照 | 商業用途,採用開發者授權模式 | 商業的 |
| 跨平台支援 | 強大的跨平台能力 | 同時支援跨平台操作 |
| CSS 支援 | 不 | 完整的 CSS3 |
| JavaScript | 不 | 完整版 ES2024 |
| Flexbox/Grid | 不 | 是的 |
| 自動佈局 | 不 | 是的 |
| 自動分頁符 | 不 | 是的 |
| 手動定位 | 必需的 | 可選(CSS 定位) |
| 學習曲線 | 高(座標系) | 低(HTML/CSS) |
| 程式碼冗長性 | 非常高 | 低的 |
快速入門:XFINIUM.PDF 到 IronPDF 的遷移
透過這些基礎步驟,遷移工作可以立即開始。
步驟 1:替換 NuGet 套件
刪除 XFINIUM.PDF:
# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf安裝 IronPDF:
# Install IronPDF
dotnet add package IronPdf# Install IronPDF
dotnet add package IronPdf步驟 2:更新命名空間
將 XFINIUM.PDF 命名空間替換為 IronPdf 命名空間:
// Before (XFINIUM.PDF)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Content;
using Xfinium.Pdf.FlowDocument;
// After (IronPDF)
using IronPdf;// Before (XFINIUM.PDF)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Content;
using Xfinium.Pdf.FlowDocument;
// After (IronPDF)
using IronPdf;步驟 3:初始化許可證
在應用程式啟動時新增許可證初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";程式碼遷移範例
將 HTML 轉換為 PDF
最基本的操作揭示了這些 .NET PDF 程式庫之間的複雜性差異。
XFINIUM.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");
}
}// 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");
}
}IronPDF 方法:
// 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");
}
}XFINIUM.PDF 需要建立PdfFixedDocument 、 PdfFlowDocument和PdfFlowContent對象,呼叫AppendHtml() ,將內容新增到流文檔,渲染到固定文檔,最後儲存。 IronPDF 將此流程簡化為三行程式碼:建立渲染器、渲染 HTML 並儲存。
如需更進階的 HTML 轉 PDF 場景,請參閱HTML 轉 PDF 轉換指南。
合併多個PDF文件
PDF合併清楚展現了API複雜性的差異。
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();
}
}IronPDF 方法:
// 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");
}
}XFINIUM.PDF 需要建立輸出文檔,開啟文件流,載入每個文檔,手動遍歷頁面並逐一添加,儲存,然後關閉流。 IronPDF 提供了一個PdfDocument.Merge()方法,在內部處理所有複雜性。
請查閱PDF 合併文件以了解更多合併選項。
建立包含文字和圖像的 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");
}
}IronPDF 方法:
// 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");
}
}XFINIUM.PDF 需要建立文件、新增頁面、建立字體和畫筆物件、在特定座標繪製文字、開啟影像流、建立PdfJpegImage 、在指定座標繪製影像並確定尺寸、關閉流並儲存。 IronPDF 使用嵌入 base64 圖像的標準 HTML——這是 Web 開發人員每天使用的方法。
XFINIUM.PDF API 到 IronPDF 映射參考
此映射透過顯示直接的 API 等效項來加速遷移:
| XFINIUM.PDF | IronPDF | 筆記 |
|---|---|---|
PdfFixedDocument | ChromePdfRenderer | 建立渲染器,而不是文檔 |
PdfPage | 自動的 | 由 HTML 內容建立的頁面 |
page.Graphics.DrawString() | HTML 文字元素 | <p>,<h1> ,<span> , ETC。 |
page.Graphics.DrawImage() | <img>標籤 | HTML影像 |
page.Graphics.DrawLine() | CSS border或<hr> | HTML/CSS 程式碼行 |
page.Graphics.DrawRectangle() | CSS border``<div> | HTML 框 |
PdfStandardFont | CSS font-family | 無需字體對象 |
PdfRgbColor | CSS color | 標準 CSS 顏色 |
PdfBrush | CSS屬性 | 背景、顏色等。 |
PdfJpegImage | <img>帶有 base64 的標籤 | 或者檔案路徑 |
document.Save(stream) | pdf.SaveAs()或pdf.BinaryData | 多種輸出選項 |
PdfFlowDocument | RenderHtmlAsPdf() | 完全支援 HTML |
PdfFlowContent.AppendHtml() | RenderHtmlAsPdf() | 直接 HTML 渲染 |
常見遷移問題及解決方案
問題 1:基於座標的佈局
XFINIUM.PDF:所有操作都需要精確的 X、Y 座標,並且需要手動定位。
解決方案:使用HTML/CSS串流佈局。 如果需要絕對定位,請使用 CSS:
.positioned-element {
position: absolute;
top: 100px;
left: 50px;
}問題 2:字體物件管理
XFINIUM.PDF:為每種字體建立PdfStandardFont或PdfUnicodeTrueTypeFont物件。
解決方案:使用 CSS font-family-字體會自動處理:
<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>第三期:色彩處理
XFINIUM.PDF:建立用於顏色的PdfRgbColor和PdfBrush物件。
解決方案:使用標準的CSS顏色:
.header { color: navy; background-color: #f5f5f5; }
.warning { color: rgb(255, 0, 0); }
.info { color: rgba(0, 0, 255, 0.8); }問題 4:手動分頁符
XFINIUM.PDF:追蹤 Y 軸位置,並在內容溢出時手動建立新頁面。
解決方案: IronPDF 可以自動處理分頁符號。 如需進行明確控制,請使用 CSS:
.section { page-break-after: always; }
.keep-together { page-break-inside: avoid; }問題 5:圖片加載
XFINIUM.PDF:開啟檔案流,建立PdfJpegImage對象,在座標處繪製,關閉流。
解決方案:使用 HTML<img>包含檔案路徑或 base64 資料的標籤:
<img src="image.jpg" width="200" height="150" />
<!-- or -->
<img src="data:image/jpeg;base64,..." /><img src="image.jpg" width="200" height="150" />
<!-- or -->
<img src="data:image/jpeg;base64,..." />XFINIUM.PDF 遷移清單
遷移前任務
審核您的程式碼庫,找出所有 XFINIUM.PDF 的使用情況:
grep -r "using Xfinium.Pdf" --include="*.cs" .
grep -r "Graphics.DrawString\|Graphics.DrawImage\|Graphics.DrawLine" --include="*.cs" .grep -r "using Xfinium.Pdf" --include="*.cs" .
grep -r "Graphics.DrawString\|Graphics.DrawImage\|Graphics.DrawLine" --include="*.cs" .記錄基於座標的佈局,並記下所有 X、Y 定位值。 辨識字體和顏色物件( PdfStandardFont 、 PdfRgbColor 、 PdfBrush )。 使用PdfFixedDocument.Pages.Add()來對應合併的 PDF 工作流程。
程式碼更新任務
- 刪除 Xfinium.Pdf NuGet 套件
- 安裝 IronPdf NuGet 套件
- 將命名空間導入從
Xfinium.Pdf更新為IronPdf - 將
DrawString()呼叫轉換為 HTML 文字元素 - 將
DrawImage()呼叫轉換為 HTML<img>標籤 - 將
DrawRectangle()和DrawLine()轉換為 CSS 邊框 - 將
PdfStandardFont替換為 CSSfont-family - 將
PdfRgbColor和PdfBrush替換為 CSS 顏色 - 將頁面循環合併替換為
PdfDocument.Merge() - 在啟動時加入 IronPDF 許可證初始化
遷移後測試
遷移完成後,請確認以下幾個面向:
- 對比視覺輸出,確保外觀符合預期
- 使用新的 HTML/CSS 方法驗證文字渲染效果
- 使用 CSS 檢查圖片定位
- 測試頁面分頁符號如預期出現
- 確認 PDF 安全設定已正確套用
- 在所有目標平台上進行測試
遷移到 IronPDF 的主要優勢
從 XFINIUM.PDF 遷移到 IronPDF 可帶來以下幾個關鍵優勢:
基於 HTML 的內容創建: Web 開發人員可以利用現有的 HTML 和 CSS 技能。 無需學習基於座標的繪圖 API 或管理字體和畫筆物件。
自動版面:文字換行、分頁和串流版面配置自動完成。 無需手動計算元素位置或分頁符號。
現代 CSS 支援:完全支援 CSS3,包括 Flexbox 和 Grid 佈局。 響應式設計可直接轉換為 PDF。
簡化 PDF 操作:使用單一方法呼叫來執行諸如PdfDocument.Merge()之類的常用操作,取代複雜的頁面迭代循環。
積極開發:隨著 .NET 10 和 C# 14 的普及,IronPDF 將持續更新,確保與目前和未來的 .NET 版本相容。
豐富的文件:與 XFINIUM.PDF 較小的生態系統相比,擁有龐大的社群、全面的文件、教學和支援資源。






