如何在 C# 中從 Spire.PDF 遷移到 IronPDF
從 Spire.PDF 遷移到 IronPDF:完整的 C# 遷移指南。
從 Spire.PDF 轉換到IronPDF可將您的 PDF 生成工作流程從將文字渲染為圖像的函式庫轉換為使用現代 Chromium 渲染引擎生成真實、可選、可搜尋文字的函式庫。本指南提供完整的逐步遷移路徑,可解決 Spire.PDF 的關鍵 HTML 渲染限制和字型嵌入問題。
為什麼要從 Spire.PDF 移轉到 IronPDF?
瞭解 Spire.PDF
Spire.PDF 是專為 .NET 開發人員設計的強大商用 PDF 函式庫,可有效率地處理 PDF 文件。 Spire.PDF 因其特殊功能而在程式設計社群中嶄露頭角,尤其是在傳統應用程式中,其整合功能可與 E-iceblue 工具集中的其他元件完美結合。
然而,Spire.PDF 有幾個影響實際使用的基本問題,特別是圍繞 HTML 到 PDF 的轉換和現代網路標準支援。
關鍵技術問題
| 問題 | 影響力 | IronPdf 解決方案 |
|---|---|---|
| 以影像呈現的文字 | PDF 無法搜尋、無法存取、無法複製文字 | 真實文字渲染 |
| Internet Explorer 依賴性 | 過時的渲染、安全風險 | 現代 Chromium 引擎 |
| 字體嵌入失敗 | 文件在其他系統上看起來不對 | 可靠的字型處理 |
| 大量部署足跡 | 記憶體使用率高、啟動速度慢 | 高效部署 |
| 有限的 CSS 支援 | 現代佈局無法正確呈現 | 完全支援 CSS3 |
核心問題:以圖片為基礎的 PDF。
Spire.PDF 的一個顯著缺點是傾向於將 HTML 文件中的文字呈現為影像。 這會導致 PDF 中的文字無法選擇或搜尋,對於需要搜尋功能或文件文字互動的應用程式來說,這可能是一個嚴重的限制。
當您使用 Spire.PDF 的 LoadFromHTML() 方法時,通常會將文字渲染為位圖影像,而非實際的文字,因而產生這些問題:
- 文字不能選擇
- 無法搜尋文字
- 文字不得複製
- 螢幕閱讀器無法閱讀 (違反可存取性)
- 檔案大小較大
- 縮放導致像素化
Spire.PDF 與IronPDF的比較
| 特點 | Spire.PDF | IronPDF |
|---|---|---|
| HTML 至 PDF 渲染 | 以影像呈現的文字 | 真實的文字呈現(可選擇與搜尋) |
| 渲染引擎 | 依賴於某些系統的 Internet Explorer | 基於 Chromium,符合現代網路標準 |
| 字體處理 | 字型嵌入的已知問題 | 可靠、穩健的字型處理 |
| CSS3 支援。 | 限額 | 全文 |
| Flexbox/網格 | 不支援 | 全面支援 |
| JavaScript。 | 限額 | 完整的 ES6+ |
| PDF 可訪問性 | 差異(以圖像為基礎) | 出色的 |
| API 設計 | 複雜的 | 簡單直觀 |
| 部署足跡 | 大型 | 緩和 |
| 授權 | 免費/商業 | 商業的 |
對於計劃在 2025 年和 2026 年之前採用 .NET 10 和 C# 14 的團隊而言,IronPDF 可解決 Spire.PDF 的 HTML-to-PDF 轉換的關鍵問題,將文字呈現為實際可選擇的文字而非影像,確保 PDF 可搜尋且可存取。
開始之前
先決條件
1..NET 環境:.NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ 2.NuGet存取:安裝 NuGet 套件的能力 3.IronPDF 授權:從IronPdf.com取得您的授權金鑰。
NuGet 套件變更
# Remove Spire.PDF
dotnet remove package Spire.PDF
dotnet remove package FreeSpire.PDF # If using free version
# Install IronPDF
dotnet add package IronPdf# Remove Spire.PDF
dotnet remove package Spire.PDF
dotnet remove package FreeSpire.PDF # If using free version
# Install IronPDF
dotnet add package IronPdf授權組態
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IRON VB CONVERTER ERROR developers@ironsoftware.com完整的 API 參考資料
命名空間變更
// Before: Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter;
// After: IronPDF
using IronPdf;
using IronPdf.Editing;// Before: Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter;
// After: IronPDF
using IronPdf;
using IronPdf.Editing;IRON VB CONVERTER ERROR developers@ironsoftware.com核心 API 對應
| Spire.PDF | IronPDF | 筆記 |
|---|---|---|
new PdfDocument() | 新的 ChromePdfRenderer() | 針對 HTML 呈現 |
| <代碼>pdf.LoadFromHTML()</代碼 | <編碼>renderer.RenderHtmlAsPdf()</編碼 | HTML 轉換 |
| <代碼>pdf.LoadFromFile()</代碼 | <代碼>PdfDocument.FromFile()</代碼 | 載入現有 PDF |
| <代碼>pdf.SaveToFile()</代碼 | <代碼>pdf.SaveAs()</代碼 | 儲存至檔案 |
| <代碼>pdf.Close()</代碼 | 不需要 | 使用處置模式 |
| <代碼>pdf.Pages.Add()</代碼 | <編碼>renderer.RenderHtmlAsPdf()</編碼 | 從 HTML 建立頁面 |
| <代碼>pdf.InsertPageRange()</代碼 | <代碼>PdfDocument.Merge()</代碼 | 合併 PDF |
page.Canvas.DrawString()。 | TextStamper + ApplyStamp() | 新增文字 |
| <編碼>PDFFont</編碼 | HTML 中的 CSS 造型 | 字型配置 |
| <編碼>PdfBrush</編碼 | HTML 中的 CSS 造型 | 顏色/填充配置 |
程式碼遷移範例
範例 1:HTML 到 PDF 的轉換
之前 (Spire.PDF):
// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
class Program
{
static void Main()
{
PdfDocument pdf = new PdfDocument();
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
string htmlString = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
pdf.LoadFromHTML(htmlString, false, true, true);
pdf.SaveToFile("output.pdf");
pdf.Close();
}
}// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
class Program
{
static void Main()
{
PdfDocument pdf = new PdfDocument();
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
string htmlString = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
pdf.LoadFromHTML(htmlString, false, true, true);
pdf.SaveToFile("output.pdf");
pdf.Close();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlString = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlString);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string htmlString = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlString);
pdf.SaveAs("output.pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com本範例展示 HTML 呈現的基本差異。 Spire.PDF 使用 LoadFromHTML() 與 PdfHtmlLayoutFormat 物件,通常會將文字渲染為位圖影像。 結果是使用者無法選擇、複製或搜尋文字的 PDF。
IronPDF 使用 ChromePdfRenderer 搭配 RenderHtmlAsPdf() 產生可完全選擇、搜尋及存取的真實文字。 不需要呼叫 Close() -IronPDF使用 dispose 模式進行自動清理。請參閱 HTML to PDF 文件,以瞭解全面的範例。
範例 2:合併多個 PDF 檔案
之前 (Spire.PDF):
// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using System;
class Program
{
static void Main()
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile("document1.pdf");
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile("document2.pdf");
pdf1.InsertPageRange(pdf2, 0, pdf2.Pages.Count - 1);
pdf1.SaveToFile("merged.pdf");
pdf1.Close();
pdf2.Close();
}
}// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using System;
class Program
{
static void Main()
{
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile("document1.pdf");
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile("document2.pdf");
pdf1.InsertPageRange(pdf2, 0, pdf2.Pages.Count - 1);
pdf1.SaveToFile("merged.pdf");
pdf1.Close();
pdf2.Close();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
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;
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");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comSpire.PDF 需要用new PdfDocument()+ LoadFromFile() 來手動載入每個文件,然後用 InsertPageRange() 來指定要插入的頁面,最後在每個文件上呼叫 Close() 。
IronPDF 使用較簡單的<代碼>PdfDocument.FromFile()</代碼模式和靜態<代碼>PdfDocument.Merge()</代碼方法,可接受多個文件。 不需要呼叫 Close()。 請參閱我們的 教學,瞭解更多資訊。
範例 3:將文字新增至 PDF
之前 (Spire.PDF):
// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;
class Program
{
static void Main()
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20);
PdfBrush brush = new PdfSolidBrush(Color.Black);
page.Canvas.DrawString("Hello from Spire.PDF!", font, brush, new PointF(50, 50));
pdf.SaveToFile("output.pdf");
pdf.Close();
}
}// NuGet: Install-Package Spire.PDF
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;
class Program
{
static void Main()
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20);
PdfBrush brush = new PdfSolidBrush(Color.Black);
page.Canvas.DrawString("Hello from Spire.PDF!", font, brush, new PointF(50, 50));
pdf.SaveToFile("output.pdf");
pdf.Close();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body></body></html>");
var textStamper = new TextStamper()
{
Text = "Hello from IronPDF!",
FontSize = 20,
VerticalOffset = 50,
HorizontalOffset = 50
};
pdf.ApplyStamp(textStamper);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body></body></html>");
var textStamper = new TextStamper()
{
Text = "Hello from IronPDF!",
FontSize = 20,
VerticalOffset = 50,
HorizontalOffset = 50
};
pdf.ApplyStamp(textStamper);
pdf.SaveAs("output.pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comSpire.PDF 使用 PdfFont, PdfBrush, 和page.Canvas.DrawString()。基於畫布的繪圖模型,使用 PointF 將文字定位在特定的座標。
IronPdf 使用具有直觀屬性的 TextStamper 物件,如 Text, FontSize, VerticalOffset, 和 HorizontalOffset, 然後用 ApplyStamp() 套用。 這種方法更具宣言性,也更容易維護。
文字即圖片的問題
為什麼這非常重要
當 Spire.PDF 使用影像渲染將 HTML 轉換為 PDF 時,您的文件會失去基本功能:
1.無文字搜尋:使用者無法使用 Ctrl+F 來尋找文字。 文件管理系統無法索引內容。
2.無法選擇/複製文字:使用者嘗試複製引文、參考資料或資料時無法選擇文字 - 這是一種圖像。
3.可访问性违规:基于图像的 PDF 不符合 WCAG 2.1 合规性、Section 508 合规性(美国政府)、ADA 要求和屏幕阅读器兼容性。
4.檔案大小大:相同內容比較顯示 Spire.PDF(以影像為基礎)產生的檔案比 IronPDF(以文字為基礎)大 16 倍。






