如何在 C# 中從 MuPDF 遷移到 IronPDF
從MuPDF轉移到 IronPDF:完整的 C# 遷移指南。
從MuPDF轉換到 IronPDF,可將您的 .NET PDF 工作流程從一個僅具備 AGPL 授權問題和本機依賴性的渲染函式庫,轉換為一個具有完整創建、操作和安全功能的完整 PDF 解決方案。 本指南提供了一個全面、循序漸進的遷移路徑,在消除本機二進位管理的同時,增加了MuPDF無法提供的 HTML 至 PDF 轉換功能。
為什麼要從MuPDF轉移到 IronPDF?
MuPDF的挑戰
MuPDF 是一款出色的 PDF 渲染器,但其 AGPL 許可證和僅用於渲染的重點對建立商業應用程式的 .NET 開發人員造成了很大的限制:
1.AGPL 授權陷阱:MuPDF 的病毒式授權要求您在 AGPL 下開放整個應用程式的原始碼,或是購買價格昂貴的商業授權,而這些授權的定價並不透明。
2.Rendering-Only Focus:MuPDF 是一個檢視器/渲染器 - 它並非設計用於從 HTML 建立 PDF、文件產生工作流程、填寫表單或新增水印和頁首/頁尾。
3.不支援 HTML:MuPDF 不支援 HTML 直接轉換為 PDF。 您需要先使用其他函式庫將 HTML 轉換成支援的格式。這是一個基本的限制-MuPDF 主要是一個 PDF 渲染器/檢視器。
4.原生相依性:特定平台的二進位檔需要手動管理 Windows、Linux 和 macOS。 Docker 部署會因本機庫需求而變得複雜,部署包裝也會帶來挑戰。
5.Limited Manipulation(有限的操作):內建不支援合併/分割 PDF、頁面旋轉或重新排序、水印或註解或數位簽章。
6.C Interop Complexity:原生綁定會引入記憶體管理問題、特定平台的錯誤以及編譯開銷。
MuPDF與IronPDF的比較
| 特點 | MuPDF | IronPDF |
|---|---|---|
| 許可證 | AGPL(病毒式)或昂貴的商業 | 價格透明的商業翻譯 |
| 主要焦點 | 渲染/檢視 | 完整的 PDF 解決方案 |
| HTML 至 PDF | 不支援 | 完整的 Chromium 引擎 |
| PDF 製作 | 不支援 | HTML、URL、影像 |
| PDF 操作 | 限額 | 完成(合併、分割、編輯) |
| 依賴 | 原生二進位檔 | 完全管理 |
| 平台支援 | 每平台手冊 | 自動化 |
| 同步支援 | 限額 | 完整的 async/await |
| .NET 集成 | C# 互操作 | 原生 .NET |
對於計劃在 2025 年和 2026 年之前採用 .NET 10 和 C# 14 的團隊而言,IronPDF 作為一個完全受管理的 .NET 函式庫,提供了一個面向未來的基礎,而不會產生本機互操作的複雜性。
遷移複雜性評估
按功能估計的工作量
| 特點 | 遷移複雜性 | 筆記 |
|---|---|---|
| 文件載入 | 非常低 | 直接方法替換 |
| 文字擷取 | 非常低 | 更簡單的 API |
| PDF 合併 | 低 | 靜態方法與手動循環 |
| 圖片渲染 | 低 | RasterizeToImageFiles vs pixmaps |
| HTML 至 PDF | 不適用 (新能力) | 無法使用 MuPDF |
| 安全性/水印 | 不適用 (新能力) | 無法使用 MuPDF |
範式轉移
這次MuPDF移轉的根本轉變是從 僅渲染的檢視器 到 完整的 PDF 解決方案:
MuPDF: MuPDFContext → MuPDFDocument → 頁面迭代 → 僅渲染/提取
IronPDF:PdfDocument.FromFile() → 完全操作 → 創建/編輯/合併/安全開始之前
先決條件
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 套件變更
# RemoveMuPDFpackages
dotnet remove package MuPDF.NET
dotnet remove package MuPDFCore
dotnet remove package MuPDFCore.MuPDFWrapper
# Install IronPDF
dotnet add package IronPdf# RemoveMuPDFpackages
dotnet remove package MuPDF.NET
dotnet remove package MuPDFCore
dotnet remove package MuPDFCore.MuPDFWrapper
# Install IronPDF
dotnet add package IronPdf同時從您的部署中移除原生的MuPDF二進位檔案:
- 刪除
mupdf.dll,libmupdf.so,libmupdf.dylib. - 移除特定平台的資料夾 (
runtimes/*/native/) - 更新 Docker 檔案以移除MuPDF安裝
授權組態
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IRON VB CONVERTER ERROR developers@ironsoftware.com識別MuPDF使用方式
# Find allMuPDFreferences
grep -r "MuPDF\|MuPDFCore\|MuPDFDocument" --include="*.cs" .# Find allMuPDFreferences
grep -r "MuPDF\|MuPDFCore\|MuPDFDocument" --include="*.cs" .完整的 API 參考資料
文件載入
| MuPDF | IronPDF | 筆記 |
|---|---|---|
new MuPDFDocument(path)。 | PdfDocument.FromFile(path) | 從檔案載入 |
new MuPDFDocument(stream)。 | PdfDocument.FromStream(stream) | 從串流載入 |
| <編碼>document.Dispose()</編碼 | <代碼>pdf.Dispose()</代碼 | 清理 |
頁面存取
| MuPDF | IronPDF | 筆記 |
|---|---|---|
| <編碼>document.Pages.Count</編碼 | <編碼>pdf.PageCount</編碼 | 頁數 |
| <編碼>document.Pages[index]</編碼 | <編碼>pdf.Pages[index]</編碼 | 存取頁面 |
| <編碼>page.GetText()</編碼 | page.Text | 頁面文字 |
文字萃取
| MuPDF | IronPDF | 筆記 |
|---|---|---|
在 document.Pages[i].GetText() 中滾動 | <代碼>pdf.ExtractAllText()</代碼 | 一次翻譯所有文字 |
PDF 製作(MuPDF 中不提供)
| MuPDF | IronPDF | 筆記 |
|---|---|---|
| (不支援) | <代碼>ChromePdfRenderer.RenderHtmlAsPdf(html)</代碼 | HTML 至 PDF |
| (不支援) | <代碼>ChromePdfRenderer.RenderUrlAsPdf(url)</代碼 | URL 至 PDF |
PDF 操作(MuPDF 中的限制)
| MuPDF | IronPDF | 筆記 |
|---|---|---|
| 手冊頁面複製循環 | PdfDocument.Merge(pdf1,pdf2)。 | 合併 PDF |
| (不支援) | pdf.ApplyWatermark(html)。 | 加入水印 |
| (不支援) | <編碼>pdf.SecuritySettings</編碼 | 密碼保護 |
程式碼遷移範例
範例 1:HTML 到 PDF 的轉換(MuPDF 無法做到這一點)
之前 (MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
//MuPDFdoesn't supportHTML 至 PDFconversion directly
// You would need to use another library to convert HTML to a supported format first
// This is a limitation -MuPDFis primarily a PDF renderer/viewer
// Alternative: Use a browser engine or intermediate conversion
string html = "<html><body><h1>Hello World</h1></body></html>";
// Not natively supported in MuPDF
throw new NotSupportedException("MuPDF does not support directHTML 至 PDFconversion");
}
}// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
//MuPDFdoesn't supportHTML 至 PDFconversion directly
// You would need to use another library to convert HTML to a supported format first
// This is a limitation -MuPDFis primarily a PDF renderer/viewer
// Alternative: Use a browser engine or intermediate conversion
string html = "<html><body><h1>Hello World</h1></body></html>";
// Not natively supported in MuPDF
throw new NotSupportedException("MuPDF does not support directHTML 至 PDFconversion");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></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></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com這個範例突顯了MuPDF最顯著的限制:它完全無法將 HTML 轉換成 PDF。MuPDF程式碼明確拋出 NotSupportedException 因為 HTML 到 PDF 的轉換根本不是MuPDF所提供的功能。 如果您需要使用MuPDF的這項功能,您必須使用像 wkhtmltopdf 之類的獨立函式庫或瀏覽器引擎,然後再使用MuPDF載入產生的 PDF 以進行檢視。
IronPdf 的 ChromePdfRenderer 使用完整的 Chromium 引擎來呈現 HTML,並支援完整的 CSS3、JavaScript 和現代網頁標準。 RenderHtmlAsPdf() 方法直接接受 HTML 字串。 請參閱 HTML to PDF 文件,以瞭解其他呈現選項,包括 URL 呈現和 HTML 檔案轉換。
範例 2:文字萃取
之前 (MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System;
using System.Text;
class Program
{
static void Main()
{
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
{
StringBuilder allText = new StringBuilder();
for (int i = 0; i < document.Pages.Count; i++)
{
string pageText = document.Pages[i].GetText();
allText.AppendLine(pageText);
}
Console.WriteLine(allText.ToString());
}
}
}// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System;
using System.Text;
class Program
{
static void Main()
{
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
{
StringBuilder allText = new StringBuilder();
for (int i = 0; i < document.Pages.Count; i++)
{
string pageText = document.Pages[i].GetText();
allText.AppendLine(pageText);
}
Console.WriteLine(allText.ToString());
}
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comMuPDF 方法需要使用 MuPDFDocument 建立一個 using 區塊,以 for 環路手動遍歷<編碼>document.Pages.Count</編碼,為每個頁面呼叫 document.Pages[i].GetText() 並使用 StringBuilder 建立文字。 這是 12 行代碼的簡單文字擷取。
IronPDF 將此功能縮減為 3 行:使用 PdfDocument.FromFile() 載入文件,呼叫 ExtractAllText() 並列印結果。 這個簡單的操作不需要手動迭代、不需要 StringBuilder、不需要使用 using 區塊進行明確的資源管理。 進一步瞭解 從 PDF 中提取文字。
範例 3:合併多個 PDF 檔案
之前 (MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
using (MuPDFDocument doc1 = new MuPDFDocument("file1.pdf"))
using (MuPDFDocument doc2 = new MuPDFDocument("file2.pdf"))
{
// Create a new document
using (MuPDFDocument mergedDoc = MuPDFDocument.Create())
{
// Copy pages from first document
for (int i = 0; i < doc1.Pages.Count; i++)
{
mergedDoc.CopyPage(doc1, i);
}
// Copy pages from second document
for (int i = 0; i < doc2.Pages.Count; i++)
{
mergedDoc.CopyPage(doc2, i);
}
mergedDoc.Save("merged.pdf");
}
}
}
}// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
using (MuPDFDocument doc1 = new MuPDFDocument("file1.pdf"))
using (MuPDFDocument doc2 = new MuPDFDocument("file2.pdf"))
{
// Create a new document
using (MuPDFDocument mergedDoc = MuPDFDocument.Create())
{
// Copy pages from first document
for (int i = 0; i < doc1.Pages.Count; i++)
{
mergedDoc.CopyPage(doc1, i);
}
// Copy pages from second document
for (int i = 0; i < doc2.Pages.Count; i++)
{
mergedDoc.CopyPage(doc2, i);
}
mergedDoc.Save("merged.pdf");
}
}
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comAfter (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("file1.pdf");
var pdf2 = PdfDocument.FromFile("file2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("file1.pdf");
var pdf2 = PdfDocument.FromFile("file2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comMuPDF 的合併作業特別冗長。 您必須在嵌套的 using 區塊中開啟兩個來源文件,使用 MuPDFDocument.Create() 建立一個新的空白文件,呼叫 CopyPage() 遍歷第一個文件的每一頁,呼叫 CopyPage() 遍歷第二個文件的每一頁,最後儲存。 這是 20 多行複雜嵌套的程式碼。
IronPDF 的靜態 PdfDocument.Merge() 方法接受多個 PDF 文件並傳回單一合併文件。 整個作業為 4 行可讀的程式碼。 若要合併許多文件,您可以傳送清單:PdfDocument.Merge(pdfList). 請參閱 合併與分割 PDF 文件,以瞭解其他選項。
關鍵遷移注意事項
移除原生二進位檔案
MuPDF 需要特定平台的本機庫。 遷移至IronPDF後,移除所有MuPDF原生二進位檔案:
# Delete native libraries
rm -f mupdf*.dll libmupdf*.so libmupdf*.dylib
# Remove runtime folders
rm -rf runtimes/*/native/
# Update Docker files to removeMuPDFinstallation# Delete native libraries
rm -f mupdf*.dll libmupdf*.so libmupdf*.dylib
# Remove runtime folders
rm -rf runtimes/*/native/
# Update Docker files to removeMuPDFinstallationIronPDF 是完全可管理的 .NET 程式碼,不需要跨平台管理本機二進位檔。
Dispose 模式簡化。
MuPDF 需要明確的上下文和文件管理:
// MuPDF: Nested using blocks required
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
{
// Work with document
}
// IronPDF: Simpler pattern
var pdf = PdfDocument.FromFile("input.pdf");
// Work with pdf// MuPDF: Nested using blocks required
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
{
// Work with document
}
// IronPDF: Simpler pattern
var pdf = PdfDocument.FromFile("input.pdf");
// Work with pdfIRON VB CONVERTER ERROR developers@ironsoftware.com頁面迭代模式變更
MuPDF 使用基於索引的迭代方式,並明確列出頁數:
// MuPDF
for (int i = 0; i < document.Pages.Count; i++)
{
var pageText = document.Pages[i].GetText();
}
//IronPDF(foreach supported)
foreach (var page in pdf.Pages)
{
var pageText = page.Text;
}// MuPDF
for (int i = 0; i < document.Pages.Count; i++)
{
var pageText = document.Pages[i].GetText();
}
//IronPDF(foreach supported)
foreach (var page in pdf.Pages)
{
var pageText = page.Text;
}IRON VB CONVERTER ERROR developers@ironsoftware.com可用的新功能
轉移到IronPDF之後,您將獲得MuPDF無法提供的功能:
//PDF 製作from HTML (not possible in MuPDF)
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello</h1>");
// Watermarks (not possible in MuPDF)
pdf.ApplyWatermark("<div style='color:red;opacity:0.3;'>DRAFT</div>");
// Password Protection (not possible in MuPDF)
pdf.SecuritySettings.OwnerPassword = "admin";
pdf.SecuritySettings.UserPassword = "user";
// Headers and Footers (not possible in MuPDF)
pdf.AddTextHeader("Document Title");
pdf.AddTextFooter("Page {page} of {total-pages}");//PDF 製作from HTML (not possible in MuPDF)
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello</h1>");
// Watermarks (not possible in MuPDF)
pdf.ApplyWatermark("<div style='color:red;opacity:0.3;'>DRAFT</div>");
// Password Protection (not possible in MuPDF)
pdf.SecuritySettings.OwnerPassword = "admin";
pdf.SecuritySettings.UserPassword = "user";
// Headers and Footers (not possible in MuPDF)
pdf.AddTextHeader("Document Title");
pdf.AddTextFooter("Page {page} of {total-pages}");IRON VB CONVERTER ERROR developers@ironsoftware.com疑難排解
問題 1:找不到 MuPDFDocument
問題:MuPDFDocument class 在IronPDF中不存在。
解決方案:使用 PdfDocument.FromFile():
// MuPDF
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
// IronPDF
var pdf = PdfDocument.FromFile("input.pdf");// MuPDF
using (MuPDFDocument document = new MuPDFDocument("input.pdf"))
// IronPDF
var pdf = PdfDocument.FromFile("input.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.com問題 2:找不到 Pages.Count
問題:document.Pages.Count 模式不起作用。
解決方案:使用 pdf.PageCount:
// MuPDF
for (int i = 0; i < document.Pages.Count; i++)
// IronPDF
for (int i = 0; i < pdf.PageCount; i++)
// Or use: foreach (var page in pdf.Pages)// MuPDF
for (int i = 0; i < document.Pages.Count; i++)
// IronPDF
for (int i = 0; i < pdf.PageCount; i++)
// Or use: foreach (var page in pdf.Pages)IRON VB CONVERTER ERROR developers@ironsoftware.com問題 3:GetText() 未找到
問題:page.GetText() 方法不存在。
解決方案:使用page.Text屬性或<代碼>pdf.ExtractAllText()</代碼:
// MuPDF
string pageText = document.Pages[i].GetText();
// IronPDF
string pageText = pdf.Pages[i].Text;
// Or for all text:
string allText = pdf.ExtractAllText();// MuPDF
string pageText = document.Pages[i].GetText();
// IronPDF
string pageText = pdf.Pages[i].Text;
// Or for all text:
string allText = pdf.ExtractAllText();IRON VB CONVERTER ERROR developers@ironsoftware.com問題 4:找不到 CopyPage
問題:手動合併的頁面複製模式。
解決方案:使用靜態 PdfDocument.Merge():
// MuPDF
mergedDoc.CopyPage(doc1, i);
// IronPDF
var merged = PdfDocument.Merge(pdf1, pdf2);// MuPDF
mergedDoc.CopyPage(doc1, i);
// IronPDF
var merged = PdfDocument.Merge(pdf1, pdf2);IRON VB CONVERTER ERROR developers@ironsoftware.com遷移清單
預遷移
- [清查程式碼庫中所有MuPDF的使用情況
- [ ] 記錄所有的渲染操作(DPI、比例因子)
- [ ] 確認任何 PDF 製作需求(目前使用外部工具)
- [ ] 列出文本提取要求
- [ ] 檢閱部署腳本以進行原生二進位處理
- [ ] 獲得 IronPdf 授權金鑰
套件變更
- [ ] 移除
MuPDF.NET套件 - [ ] 移除
MuPDFCore套件 - [ ] 移除
MuPDFCore.MuPDFWrapper套件 - [ ] 安裝
IronPDFNuGet 套件:dotnet add package IronPdf。 - [ ] 更新命名空間匯入
程式碼變更
- [ ] 在啟動時加入授權金鑰組態
- [ ] 將
MuPDFDocument替換為PdfDocument.FromFile() - [ ] 將<編碼>document.Pages.Count</編碼替換為
pdf.PageCount - [ ] 將<編碼>page.GetText()</編碼替換為
page.Text或<代碼>pdf.ExtractAllText()</代碼。 - [ ] 使用
PdfDocument.Merge()取代手動CopyPage環路 - [ ] 移除用於上下文管理的嵌套
using區塊 - [ ] 如果需要,添加 PDF 創建代碼(HTML 至 PDF)
後遷移
- [從專案中移除原生的MuPDF二進位檔
- [ ] 更新 Docker 檔案以移除MuPDF安裝
- [ ] 移除特定平台的執行時資料夾
- [ ] 執行回歸測試,比較呈現的輸出
- [ ] 在所有目標平台(Windows、Linux、macOS)上進行測試
- [ ] 考慮增加新功能(水印、安全性、頁首/頁腳)
結論
從MuPDF轉換到IronPDF可消除 AGPL 授權疑慮、原生二進位管理以及僅限渲染的限制,同時增加完整的 PDF 建立與操作功能。 從檢視器函式庫到完整 PDF 解決方案的範式轉換,意味著您可以將多種工具整合到單一函式庫中。
本次轉換的主要變更如下 1.授權:AGPL (病毒) → 商業 (透明) 2.重點:僅渲染 → 完整的 PDF 解決方案 3.HTML 至 PDF:不支援 → 完整 Chromium 引擎 4.相依性:原生二進位檔案 → 完全受管理 5.合併:手動頁面循環 → PdfDocument.Merge() 6.文字萃取:循環 + StringBuilder → ExtractAllText()






