跳至頁尾內容
移民指南

如何在 C# 中將 MuPDF 遷移到 IronPDF

為什麼要從 PDF格式 遷移到 IronPDF

MuPDF挑戰賽

MuPDF 是一款優秀的 PDF 渲染器,但其 AGPL 授權和僅專注於渲染功能,為建立商業應用程式的 .NET 開發人員帶來了巨大的限制:

  1. AGPL 許可陷阱: PDF格式 的病毒式許可要求要么將整個應用程式開源為 AGPL,要么購買價格昂貴且不透明的商業許可,並透過聯繫銷售進行定價。

2.僅渲染功能: PDF格式 是一個檢視器/渲染器-它並非設計用於從 HTML 建立 PDF、文件產生工作流程、表單填寫或新增浮水印和頁首/頁尾。

3.不支援 HTML: PDF格式 不支援直接將 HTML 轉換為 PDF。 您需要先使用其他程式庫將 HTML 轉換為支援的格式。這是一個根本性的限制——MuPDF 主要是 PDF 渲染器/檢視器。

4.本機相依性:特定於平台的二進位檔案需要手動管理,適用於 Windows、Linux 和 macOS。 Docker 部署會因對原生程式庫的要求而變得複雜,部署打包也會帶來挑戰。

5.操作有限:不支援合併/分割 PDF、頁面旋轉或重新排序、浮水印或註釋、數位簽章等內建功能。

  1. C 互通複雜性:原生綁定引入了記憶體管理問題、平台特定的錯誤和編組開銷。

PDF格式 與 IronPDF 對比

特徵PDF格式IronPDF
執照AGPL(病毒式傳播)或昂貴的商業用途商業定價透明
主要關注點渲染/檢視完整的 PDF 解決方案
HTML 轉 PDF不支援全鉻發動機
PDF 建立不支援HTML、URL、圖片
PDF 處理有限的完成(合併、拆分、編輯)
依賴關係本地二進位文件全託管
平台支援手動按平台操作自動的
非同步支援有限的完全異步/等待
.NET 集成C 互通Native .NET

對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊來說,IronPDF 提供了一個面向未來的基礎,它是一個完全託管的 .NET 庫,沒有原生互通的複雜性。


遷移複雜度評估

各功能預計工作量

特徵遷移複雜性筆記
文件載入中非常低直接替代法
文字擷取非常低更簡單的 API
PDF合併低的靜態方法與手動循環
影像渲染低的RasterizeToImageFiles 與像素圖
HTML 轉 PDF不適用(新增功能)PDF格式 中無法實現
安全/浮水印不適用(新增功能)PDF格式 中無法實現

範式轉移

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 套件變更

# Remove PDF格式 packages
dotnet remove package MuPDF.NET
dotnet remove package MuPDFCore
dotnet remove package MuPDFCore.MuPDFWrapper

# Install IronPDF
dotnet add package IronPdf
# Remove PDF格式 packages
dotnet remove package MuPDF.NET
dotnet remove package MuPDFCore
dotnet remove package MuPDFCore.MuPDFWrapper

# Install IronPDF
dotnet add package IronPdf
SHELL

