如何在 C# 中從 MuPDF 遷移到 IronPDF
為什麼要從MuPDF轉移到 IronPDF?
MuPDF的挑戰
MuPDF 是一款出色的 PDF 渲染器,但其 AGPL 許可證和僅用於渲染的重點對建立商業應用程式的 .NET 開發人員造成了很大的限制:
- AGPL 許可陷阱:MuPDF的病毒式許可要求要么將整個應用程式開源為 AGPL,要么購買價格昂貴且不透明的商業許可,並透過聯繫銷售進行定價。
2.僅渲染功能:MuPDF是一個檢視器/渲染器-它並非設計用於從 HTML 建立 PDF、文件產生工作流程、表單填寫或新增浮水印和頁首/頁尾。
3.不支援 HTML:MuPDF不支援直接將 HTML 轉換為 PDF。 您需要先使用其他函式庫將 HTML 轉換成支援的格式。這是一個基本的限制-MuPDF 主要是一個 PDF 渲染器/檢視器。
4.本機相依性:特定於平台的二進位檔案需要手動管理,適用於 Windows、Linux 和 macOS。 Docker 部署會因本機庫需求而變得複雜,部署包裝也會帶來挑戰。
5.操作有限:不支援合併/分割 PDF、頁面旋轉或重新排序、浮水印或註釋、數位簽章等內建功能。
- C 互通複雜性:原生綁定引入了記憶體管理問題、平台特定的錯誤和編組開銷。
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 函式庫,提供了一個面向未來的基礎,而不會產生本機互操作的複雜性。
遷移複雜性評估
按功能估計的工作量
| 特點 | 遷移複雜性 |
|---|---|
| 文件載入 | 非常低 |
| 文字擷取 | 非常低 |
| PDF 合併 | 低 |
| 圖片渲染 | 低 |
| HTML 至 PDF | 不適用 (新能力) |
| 安全性/水印 | 不適用 (新能力) |
範式轉移
MuPDF此次遷移的根本轉變在於從僅用於渲染的檢視器轉變為完整的PDF解決方案:
MuPDF: MuPDFContext → MuPDFDocument → 頁面迭代 → 僅渲染/提取
IronPDF:PdfDocument.FromFile() → 完全操作 → 創建/編輯/合併/安全
開始之前
先決條件
- .NET環境: .NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取權限:能夠安裝NuGet套件
- IronPDF許可證:請從IronPDF取得您的許可證密鑰。
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";
' Add at application startup (Program.vb or Startup.vb)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
識別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 |
|---|---|
| 循環遍歷 @@--CODE-18121--@@ | pdf.ExtractAllText() |
PDF 製作(MuPDF 中不提供)
| MuPDF | IronPDF |
|---|---|
| (不支援) | ChromePdfRenderer.RenderHtmlAsPdf(html) |
| (不支援) | ChromePdfRenderer.RenderUrlAsPdf(url) |
PDF 操作(MuPDF 中的限制)
| MuPDF | IronPDF |
|---|---|
| 手冊頁面複製循環 | PdfDocument.Merge(pdf1, pdf2) |
| (不支援) | 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");
}
}
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
'MuPDF doesn't support HTML to PDF conversion directly
' You would need to use another library to convert HTML to a supported format first
' This is a limitation - MuPDF is primarily a PDF renderer/viewer
' Alternative: Use a browser engine or intermediate conversion
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
' Not natively supported in MuPDF
Throw New NotSupportedException("MuPDF does not support direct HTML to PDF conversion")
End Sub
End Class
After (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");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
這個範例突顯了MuPDF最顯著的限制:它完全無法將 HTML 轉換成 PDF。MuPDF程式碼明確拋出 NotSupportedException,因為 HTML 到 PDF 的轉換根本不是MuPDF提供的功能。 如果您需要使用MuPDF的這項功能,您必須使用像 wkhtmltopdf 之類的獨立函式庫或瀏覽器引擎,然後再使用MuPDF載入產生的 PDF 以進行檢視。
IronPDF 的 ChromePdfRenderer 使用完整的 Chromium 引擎渲染 HTML,並完全支援 CSS3、JavaScript 和現代 Web 標準。 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());
}
}
}
Imports MuPDFCore
Imports System
Imports System.Text
Class Program
Shared Sub Main()
Using document As New MuPDFDocument("input.pdf")
Dim allText As New StringBuilder()
For i As Integer = 0 To document.Pages.Count - 1
Dim pageText As String = document.Pages(i).GetText()
allText.AppendLine(pageText)
Next
Console.WriteLine(allText.ToString())
End Using
End Sub
End Class
After (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);
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("input.pdf")
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
End Sub
End Class
MuPDF 方法需要建立一個 using 區塊,其中包含 MuPDFDocument,使用 for 循環手動遍歷 document.Pages.Count,對每一頁呼叫 document.Pages[i].GetText(),並使用 @@CO-188132-188134--@@,並使用 @@CO-1888@18。 這是 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");
}
}
}
}
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
Using doc1 As New MuPDFDocument("file1.pdf"),
doc2 As New MuPDFDocument("file2.pdf")
' Create a new document
Using mergedDoc As MuPDFDocument = MuPDFDocument.Create()
' Copy pages from first document
For i As Integer = 0 To doc1.Pages.Count - 1
mergedDoc.CopyPage(doc1, i)
Next
' Copy pages from second document
For i As Integer = 0 To doc2.Pages.Count - 1
mergedDoc.CopyPage(doc2, i)
Next
mergedDoc.Save("merged.pdf")
End Using
End Using
End Sub
End Class
After (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");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim pdf1 = PdfDocument.FromFile("file1.pdf")
Dim pdf2 = PdfDocument.FromFile("file2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)
merged.SaveAs("merged.pdf")
End Sub
End Class
MuPDF 的合併作業特別冗長。 您必須在嵌套的 using 區塊中開啟兩個來源文檔,建立一個新的空白文檔 MuPDFDocument.Create(),遍歷第一個文件的每一頁並呼叫 CopyPage(),遍歷第二個文件的每一頁並呼叫 CopyPage(),遍歷第二個文件的每一頁並呼叫 CopyPage(),遍歷第二個文件的每一頁並呼叫 @@CO2-1814。 這是 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 removeMuPDFinstallation
IronPDF 是完全可管理的 .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 pdf
Imports MuPDF
Using document As New MuPDFDocument("input.pdf")
' Work with document
End Using
Dim pdf = PdfDocument.FromFile("input.pdf")
' Work with pdf
頁面迭代模式變更
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;
}
' MuPDF
For i As Integer = 0 To document.Pages.Count - 1
Dim pageText = document.Pages(i).GetText()
Next
' IronPDF (foreach supported)
For Each page In pdf.Pages
Dim pageText = page.Text
Next
可用的新功能
轉移到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}");
' PDF 製作from HTML (not possible in MuPDF)
Dim 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}")
疑難排解
問題 1:找不到 MuPDFDocument
問題:IronPDF中不存在 MuPDFDocument 類別。
解:使用 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");
Imports MuPDF
Imports IronPDF
Using document As New MuPDFDocument("input.pdf")
End Using
Dim pdf = PdfDocument.FromFile("input.pdf")
問題 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)
' MuPDF
For i As Integer = 0 To document.Pages.Count - 1
' IronPDF
For i As Integer = 0 To pdf.PageCount - 1
' Or use: For Each page In pdf.Pages
問題 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();
' MuPDF
Dim pageText As String = document.Pages(i).GetText()
' IronPDF
Dim pageText As String = pdf.Pages(i).Text
' Or for all text:
Dim allText As String = pdf.ExtractAllText()
問題 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);
' MuPDF
mergedDoc.CopyPage(doc1, i)
' IronPDF
Dim merged = PdfDocument.Merge(pdf1, pdf2)
遷移清單
預遷移
- 清點程式碼庫中所有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() - 將手動
CopyPage循環替換為PdfDocument.Merge() - 移除嵌套的
using程式碼區塊以進行上下文管理 - 如有需要,請新增 PDF 建立程式碼(HTML 轉 PDF)
後遷移
- 從專案中移除原生MuPDF二進位文件
- 更新 Docker 檔案以移除MuPDF安裝
- 刪除平台特定的運行時資料夾
- 執行迴歸測試,比較渲染輸出
- 在所有目標平台(Windows、Linux、macOS)上進行測試
- 考慮新增功能(浮水印、安全設定、頁首/頁尾)

