
如何在 C# | IronPDF 中使用 IronPDF 轉換 PDF 頁面
XFINIUM.PDF 是由 O2 Solutions 開發的商用跨平台 PDF 程式庫,提供以 C# 程式化方式建立和編輯 PDF 的工具。 它提供兩個版本 —— 生成器(PDF 生成和編輯)和查看器(生成器功能加上渲染/顯示)。 此程式庫在 NuGet 上分發為 Xfinium.Pdf.NetStandard 和 Xfinium.Pdf.NetCore。 其基於座標的圖形程式設計模型可能會為構建文件密集型應用程式的團隊帶來摩擦,因為大多數版面設計工作都涉及使用像素座標手動定位元素。
本指南提供從 XFINIUM.PDF 遷移到IronPDF的過程,包含分步指南、程式碼比較,以及 .NET 開發人員評估此遷移的實用範例。
為什麼從 XFINIUM.PDF 遷移
XFINIUM.PDF 是一個低級 PDF 程式庫,依賴於基於座標的圖形程式設計,這意味著開發人員直接在頁面上定位大多數元素。 開發團隊考慮遷移的常見原因包括:
無原生 HTML 引擎: XFINIUM.PDF 沒有內建的 HTML 到 PDF 轉換器。 供應商提供的樣本 XHTML 瀏覽器僅支援有限的標籤集 (p, font, b, i, u, ul, li),且不解析 CSS 或執行 JavaScript,這可能不適合需要完整 HTML 到 PDF 功能的項目。
基於座標的 API: 大多數元素位置需要使用像素座標的手動定位,例如 DrawString("text", font, brush, 50, 100)。
手動字體管理: 字體物件必須使用 PdfStandardFont 和 PdfBrush 類顯式建立和管理。
無 CSS 樣式設計: 不支持現代 Web 樣式。 顏色、字型和佈局是通過程式方法調用處理的。
無 JavaScript 渲染: 僅靜態內容。 XFINIUM.PDF 不渲染動態 Web 內容或執行 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 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: Declarative HTML
Dim html As String = "<h1>Invoice</h1><p><b>Customer:</b> " & customer.Name & "</p>"
Dim 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 | 無原生引擎;僅樣本型 XHTML 轉換器(有限標籤,無 CSS/JS) | 使用 Chromium 進行完整的 HTML 到 PDF 轉換(CSS3 + JavaScript) |
| 社區與支援 | 小社區,較少的線上資源 | 大型社區,具有豐富的文件和教程 |
| 授權 | 商業性,基於開發人員的授權 | 商業 |
| 跨平台支持 | 強大的跨平台能力 | 也支持跨平台操作 |
| CSS 支援 | 否 | 完整支持 CSS3 |
| JavaScript | 否 | 完整的ES2024 |
| Flexbox/Grid | 否 | 是 |
| 自動佈局 | 否 | 是 |
| 自動分頁 | 否 | 是 |
| 手動定位 | 必需的 | 可選(CSS 佈置) |
| 學習曲線 | 高(座標系統) | 低(HTML/CSS) |
| 程式碼冗長性 | 非常高 | 低 |
快速入門:XFINIUM.PDF 到IronPDF的遷移
可以立即透過這些基本步驟開始遷移。
步驟 1:替換 NuGet 套件
移除 XFINIUM.PDF(實際 NuGet ID 是平台特定的):
# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf.NetStandard
# or: dotnet remove package Xfinium.Pdf.NetCore
安裝 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;Imports IronPdfImports IronPdf步驟 3:初始化授權
在應用啓動時新增授權初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";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.NetStandard (or .NetCore)
// XFINIUM.PDF has no native HTML-to-PDF engine. Flow content accepts
// styled text, headings, tables and images - not HTML. The vendor sample
// converter parses limited XHTML (p, font, b, i, u, ul, li) into
// PdfFormattedContent; full CSS/JS rendering is not supported.
using Xfinium.Pdf;
using Xfinium.Pdf.FlowDocument;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFlowDocument flowDocument = new PdfFlowDocument();
flowDocument.AddContent(new PdfFlowHeadingContent(
"Hello World",
new PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)));
flowDocument.AddContent(new PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
new PdfStandardFont(PdfStandardFontFace.Helvetica, 12)));
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
flowDocument.Save(fs);
}
}
}// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
// XFINIUM.PDF has no native HTML-to-PDF engine. Flow content accepts
// styled text, headings, tables and images - not HTML. The vendor sample
// converter parses limited XHTML (p, font, b, i, u, ul, li) into
// PdfFormattedContent; full CSS/JS rendering is not supported.
using Xfinium.Pdf;
using Xfinium.Pdf.FlowDocument;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFlowDocument flowDocument = new PdfFlowDocument();
flowDocument.AddContent(new PdfFlowHeadingContent(
"Hello World",
new PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)));
flowDocument.AddContent(new PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
new PdfStandardFont(PdfStandardFontFace.Helvetica, 12)));
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
flowDocument.Save(fs);
}
}
}Imports Xfinium.Pdf
Imports Xfinium.Pdf.FlowDocument
Imports Xfinium.Pdf.Graphics
Imports System.IO
Module Program
Sub Main()
Dim flowDocument As New PdfFlowDocument()
flowDocument.AddContent(New PdfFlowHeadingContent(
"Hello World",
New PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)))
flowDocument.AddContent(New PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
New PdfStandardFont(PdfStandardFontFace.Helvetica, 12)))
Using fs As New FileStream("output.pdf", FileMode.Create)
flowDocument.Save(fs)
End Using
End Sub
End ModuleImports Xfinium.Pdf
Imports Xfinium.Pdf.FlowDocument
Imports Xfinium.Pdf.Graphics
Imports System.IO
Module Program
Sub Main()
Dim flowDocument As New PdfFlowDocument()
flowDocument.AddContent(New PdfFlowHeadingContent(
"Hello World",
New PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)))
flowDocument.AddContent(New PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
New PdfStandardFont(PdfStandardFontFace.Helvetica, 12)))
Using fs As New FileStream("output.pdf", FileMode.Create)
flowDocument.Save(fs)
End Using
End Sub
End ModuleIronPDF 方法:
// 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");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Module Program
Sub Main()
Dim renderer As 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 Module' NuGet: Install-Package IronPdf
Imports IronPdf
Module Program
Sub Main()
Dim renderer As 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 ModuleXFINIUM.PDF 需要建立 PdfFlowDocument,為每個樣式實例化 PdfStandardFont 物件,將每個內容部分構建為 PdfFlowHeadingContent 或 PdfFlowTextContent,將它們新增到流文件中,並通過 FileStream 保存。 流文件 API 接收型別化的內容物件,而非 HTML 標記。IronPDF 簡化為三行:建立渲染器,渲染 HTML 並保存。
如需進階 HTML 到 PDF 的場景,請參見 HTML 到 PDF 轉換指南。
合併多個 PDF
PDF 合併清楚地展示了 API 複雜性差異。
XFINIUM.PDF 方法:
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
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.NetStandard (or .NetCore)
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.NetStandard (or .NetCore)
Imports Xfinium.Pdf
Imports System.IO
Class Program
Shared 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 Class' NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
Imports Xfinium.Pdf
Imports System.IO
Class Program
Shared 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 ClassIronPDF 方法:
// 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");
}
}' NuGet: Install-Package IronPdf
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' NuGet: Install-Package IronPdf
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 ClassXFINIUM.PDF 需要建立一個輸出文件,打開文件流,載入每個文件,手動遍歷頁面並一一新增,保存,然後關閉流。IronPDF提供單一的 PdfDocument.Merge() 方法,內部處理所有複雜性。
探索 PDF 合併文件 了解更多合併選項。
建立包含文字和圖像的 PDF
多內容文件顯示出基本的範例差異。
XFINIUM.PDF 方法:
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
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(new PdfRgbColor(0, 0, 0));
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
using (FileStream imageStream = File.OpenRead("image.jpg"))
{
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
}
document.Save("output.pdf");
}
}// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
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(new PdfRgbColor(0, 0, 0));
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
using (FileStream imageStream = File.OpenRead("image.jpg"))
{
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
}
document.Save("output.pdf");
}
}Imports Xfinium.Pdf
Imports Xfinium.Pdf.Graphics
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(New PdfRgbColor(0, 0, 0))
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50)
Using imageStream As FileStream = File.OpenRead("image.jpg")
Dim image As New PdfJpegImage(imageStream)
page.Graphics.DrawImage(image, 50, 100, 200, 150)
End Using
document.Save("output.pdf")
End Sub
End ClassImports Xfinium.Pdf
Imports Xfinium.Pdf.Graphics
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(New PdfRgbColor(0, 0, 0))
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50)
Using imageStream As FileStream = File.OpenRead("image.jpg")
Dim image As New PdfJpegImage(imageStream)
page.Graphics.DrawImage(image, 50, 100, 200, 150)
End Using
document.Save("output.pdf")
End Sub
End ClassIronPDF 方法:
// 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");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System.IO
Module Program
Sub Main()
Dim renderer As 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 Module' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System.IO
Module Program
Sub Main()
Dim renderer As 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 ModuleXFINIUM.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() | 在 <div> 上的 CSS border |
PdfStandardFont | CSS font-family |
PdfRgbColor | CSS color |
PdfBrush | CSS 屬性 |
PdfJpegImage | 帶有 base64 的 <img> 標籤 |
document.Save(stream) | pdf.SaveAs() 或 pdf.BinaryData |
PdfFlowDocument | RenderHtmlAsPdf() |
| 供應商樣品 XHTML 轉換器 | RenderHtmlAsPdf() |
常見遷移問題及解決方案
問題 1:基於座標的佈局
XFINIUM.PDF:一切都需要精確的 X,Y 座標以及手動定位。
解決方案:使用 HTML/CSS 流佈置。 當需要絕對定位時,請使用 CSS:
.positioned-element {
position: absolute;
top: 100px;
left: 50px;
}
問題 2:字體物件管理
XFINIUM.PDF:為每種字體建立 PdfStandardFont 或 PdfUnicodeTrueTypeFont 物件。
解決方案:使用 CSS 字體家族——字體自動處理:
<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" />
<!-- 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" .
文件基於座標的佈局,並記錄所有 X,Y 定位值。 確認字體和顏色物件 (PdfStandardFont, PdfRgbColor, PdfBrush)。 使用 PdfFixedDocument.Pages.Add() 映射合併 PDF 工作流。
程式碼更新任務
- 刪除 Xfinium.Pdf NuGet 套件
- 安裝IronPDFNuGet 套件
- 將命名空間導入從
Xfinium.Pdf更新為IronPdf - 將
DrawString()調用轉換為 HTML 文字元素 - 將
DrawImage()調用轉換為 HTML<img>標籤 - 將
DrawRectangle()和DrawLine()轉換為 CSS 邊框 - 用 CSS
font-family替換PdfStandardFont - 用 CSS 顏色替換
PdfRgbColor和PdfBrush - 用
PdfDocument.Merge()替換頁面迴圈合併 - 在啟動時新增IronPDF授權初始化
遷移後測試
遷移後,驗證以下方面:
- 比較視覺輸出以確保外觀符合預期
- 驗證使用新的 HTML/CSS 方法的文字渲染
- 使用 CSS 檢查圖像定位
- 測試分頁符按照預期發生
- 驗證 PDF 安全設置是否正確應用
- 在所有目標平台上進行測試
遷移到IronPDF的關鍵好處
從 XFINIUM.PDF 遷移到IronPDF提供了多項關鍵優勢:
基於 HTML 的內容創作: 網頁開發人員可以利用現有的 HTML 和 CSS 技能。 不需要學習基於座標的繪圖 API 或管理字體和畫刷物件。
自動佈局: 文字換行、分頁和流佈局自動進行。 無需手動計算元素位置或分頁符。
現代CSS支持: 完整的CSS3包括Flexbox和網格佈局。 響應式設計直接轉化為PDF。
簡化的 PDF 操作: 單一方法調用,替代如 PdfDocument.Merge() 這樣的常用操作中的複雜頁面迭代迴圈。
積極的開發:IronPDF的定期更新與當前 .NET 版本和 C# 語言釋放保持同步。
廣泛的文件: 與 XFINIUM.PDF 較小的生態系統相比,大型社區提供全面的文件、教程和支援資源。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


