如何在 C# 中從 XFINIUM.PDF 轉移到 IronPDF
XFINIUM.PDF 是一個跨平台的 PDF 函式庫,提供全面的工具以 C# 程式化的方式建立與編輯 PDF。 雖然它提供了兩個版本-Generator 和 Viewer,但資料庫對於基於座標的圖形程式設計的依賴,為建立文件繁重應用程式的開發團隊帶來了重大的挑戰。 每個元素都必須使用像素坐標來手動定位,讓原本簡單的文件變成複雜的繪圖練習。
本指南提供了從 XFINIUM.PDF 到IronPDF的完整轉換路徑,為評估此轉換的專業 .NET 開發人員提供了分步說明、程式碼比較以及實用範例。
為何要從 XFINIUM.PDF 遷移?
XFINIUM.PDF 是一個低階 PDF 函式庫,它依賴於基於座標的圖形編程,迫使開發人員手動定位頁面上的每個元素。 隨著需求的變化,這種方法也成為維護的惡夢。 開發團隊考慮遷移的主要原因包括
不支援HTML: XFINIUM.PDF無法直接將HTML/CSS轉換為PDF。 它著重於使用低階繪圖原素進行程式化 PDF 創作,對於需要大量 HTML-to-PDF 功能的專案而言,這可能並不足夠。
基於座標的 API:頁面上的每個元素都需要使用像素座標(例如 DrawString("text", font, brush, 50, 100))進行手動定位。
手動字型管理:字型物件必須使用類似 PdfStandardFont 和 PdfBrush 的類別明確建立和管理。
不支援 CSS 樣式:不支援現代網頁樣式。 顏色、字型和版面必須透過程式化的方法呼叫來手動處理。
不使用 JavaScript 渲染:僅顯示靜態內容。 XFINIUM.PDF 無法渲染動態網頁內容或執行 JavaScript。
複雜文本佈局:對於除簡單單行文本以外的任何文本,都需要手動測量文本尺寸和換行計算。
社群資源有限:與主流解決方案相比,社群提供的資源(例如範例和教學)較少,這可能會使新用戶更難入門。
核心問題:圖形 API vs 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 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 document
IronPDF 使用熟悉的 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: Declarative HTML
Dim html As String = "<h1>Invoice</h1><p><b>Customer:</b> " & customer.Name & "</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
IronPDFvs XFINIUM.PDF:功能比較
了解架構上的差異有助於技術決策者評估遷移投資:
| 特點 | XFINIUM.PDF | IronPDF |
|---|---|---|
| HTML至PDF | 有限的 HTML 支援,著重於程式化 PDF 建立 | 全面支援 HTML 至 PDF 的完整轉換 |
| 社群與支援 | 社群規模較小,可用的線上資源較少 | 擁有大量文件和教學的大型社群 |
| 執照 | 基於開發人員的商業授權 | 商業的 |
| 跨平台支援 | 強大的跨平台能力 | 同時支援跨平台作業 |
| CSS 支援 | 無 | 完整的 CSS3 |
| JavaScript。 | 無 | 完整的 ES2024 |
| Flexbox/網格 | 無 | 是 |
| 自動排版 | 無 | 是 |
| 自動分頁 | 無 | 是 |
| 手冊定位 | 要求 | 可選 (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:更新命名空間
用IronPDF命名空間取代 XFINIUM.PDF 命名空間:
// 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;
Imports IronPdf
步驟 3:初始化授權
在應用程式啟動時加入授權初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
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");
}
}
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
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");
}
}
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 Class
XFINIUM.PDF 需要建立 PdfFixedDocument、PdfFlowDocument、PdfFlowContent 對象,呼叫 AppendHtml(),將內容新增至串流文檔,渲染到固定文檔,最後儲存。IronPDF將此簡化為三行:建立渲染器、渲染 HTML、儲存。
如需進階的 HTML 至IronPDF情境,請參閱 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();
}
}
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 方法:
// 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 Class
XFINIUM.PDF 需要建立輸出文件、開啟檔案串流、載入每個文件、手動反覆瀏覽頁面並逐一新增、儲存,然後關閉串流。IronPDF提供了一個單一的 PdfDocument.Merge() 方法,該方法在內部處理所有複雜性。
探索 IronPDF合併文件,以獲得其他合併選項。
使用文字和圖片建立 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
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");
}
}
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 Class
XFINIUM.PDF 需要建立文件、新增頁面、建立字體和畫筆物件、在特定座標繪製文字、開啟圖像流、建立 PdfJpegImage、在具有尺寸的座標處繪製圖像、關閉流並儲存。IronPDF使用內嵌 base64 圖片的標準 HTML - 與網頁開發人員日常使用的方法相同。
XFINIUM.PDF API 到IronPDF的映射參考
此對應可透過顯示直接的 API 對應關係來加速遷移:
| XFINIUM.PDF | IronPDF |
|---|---|
PdfFixedDocument |
ChromePdfRenderer |
PdfPage |
自動化 |
page.Graphics.DrawString() |
HTML 文字元素 |
page.Graphics.DrawImage() |
<img> 標籤 |
page.Graphics.DrawLine() |
CSS border 或 <hr> |
page.Graphics.DrawRectangle() |
CSS border on <div> |
PdfStandardFont |
CSS font-family |
PdfRgbColor |
CSS color |
PdfBrush |
CSS 特性 |
PdfJpegImage |
<img> 有 base64 的標籤 |
document.Save(stream) |
pdf.SaveAs() 或 pdf.BinaryData |
PdfFlowDocument |
RenderHtmlAsPdf() |
PdfFlowContent.AppendHtml() |
RenderHtmlAsPdf() |
常見的遷移問題與解決方案
問題 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>
第 3 期:顏色處理
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" />
<img src="data:image/jpeg;base64,..." />
<img src="image.jpg" width="200" height="150" />
<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 工作流程。
程式碼更新任務
1.移除 Xfinium.Pdf NuGet 套件 2.安裝IronPDFNuGet 套件
- 將命名空間匯入從
Xfinium.Pdf更新為IronPdf - 將
DrawString()呼叫轉換為 HTML 文字元素 - 將
DrawImage()呼叫轉換為 HTML<img>標籤 - 將
DrawRectangle()和DrawLine()轉換為 CSS 邊框 - 將
PdfStandardFont替換為 CSSfont-family - 將
PdfRgbColor和PdfBrush替換為 CSS 顏色 - 將頁面循環合併替換為
PdfDocument.Merge()10.在啟動時增加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 較小的生態系統相比,擁有龐大的社群、全面的文件、教學和支援資源。

