
Jak obracać tekst w plikach PDF przy użyciu języka C#
Dlaczego warto przejść zMuPDFna IronPDF
Wyzwania związane z MuPDF
MuPDF to wszechstronne narzędzie PDF, ale jego dualna licencja AGPL/komercyjna (od Artifex) oraz projekt ukierunkowany na renderowanie tworzą realne tarcia dla deweloperów .NET budujących komercyjne aplikacje napędzane HTML. Istnieją dwa główne wrappery .NET: MuPDFCore (społeczność, przez arklumpus, AGPL-3.0) i MuPDF.NET (oficjalne powiązania C# Artifex, odzwierciedlające PyMuPDF).
-
Pułapka licencji AGPL: Zarówno MuPDF, jak i wrappery są dostarczane domyślnie pod licencją AGPL-3.0. Dystrybucja produktu zamkniętego na kodzie AGPL wymaga albo otworzenia kodu na AGPL, albo wykupienia licencji komercyjnej od Artifex (cena na żądanie, nie jest publikowana).
-
Skupienie na renderowaniu: Chociaż MuPDF.NET może scalać, zacierać, podpisywać i oznaczać znakami wodnymi (odzwierciedlając PyMuPDF), większość API i narzędzi jest ukierunkowana na przeglądanie i ekstrakcję tekstu strukturalnego, a nie na generowanie dokumentów napędzanych HTML.
-
Brak silnika HTML-do-PDF: Ani MuPDF.NET, ani MuPDFCore nie dostarcza silnika HTML/CSS. Generowanie PDF z HTML wymaga użycia odrębnego silnika przeglądarki (Chromium, wkhtmltopdf, PrinceXML) i tylko ładowania wyniku za pomocą MuPDF.
-
Natywne zależności: Oba wrappery dostarczają pakiety
MuPDFCore.NativeAssets/MuPDF.NativeAssetsz binariami per-RID (win-x64, linux-x64, osx-x64). Wdrożenie międzyplatformowe działa, ale obrazy Docker potrzebują pasującego natywnego pakietu i obrazu bazowego (glibc vs musl). -
Brak znaków wodnych HTML / nagłówków / stopek: Stemplowanie jest oparte na obrazach i tekście na stronę; nie ma
{page}/{total-pages}szablonowego silnika exposowanego przez renderery oparte na Chromium. -
Powierzchnia interoperacyjna C: Wrappery eksponują czas życia kontekstu/dokumentu, które mapują się na bibliotekę C - kolejność usunięcia ma znaczenie, awarie natywne mogą zabić cały proces, a koszt marshalingu nie jest trywialny w przypadku dużych dokumentów.
PorównanieMuPDFi IronPDF
| Funkcja | MuPDF | IronPDF |
|---|---|---|
| Licencja | AGPL-3.0 lub komercyjna na żądanie (Artifex) | Komercyjna z opublikowaną wyceną |
| Główny cel | Renderowanie, tekst strukturalny, narzędzie niskiego poziomu | Generowanie i manipulacja sterowana HTML |
| HTML do PDF | Nie wspierane (brak silnika HTML) | Pełny silnik Chromium |
| Tworzenie plików PDF | Tylko na poziomie strony (wstawianie tekstu/obrazów/wektorów) | HTML, adresy URL, obrazy |
| Manipulacja plikami PDF | Obsługiwane w MuPDF.NET (InsertPdf, Select, DeletePage) | Obsługiwane (Merge, CopyPages, RemovePages) |
| Zależności | NativeMuPDF+ RID NativeAssets | Zarządzana obudowa + wbudowany Chromium |
| Obsługa platform | Win/Linux/macOS za pośrednictwem NativeAssets | Win/Linux/macOS/Docker |
| Obsługa asynchroniczna | Głównie synchroniczne; niektóre asynchroniczne w MuPDFCore | Asynchroniczne interfejsy API w rendererze i dokumencie |
| Integracja z platformą .NET | Wrappery P/Invoke wokół biblioteki C | .NET wrapper + native Chromium |
IronPDF działa jako zarządzana biblioteka .NET w całej gamie nowoczesnych wersji .NET bez złożoności natywnego interopu.
Ocena złożoności migracji
Szacowany nakład pracy według funkcji
| Funkcja | Złożoność migracji |
|---|---|
| Ładowanie dokumentu | Bardzo niski |
| Ekstrakcja tekstu | Bardzo niski |
| Łączenie plików PDF | Niski |
| Renderowanie obrazów | Niski |
| HTML do PDF | N/A (Nowa funkcja) |
| Bezpieczeństwo/Znaki wodne | N/A (Nowa funkcja) |
Zmiana paradygmatu
Podstawową zmianą w migracjiMuPDFjest przejście od przeglądarki służącej wyłącznie do renderowania do kompletnego rozwiązania PDF:
MuPDF.NET: Document → doc[i] → GetText / GetPixmap / InsertPdf
MuPDFCore: MuPDFContext → MuPDFDocument → Pages[i] → Render / SaveImage
IronPDF: PdfDocument.FromFile() → Full manipulation → Create/Edit/Merge/Secure
Zanim zaczniesz
Wymagania wstępne
- Środowisko .NET: .NET Framework 4.6.2+ lub .NET Core 3.1+ / .NET 5/6/7/8/9+
- Dostęp do NuGet: Możliwość instalowania pakietów NuGet
- Licencja IronPDF: Uzyskaj klucz licencyjny na stronie ironpdf.com
Zmiany w pakiecie 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
Należy również usunąć natywne pliki binarne MuPDF z wdrożenia:
- Usuń
mupdf.dll,libmupdf.so,libmupdf.dylib - Usuń foldery specyficzne dla platform (
runtimes/*/native/) - Zaktualizuj pliki Docker, aby usunąć instalację MuPDF
Konfiguracja licencji
// 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"Zidentyfikuj zastosowanie MuPDF
# Find allMuPDFreferences
grep -r "MuPDF\|MuPDFCore\|MuPDFDocument" --include="*.cs" .
Kompletna dokumentacija API
Ładowanie dokumentów
| 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() |
Dostęp do strony
| 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() / iteracja tekstu strukturalnego (MuPDFCore) | page.Text |
Wyodrębnianie tekstu
| MuPDF | IronPDF |
|---|---|
Iteruj po doc[i].GetText() | pdf.ExtractAllText() |
Tworzenie plików PDF (niedostępne w MuPDF)
| MuPDF | IronPDF |
|---|---|
| (brak silnika HTML) | ChromePdfRenderer.RenderHtmlAsPdf(html) |
| (brak silnika HTML) | ChromePdfRenderer.RenderUrlAsPdf(url) |
Manipulacja plikami PDF
| MuPDF | IronPDF |
|---|---|
docA.InsertPdf(docB) (MuPDF.NET) | PdfDocument.Merge(pdf1, pdf2) |
page.InsertImage(...) na stronę (tylko stemple obrazu/tekstu) | pdf.ApplyWatermark(html) |
doc.Save(path, encryption: ..., ownerPw: ..., userPw: ...) | pdf.SecuritySettings |
Przykłady migracji kodu
Przykład 1: Konwersja HTML do PDF (MuPDF nie potrafi tego zrobić)
Przed (MuPDF):
// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
//MuPDFdoesn't support HTML do PDF conversion 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 direct HTML do PDF conversion");
}
}// NuGet: Install-Package MuPDF.NET
using MuPDFCore;
using System.IO;
class Program
{
static void Main()
{
//MuPDFdoesn't support HTML do PDF conversion 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 direct HTML do PDF conversion");
}
}' NuGet: Install-Package MuPDF.NET
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
' MuPDFdoesn'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 -MuPDFis 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' NuGet: Install-Package MuPDF.NET
Imports MuPDFCore
Imports System.IO
Class Program
Shared Sub Main()
' MuPDFdoesn'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 -MuPDFis 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 ClassPo (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 ClassTen przykład podkreśla najważniejsze ograniczenie MuPDF: nie może on w ogóle konwertować HTML na PDF. KodMuPDFjawnie rzuca NotSupportedException, ponieważ konwersja HTML-to-PDF nie jest funkcjonalnością oferowaną przez MuPDF. Jeśli potrzebna była ta funkcjonalność w MuPDF, należało użyć oddzielnej biblioteki, takiej jak wkhtmltopdf lub silnika przeglądarki, a następnie załadować wynikowy plik PDF doMuPDFw celu wyświetlenia.
ChromePdfRenderer od IronPDF używa pełnego silnika Chromium do renderowania HTML z pełnym wsparciem dla CSS3, JavaScript i nowoczesnych standardów webowych. Metoda RenderHtmlAsPdf() akceptuje bezpośrednio ciągi HTML. Zobacz dokumentację HTML do PDF, aby uzyskać informacje o dodatkowych opcjach renderowania, w tym renderowaniu adresów URL i konwersji plików HTML.
Przykład 2: Wyodrębnianie tekstu
Przed (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 ModulePo (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 ModulePodejście MuPDF.NET otwiera Document, iteruje doc.PageCount za pomocą pętli for, wywołuje doc[i].GetText() dla każdej strony, i buduje tekst za pomocą StringBuilder.
IronPDF redukuje to do trzech linii: załaduj dokument za pomocą PdfDocument.FromFile(), wywołaj ExtractAllText(), i wyświetl wynik. Bez ręcznej iteracji, bez StringBuilder. Dowiedz się więcej o wyciąganiu tekstu z plików PDF.
Przykład 3: Łączenie wielu plików PDF
Przed (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 ClassPo (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 wspiera łączenie poprzez Document.InsertPdf, co jest lustrzanym odbiciem insert_pdf() z PyMuPDF i przypina każdą stronę jednego dokumentu do innego w miejscu. MuPDFCore nie ujawnia równoważnego wysokopoziomowego API scalania.
Statyczna metoda PdfDocument.Merge() w IronPDF akceptuje wiele dokumentów PDF i zwraca jeden scalony dokument, nie modyfikując źródeł. Aby scalić wiele dokumentów, przekaż listę: PdfDocument.Merge(pdfList). Dodatkowe opcje można znaleźć w dokumentacji dotyczącej scałania i dzielenia plików PDF.
Ważne uwagi dotyczące migracji
Usuń pliki binarne
MuPDF wymaga bibliotek natywnych dla danej platformy. Po migracji do IronPDF usuń wszystkie natywne pliki binarne 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 to w pełni zarządzany kod .NET - nie ma potrzeby zarządzania natywnymi plikami binarnymi na różnych platformach.
Uproszczony wzorzec Dispose
MuPDFCore wymaga jawnego MuPDFContext; MuPDF.NET ukrywa to, ale nadal potrzebuje 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 pdfZmiana wzorca iteracji strony
MuPDF wykorzystuje iterację opartą na indeksie z jawną liczbą stron:
// 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
NextNowe dostępne funkcje
Po migracji do IronPDF zyskujesz możliwości, których nie zapewnia MuPDF:
// Tworzenie plików 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
};// Tworzenie plików 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
};' Tworzenie plików 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
}' Tworzenie plików 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
}Rozwiązywanie problemów
Problem 1: Nie znaleziono MuPDFDocument
Problem: Klasa MuPDFDocument nie istnieje w IronPDF.
Rozwiązanie: Użyj 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")Problem 2: Nie znaleziono Pages.Count
Problem: Wzorzec document.Pages.Count nie działa.
Rozwiązanie: Użyj 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.PagesProblem 3: Nie znaleziono funkcji GetText()
Problem: Metoda page.GetText() nie istnieje.
Rozwiązanie: Użyj właściwości page.Text lub 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()Problem 4: Nie znaleziono InsertPdf
Problem: Document.InsertPdf w MuPDF.NET (odbicie insert_pdf w PyMuPDF) nie ma bezpośredniego odpowiednika w IronPDF, a MuPDFCore w ogóle nie udostępnia operacji scalania na wysokim poziomie.
Rozwiązanie: Użyj statycznej funkcji 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);netnetLista kontrolna migracji
Przed migracją
- Sporządź spis wszystkich miejsc użyciaMuPDFw kodzie źródłowym
- Dokumentuj wszystkie operacje renderowania (DPI, współczynniki skalowania)
- Określ wszelkie potrzeby związane z tworzeniem plików PDF (obecnie korzystasz z narzędzi zewnętrznych)
- Wymagania dotyczące ekstrakcji tekstu z listy
- Przejrzyj skrypty wdrażania dotyczące obsługi natywnych plików binarnych
- Uzyskaj klucz licencyjny IronPDF
Zmiany w pakiecie
- Usuń pakiet
MuPDF.NET - Usuń pakiet
MuPDFCore - Usuń pakiety
MuPDFCore.NativeAssets.*/MuPDF.NativeAssets - Zainstaluj pakiet NuGet
IronPdf:dotnet add package IronPdf - Zaktualizuj importy przestrzeni nazw
Zmiany w kodzie
- Dodaj konfigurację klucza licencyjnego podczas uruchamiania
- Zastąp
Document(MuPDF.NET) /MuPDFDocument(MuPDFCore) przezPdfDocument.FromFile() - Zastąp
doc.PageCount/document.Pages.Countprzezpdf.PageCount - Zastąp
doc[i].GetText()przezpage.Textlubpdf.ExtractAllText() - Zastąp wywołania
Document.InsertPdfprzezPdfDocument.Merge() - Usuń jawny szablon kontekstu/usuń
- W razie potrzeby dodaj kod do tworzenia plików PDF (HTML do PDF)
Po migracji
- Usuń natywne pliki binarneMuPDFz projektu
- Zaktualizuj pliki Docker, aby usunąć instalację MuPDF
- Usuń foldery środowiska uruchomieniowego specyficzne dla platformy
- Przeprowadź testy regresji, porównując wygenerowane wyniki
- Przetestuj na wszystkich platformach docelowych (Windows, Linux, macOS)
- Rozważ dodanie nowych funkcji (znaki wodne, zabezpieczenia, nagłówki/stopki)
Curtis Chau posiada tytuł licencjata z informatyki (Uniwersytet Carleton) i specjalizuje się w front-endowym rozwoju, z ekspertką w Node.js, TypeScript, JavaScript i React. Pasjonuje się tworzeniem intuicyjnych i estetycznie przyjemnych interfejsów użytkownika, Curtis cieszy się pracą z nowoczesnymi frameworkami i tworzeniem dobrze zorganizowanych, atrakcyjnych wizualnie podręczników.
Powiązane artykuły


