
如何在 C# 中旋轉 PDF 文字
為什麼從MuPDF遷移到IronPDF
MuPDF的挑戰
MuPDF是一個功能強大的PDF工具包,但因其雙重AGPL/商業授權(來自Artifex)和以渲染為主的設計對於構建商業HTML驅動應用程式的.NET開發人員來說造成了現實的摩擦。 存在兩個主要的.NET包裝器:MuPDFCore(由arklumpus社群開發,AGPL-3.0)和MuPDF.NET(Artifex的官方C#綁定,類似PyMuPDF)。
-
AGPL授權陷阱: 無論是MuPDF還是包裝器預設均以AGPL-3.0授權發行。 在AGPL程式碼上分發閉源產品需符合開源AGPL或從Artifex購買商業授權(價格基於報價,不公開)。
-
以渲染為主的焦點: 雖然MuPDF.NET 可以 合併、修訂、簽名和載入浮水印(類似PyMuPDF),但大部分API和工具都以檢視和結構化文字提取為中心,而非HTML驅動的文件生成。
-
沒有HTML轉PDF引擎: 無論是MuPDF.NET還是MuPDFCore都不提供HTML/CSS引擎。從HTML生成PDF需要調用單獨的瀏覽器引擎(Chromium, wkhtmltopdf, PrinceXML),然後僅用MuPDF載入結果。
-
本機依賴: 兩個包裝器都以
MuPDFCore.NativeAssets/MuPDF.NativeAssets包發行,內含每個RID的二進位檔(win-x64, linux-x64, osx-x64)。 跨平台部署可行,但Docker鏡像需要匹配的本機包和基礎鏡像(glibc與musl)。 -
沒有HTML浮水印/標題/頁尾: 每頁的加蓋是基於圖像和文字的; 沒有
{page}/{total-pages}範本引擎,正如基於Chromium的渲染器所提供的那樣。 -
C互操作介面: 包裝器暴露了映射到底層C庫的上下文/文件生存期——處置順序很重要,原生崩潰會導致整個程式中斷,對於大文件的編排成本不容小覷。
MuPDF與IronPDF比較
| 功能 | MuPDF | IronPDF |
|---|---|---|
| 授權 | AGPL-3.0或商業報價(Artifex) | 商業授權,價格已公佈 |
| 主要焦點 | 渲染、結構化文字、低層次工具包 | HTML 驅動的生成與操作 |
| HTML轉PDF | 不支持(沒有HTML引擎) | 完整的Chromium引擎 |
| PDF建立 | 僅限頁面級(插入文字/圖像/向量) | HTML,URL,圖像 |
| PDF操作 | 在MuPDF.NET中支持(InsertPdf, Select, DeletePage) | 支持(Merge, CopyPages, RemovePages) |
| 依賴關係 | 本機MuPDF + RID NativeAssets | 託管包裝器 + 捆绑Chromium |
| 平台支持 | Win/Linux/macOS 使用NativeAssets | Win/Linux/macOS/Docker |
| 異步支援 | 主要是同步的; MuPDFCore中有一些異步 | 異步API跨渲染器和文件 |
| .NET整合 | C庫周圍的P/Invoke包裝器 | .NET包裝器 + 本機Chromium |
IronPDF作為現代.NET版本之上的託管.NET程式庫運行,沒有本機互操作的複雜性。
遷移複雜性評估
功能的預估努力程度
| 功能 | 遷移複雜性 |
|---|---|
| 文件載入 | 非常低 |
| 文字提取 | 非常低 |
| PDF合併 | 低 |
| 圖像渲染 | 低 |
| HTML轉PDF | 不適用(新能力) |
| 安全/浮水印 | 不適用(新能力) |
範式轉變
此次MuPDF遷移的基本轉變是從 僅渲染查看器 轉變為 完整的PDF解決方案:
MuPDF.NET: Document → doc[i] → GetText / GetPixmap / InsertPdf
MuPDFCore: MuPDFContext → MuPDFDocument → Pages[i] → Render / SaveImage
IronPDF: PdfDocument.FromFile() → 完全操作 → 創建/編輯/合併/安全
開始之前
前提條件
- .NET Environment: .NET Framework 4.6.2+ or .NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取: 有能力安裝NuGet套件
- IronPDF授權: 從ironpdf.com獲取您的授權金鑰
NuGet包變更
# RemoveMuPDFpackages (whichever wrapper you used)
dotnet remove package MuPDF.NET
dotnet remove package MuPDFCore
dotnet remove package MuPDFCore.NativeAssets.Linux
dotnet remove package MuPDFCore.NativeAssets.MacOS
dotnet remove package MuPDFCore.NativeAssets.Windows
dotnet remove package MuPDF.NativeAssets
# 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.vb or Startup.vb)
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" .
完整API參考
文件載入
| MuPDF | IronPDF |
|---|---|
new Document(path) (MuPDF.NET) / new MuPDFDocument(ctx, path) (MuPDFCore) | PdfDocument.FromFile(path) |
new Document("pdf", stream) (MuPDF.NET) | PdfDocument.FromStream(stream) |
doc.Close() / Dispose() | pdf.Dispose() |
頁面存取
| MuPDF | IronPDF |
|---|---|
doc.PageCount (MuPDF.NET) / document.Pages.Count (MuPDFCore) | pdf.PageCount |
doc[index] (MuPDF.NET) / document.Pages[index] (MuPDFCore) | pdf.Pages[index] |
doc[i].GetText() / 結構化文字迭代(MuPDFCore) | page.Text |
文字提取
| MuPDF | IronPDF |
|---|---|
迴圈 doc[i].GetText() | pdf.ExtractAllText() |
PDF建立(MuPDF中不可用)
| MuPDF | IronPDF |
|---|---|
| (沒有HTML引擎) | ChromePdfRenderer.RenderHtmlAsPdf(html) |
| (沒有HTML引擎) | ChromePdfRenderer.RenderUrlAsPdf(url) |
PDF操作
| MuPDF | IronPDF |
|---|---|
docA.InsertPdf(docB) (MuPDF.NET) | PdfDocument.Merge(pdf1, pdf2) |
每頁page.InsertImage(...)(僅圖像/文字浮水印) | pdf.ApplyWatermark(html) |
doc.Save(path, encryption: ..., ownerPw: ..., userPw: ...) | 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");
}
}' NuGet: Install-Package MuPDF.NET
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
' MuPDF 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 - 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 轉 PDF conversion")
End Sub
End Class' NuGet: Install-Package MuPDF.NET
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
' MuPDF 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 - 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 轉 PDF conversion")
End Sub
End Class之後(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");
}
}' NuGet: Install-Package IronPdf
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' NuGet: Install-Package IronPdf
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不提供HTML到PDF轉換的能力,所以MuPDF程式碼會顯式拋出NotSupportedException。 如果您需要MuPDF的此功能,就必須使用像wkhtmltopdf之類的單獨庫或瀏覽器引擎,然後使用MuPDF載入結果的PDF進行查看。
IronPDF的ChromePdfRenderer使用完整的Chromium引擎來渲染HTML,支援完整的CSS3、JavaScript和現代網路標準。 RenderHtmlAsPdf() 方法直接接收HTML字串。 查看 HTML轉PDF文件 以獲取更多渲染選項,包括URL渲染和HTML文件轉換。
範例2:文字提取
之前(MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDF.NET;
using System;
using System.Text;
class Program
{
static void Main()
{
Document doc = new Document("input.pdf");
StringBuilder allText = new StringBuilder();
for (int i = 0; i < doc.PageCount; i++)
{
string pageText = doc[i].GetText();
allText.AppendLine(pageText);
}
Console.WriteLine(allText.ToString());
}
}// NuGet: Install-Package MuPDF.NET
using MuPDF.NET;
using System;
using System.Text;
class Program
{
static void Main()
{
Document doc = new Document("input.pdf");
StringBuilder allText = new StringBuilder();
for (int i = 0; i < doc.PageCount; i++)
{
string pageText = doc[i].GetText();
allText.AppendLine(pageText);
}
Console.WriteLine(allText.ToString());
}
}Imports MuPDF.NET
Imports System
Imports System.Text
Module Program
Sub Main()
Dim doc As New Document("input.pdf")
Dim allText As New StringBuilder()
For i As Integer = 0 To doc.PageCount - 1
Dim pageText As String = doc(i).GetText()
allText.AppendLine(pageText)
Next
Console.WriteLine(allText.ToString())
End Sub
End ModuleImports MuPDF.NET
Imports System
Imports System.Text
Module Program
Sub Main()
Dim doc As New Document("input.pdf")
Dim allText As New StringBuilder()
For i As Integer = 0 To doc.PageCount - 1
Dim pageText As String = doc(i).GetText()
allText.AppendLine(pageText)
Next
Console.WriteLine(allText.ToString())
End Sub
End Module之後(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);
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Module Program
Sub Main()
Dim pdf = PdfDocument.FromFile("input.pdf")
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
End Sub
End Module' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Module Program
Sub Main()
Dim pdf = PdfDocument.FromFile("input.pdf")
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
End Sub
End ModuleMuPDF.NET方法打開StringBuilder累積文字。
IronPDF將其簡化到三行:用ExtractAllText(),然後列印結果。 無需手動迭代,無需StringBuilder。 了解更多關於從PDF中提取文字的內容。
範例3:合併多個PDF
之前(MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDF.NET;
class Program
{
static void Main()
{
Document doc1 = new Document("file1.pdf");
Document doc2 = new Document("file2.pdf");
// Append every page of doc2 to the end of doc1.
doc1.InsertPdf(doc2);
doc1.Save("merged.pdf");
}
}// NuGet: Install-Package MuPDF.NET
using MuPDF.NET;
class Program
{
static void Main()
{
Document doc1 = new Document("file1.pdf");
Document doc2 = new Document("file2.pdf");
// Append every page of doc2 to the end of doc1.
doc1.InsertPdf(doc2);
doc1.Save("merged.pdf");
}
}Imports MuPDF.NET
Class Program
Shared Sub Main()
Dim doc1 As New Document("file1.pdf")
Dim doc2 As New Document("file2.pdf")
' Append every page of doc2 to the end of doc1.
doc1.InsertPdf(doc2)
doc1.Save("merged.pdf")
End Sub
End ClassImports MuPDF.NET
Class Program
Shared Sub Main()
Dim doc1 As New Document("file1.pdf")
Dim doc2 As New Document("file2.pdf")
' Append every page of doc2 to the end of doc1.
doc1.InsertPdf(doc2)
doc1.Save("merged.pdf")
End Sub
End Class之後(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");
}
}' NuGet: Install-Package IronPdf
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' NuGet: Install-Package IronPdf
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 ClassMuPDF.NET透過insert_pdf(),並把一個文件的每一頁追加到另一個文件中。 MuPDFCore沒有暴露等效的高級合併API。
IronPDF的靜態PdfDocument.Merge()方法接受多個PDF文件並返回一個已合併的單一文件,輸入文件保持不變。 若合併多個文件,可傳遞一個清單: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
IronPDF完全是託管的.NET程式碼——無需管理跨平台的本機二進位檔。
簡化處置模式
MuPDFCore需要顯式MuPDFContext; MuPDF.NET隱藏了它但仍然需要Close() / Dispose():
// MuPDFCore: explicit context required
using (var context = new MuPDFContext())
using (var document = new MuPDFDocument(context, "input.pdf"))
{
// Work with document
}
// MuPDF.NET: implicit context, manual close
Document doc = new Document("input.pdf");
try { /* work */ } finally { doc.Close(); }
// IronPDF: simpler pattern
var pdf = PdfDocument.FromFile("input.pdf");
// Work with pdf// MuPDFCore: explicit context required
using (var context = new MuPDFContext())
using (var document = new MuPDFDocument(context, "input.pdf"))
{
// Work with document
}
// MuPDF.NET: implicit context, manual close
Document doc = new Document("input.pdf");
try { /* work */ } finally { doc.Close(); }
// IronPDF: simpler pattern
var pdf = PdfDocument.FromFile("input.pdf");
// Work with pdfImports MuPDFCore
Imports MuPDF.NET
Imports IronPDF
' MuPDFCore: explicit context required
Using context As New MuPDFContext()
Using document As New MuPDFDocument(context, "input.pdf")
' Work with document
End Using
End Using
' MuPDF.NET: implicit context, manual close
Dim doc As New Document("input.pdf")
Try
' work
Finally
doc.Close()
End Try
' IronPDF: simpler pattern
Dim pdf = PdfDocument.FromFile("input.pdf")
' Work with pdfImports MuPDFCore
Imports MuPDF.NET
Imports IronPDF
' MuPDFCore: explicit context required
Using context As New MuPDFContext()
Using document As New MuPDFDocument(context, "input.pdf")
' Work with document
End Using
End Using
' MuPDF.NET: implicit context, manual close
Dim doc As New Document("input.pdf")
Try
' work
Finally
doc.Close()
End Try
' IronPDF: simpler pattern
Dim pdf = PdfDocument.FromFile("input.pdf")
' Work with pdf頁面迭代模式變更
MuPDF使用基於索引的迭代加上顯式頁面計數:
// MuPDF.NET
for (int i = 0; i < doc.PageCount; i++)
{
var pageText = doc[i].GetText();
}
// MuPDFCore
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.NET
for (int i = 0; i < doc.PageCount; i++)
{
var pageText = doc[i].GetText();
}
// MuPDFCore
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.NET
For i As Integer = 0 To doc.PageCount - 1
Dim pageText = doc(i).GetText()
Next
' MuPDFCore
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' MuPDF.NET
For i As Integer = 0 To doc.PageCount - 1
Dim pageText = doc(i).GetText()
Next
' MuPDFCore
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 (no HTML header/footer template in MuPDF)
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<div style='text-align:center;'>Document Title</div>",
MaxHeight = 25
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<div style='text-align:center;'>Page {page} of {total-pages}</div>",
MaxHeight = 25
};// 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 (no HTML header/footer template in MuPDF)
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<div style='text-align:center;'>Document Title</div>",
MaxHeight = 25
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<div style='text-align:center;'>Page {page} of {total-pages}</div>",
MaxHeight = 25
};' 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 (no HTML header/footer template in MuPDF)
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
.HtmlFragment = "<div style='text-align:center;'>Document Title</div>",
.MaxHeight = 25
}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.HtmlFragment = "<div style='text-align:center;'>Page {page} of {total-pages}</div>",
.MaxHeight = 25
}' 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 (no HTML header/footer template in MuPDF)
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
.HtmlFragment = "<div style='text-align:center;'>Document Title</div>",
.MaxHeight = 25
}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.HtmlFragment = "<div style='text-align:center;'>Page {page} of {total-pages}</div>",
.MaxHeight = 25
}故障排除
問題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")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' 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()方法不存在。
解決方案:使用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()' 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:找不到InsertPdf
問題: MuPDF.NET的insert_pdf)在IronPDF中沒有直接等效名稱,並且MuPDFCore完全未暴露高級合併。
解決方案:使用靜態PdfDocument.Merge():
// MuPDF.NET
docA.InsertPdf(docB);
// IronPDF
var merged = PdfDocument.Merge(pdf1, pdf2);// MuPDF.NET
docA.InsertPdf(docB);
// IronPDF
var merged = PdfDocument.Merge(pdf1, pdf2);netnet遷移檢查表
遷移前
- 清點程式碼庫中的所有MuPDF使用情況
- 記錄所有渲染操作(DPI,縮放因子)
- 確認任何PDF建立需求(目前使用外部工具)
- 列出文字提取要求
- 審查原生二進位檔處理的部署腳本
- 獲得IronPDF授權金鑰
套件變更
- 移除
MuPDF.NET包 - 移除
MuPDFCore包 - 移除
MuPDFCore.NativeAssets.*/MuPDF.NativeAssets包 - 安裝
IronPdfNuGet包:dotnet add package IronPdf - 更新命名空間導入
程式碼變更
- 在啟動時加入授權金鑰配置
- 用
Document(MuPDF.NET) /MuPDFDocument(MuPDFCore) - 用
doc.PageCount/document.Pages.Count - 將
pdf.ExtractAllText() - 用
Document.InsertPdf呼叫 - 移除顯式上下文/處置樣板程式碼
- 新增PDF建立程式碼(如需要)(HTML到PDF)
遷移後
- 從專案中移除本機MuPDF二進位檔
- 更新Docker文件以刪除MuPDF安裝
- 刪除平台特定的運行時文件夾
- 運行比較渲染輸出的回歸測試
- 在所有目標平台(Windows, Linux, macOS)上進行測試
- 考慮新增新功能(浮水印,安全性,標題/頁尾)
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