同時從部署環境中移除原生 PDF格式 二進位檔案

  • 刪除mupdf.dlllibmupdf.solibmupdf.dylib
  • 刪除平台特定資料夾( runtimes/*/native/
  • 更新 Docker 檔案以移除 PDF格式 安裝

許可證配置

// 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";
$vbLabelText   $csharpLabel

識別 PDF格式 使用情況

# Find all PDF格式 references
grep -r "MuPDF\|MuPDFCore\|MuPDFDocument" --include="*.cs" .
# Find all PDF格式 references
grep -r "MuPDF\|MuPDFCore\|MuPDFDocument" --include="*.cs" .
SHELL

完整 API 參考

文件載入中

PDF格式IronPDF筆記
new MuPDFDocument(path)PdfDocument.FromFile(path)從檔案載入
new MuPDFDocument(stream)PdfDocument.FromStream(stream)從串流中載入
document.Dispose()pdf.Dispose()清理

頁面訪問

PDF格式IronPDF筆記
document.Pages.Countpdf.PageCount頁數
document.Pages[index]pdf.Pages[index]訪問頁面
page.GetText()page.Text頁面文本

文字擷取

PDF格式IronPDF筆記
遍歷document.Pages[i].GetText()pdf.ExtractAllText()一次顯示所有文本

PDF 建立(MuPDF 中不可用)

PDF格式IronPDF筆記
(不支持)ChromePdfRenderer.RenderHtmlAsPdf(html)HTML 轉 PDF
(不支持)ChromePdfRenderer.RenderUrlAsPdf(url)PDF檔案的URL

PDF 操作(MuPDF 中的限制)

PDF格式IronPDF筆記
手動頁面複製循環PdfDocument.Merge(pdf1, pdf2)合併PDF
(不支持)pdf.ApplyWatermark(html)添加浮水印
(不支持)pdf.SecuritySettings密碼保護

程式碼遷移範例

範例 1:HTML 轉 PDF(MuPDF 無法完成此操作)

之前(PDF格式):

// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;

class Program
{
    static void Main()
    {
        // PDF格式 doesn't support HTML 轉 PDF conversion directly
        // You would need to use another library to convert HTML to a supported format first
        // This is a limitation - PDF格式 is 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 direct HTML 轉 PDF conversion");
    }
}
// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;

class Program
{
    static void Main()
    {
        // PDF格式 doesn't support HTML 轉 PDF conversion directly
        // You would need to use another library to convert HTML to a supported format first
        // This is a limitation - PDF格式 is 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 direct HTML 轉 PDF conversion");
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

這個例子凸顯了 PDF格式 最主要的限制:它根本無法將 HTML 轉換為 PDF。 PDF格式 程式碼明確拋出NotSupportedException ,因為 HTML 到 PDF 的轉換根本不是 PDF格式 提供的功能。 如果您需要使用 PDF格式 實作此功能,則必須使用像 wkhtmltopdf 這樣的單獨庫或瀏覽器引擎,然後使用 PDF格式 載入產生的 PDF 進行檢視。

IronPDF 的ChromePdfRenderer使用完整的 Chromium 引擎渲染 HTML,並完全支援 CSS3、JavaScript 和現代 Web 標準。 RenderHtmlAsPdf()方法直接接受 HTML 字串。 有關 URL 渲染和 HTML 文件轉換等其他渲染選項,請參閱HTML 轉 PDF 文件

範例 2:文字擷取

之前(PDF格式):

// 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());
        }
    }
}
$vbLabelText   $csharpLabel

(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);
    }
}
$vbLabelText   $csharpLabel

MuPDF 方法需要建立一個帶有MuPDFDocument using區塊,使用 for 迴圈手動遍歷document.Pages.Count ,對每一頁呼叫document.Pages[i].GetText() ,並使用StringBuilder建立文字。 這是用於簡單文字提取的 12 行程式碼。

IronPDF 將此操作簡化為 3 行程式碼:使用PdfDocument.FromFile()載入文檔,呼叫ExtractAllText() ,並列印結果。 對於這種簡單的操作,無需手動迭代,無需 StringBuilder,也無需using程式碼區塊進行明確資源管理。 了解更多關於從PDF中提取文字的資訊

範例 3:合併多個 PDF 文件

之前(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");
            }
        }
    }
}
// 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");
            }
        }
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

MuPDF合併操作尤其冗長。 您必須在嵌套的using區塊中開啟兩個來源文檔,使用MuPDFDocument.Create()建立一個新的空文檔,遍歷第一個文檔的每一頁並調用CopyPage() ,遍歷第二個文檔的每一頁並調用CopyPage() ,最後儲存。 那可是二十多行程式碼,而且嵌套結構很複雜。

IronPDF 的靜態方法PdfDocument.Merge()接受多個 PDF 文件並傳回一個合併後的文件。 整個操作僅需 4 行易讀程式碼。 若要合併多個文檔,您可以傳遞一個清單: PdfDocument.Merge(pdfList) 。 如需更多選項,請參閱合併和拆分 PDF 文件


關鍵遷移說明

移除本地二進位文件

MuPDF 需要平台特定的本機庫。 遷移到 IronPDF 後,請刪除所有 PDF格式 原生二進位檔案:

# Delete native libraries
rm -f mupdf*.dll libmupdf*.so libmupdf*.dylib

# Remove runtime folders
rm -rf runtimes/*/native/

# Update Docker files to remove PDF格式 installation
# Delete native libraries
rm -f mupdf*.dll libmupdf*.so libmupdf*.dylib

# Remove runtime folders
rm -rf runtimes/*/native/

# Update Docker files to remove PDF格式 installation
SHELL

IronPDF 是完全託管的 .NET 程式碼-無需跨平台管理原生二進位檔案。

處置模式簡化

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
$vbLabelText   $csharpLabel

頁面迭代模式變化

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;
}
$vbLabelText   $csharpLabel

新增功能

遷移到 IronPDF 後,您將獲得 PDF格式 無法提供的功能:

// 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}");
$vbLabelText   $csharpLabel

故障排除

問題 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");
$vbLabelText   $csharpLabel

問題 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)
$vbLabelText   $csharpLabel

問題 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();
$vbLabelText   $csharpLabel

問題 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);
$vbLabelText   $csharpLabel

遷移清單

遷移前

  • 清點程式碼庫中所有 PDF格式 的使用情況
  • 記錄所有渲染操作(DPI、縮放因子)
  • 確定任何 PDF 創建需求(目前使用外部工具)
  • 列出文字擷取要求
  • 檢查部署腳本中的原生二進位檔案處理情況
  • 取得 IronPDF 許可證密鑰

軟體包變更

  • 刪除MuPDF.NET
  • 移除MuPDFCore軟體包
  • 刪除MuPDFCore.MuPDFWrapper包 安裝IronPdf NuGet 套件: dotnet add package IronPdf
  • 更新命名空間匯入

程式碼更改

  • 在啟動時新增許可證金鑰配置
  • MuPDFDocument替換為PdfDocument.FromFile()
  • document.Pages.Count替換為pdf.PageCount
  • page.GetText()替換為page.Textpdf.ExtractAllText()
  • 將手動CopyPage循環替換為PdfDocument.Merge()
  • 移除嵌套的using語句區塊以進行上下文管理
  • 如有需要,請新增 PDF 建立程式碼(HTML 轉 PDF)

移民後

  • 從專案中移除原生 PDF格式 二進位文件
  • 更新 Docker 檔案以移除 PDF格式 安裝
  • 刪除平台特定的運行時資料夾
  • 執行迴歸測試,比較渲染輸出
  • 在所有目標平台(Windows、Linux、macOS)上進行測試
  • 考慮新增功能(浮水印、安全設定、頁首/頁尾)

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。