Jak przeprowadzić migrację z PdfiumViewer do IronPDF w języku C#
Migracja zPdfiumViewerdo IronPDF przenosi twój przepływ pracy PDF .NET z wrappera widoku i renderowania Windows Forms wokół PDFium Google — z natywnymi zależnościami binarnymi i tylko ekstrakcją tekstu na poziomie strony — do kompleksowego rozwiązania PDF, które obsługuje tworzenie, manipulację, pełną ekstrakcję tekstu i renderowanie we wszystkich typach aplikacji .NET. Ten przewodnik dostarcza kompletną ścieżkę migracji krok po kroku, która eliminuje ograniczenia platformowe, jednocześnie dodając możliwości, którychPdfiumViewernigdy nie był zaprojektowany, by dostarczać.
Dlaczego warto przejść zPdfiumViewerna IronPDF
Zrozumienie PdfiumViewer
PdfiumViewer (pakiet NuGet PdfiumViewer, repozytorium pvginkel/PdfiumViewer, Apache 2.0) to owinieta dla .NET wokół silnika renderującego PDFium Google'a — tego samego silnika używanego w Chrome — udostępniającego oglądanie i rasteryzację PDF dla aplikacji Windows Forms.
PdfiumViewer to przeglądarka i renderer, a nie generator. Nie obsługuje tworzenia, edytowania ani manipulacji PDF, a jego API tekstu (PdfDocument.GetPdfText(int page)) zwraca surowy tekst strony bez układu ani współrzędnych poszczególnych słów. Repozytorium nadrzędne zostało zarchiwizowane przez jego opiekuna 2 sierpnia 2019, a ostatnia wersja NuGet (2.13.0) została wysłana 6 listopada 2017 — istniejący kod działa, ale nie są planowane żadne dalsze poprawki, aktualizacje .NET 6+ ani poprawki CVE.
Istotne ograniczenia programu PdfiumViewer
-
Tylko przeglądarka / renderer: Nie może tworzyć PDF-ów z HTML, obrazów ani programowo. MożliwościPdfiumViewerograniczają się do oglądania i rasteryzacji — nie obsługuje tworzenia PDF-ów, edycji, łączenia ani innych funkcji manipulacyjnych.
-
Tylko Windows Forms / Windows: Zbudowane wokół
System.Drawingi kontrolek WinForms; nie zaprojektowane dla innych frameworków interfejsu użytkownika, aSystem.Drawing.Commonjest tylko dla Windows na .NET 6+. -
Brak manipulacji PDF: Nie można łączyć, dzielić ani modyfikować zawartości PDF.
-
Natywne zależności binarne: Wymaga binarek PDFium dla specyficznej platformy poprzez
PdfiumViewer.Native.x86.v8-xfaiPdfiumViewer.Native.x86_64.v8-xfa(lub warianty no-V8 / no-XFA). -
Zarchiwizowane repozytorium: Repo zarchiwizowane 2019-08-02; ostatnia wersja NuGet 2.13.0 (2017-11-06); nie ma dalszych aktualizacji.
-
Tylko wyodrębnianie tekstu na poziomie strony:
PdfDocument.GetPdfText(int page)zwraca surowy tekst strony — brak współrzędnych poszczególnych słów, brak rekonstrukcji układu, brak OCR dla PDF-ów zawierających tylko obrazy. -
Brak HTML do PDF: PDFium nie analizuje HTML. Aby wytworzyć PDF z HTML, potrzebujesz oddzielnego generatora (wkhtmltopdf, IronPDF, PuppeteerSharp, itd.) i dopiero potem możesz załadować wynik za pomocąPdfiumViewerdo wyświetlenia lub rasteryzacji.
-
Brak nagłówków/stopek: Nie można dodać numerów stron ani powtarzającej się zawartości.
-
Brak znaków wodnych: Nie można stemplić dokumentów nakładkami.
- Brak funkcji zabezpieczeń: Nie można szyfrować ani chronić PDF-ów hasłem.
PorównaniePdfiumVieweri IronPDF
| Aspekt | PdfiumViewer | IronPDF |
|---|---|---|
| Główny cel | Przeglądarka plików PDF dla WinForms | Kompletne rozwiązanie PDF |
| Licencja | Apache 2.0 | Komercjalne |
| Tworzenie plików PDF | ✗ | ✓(HTML, URL, obrazy) |
| Manipulacja plikami PDF | ✗ | ✓(scal, podziel, edytuj) |
| HTML do PDF | ✗ | ✓(silnik Chromium) |
| Wyodrębnianie tekstu | Na poziomie strony tylko (GetPdfText) |
Pełny (z układem, na stronę) |
| Znaki wodne | ✗ | ✓ |
| Nagłówki/stopki | ✗ | ✓ |
| Bezpieczeństwo | ✗ | ✓ |
| Wbudowana przeglądarka | ✓ | ✗(skupione na backendzie) |
| Obsługa platform | Tylko Windows / WinForms | Konsola, Web, Desktop, międzyplatformowe |
| Obsługa frameworków | .NET Framework 2.0+ | .NET Framework, Core, 5+ |
| Konserwacja | Zarchiwizowane 2019-08-02 (ostatnie wydanie 2017-11-06) | Aktywne |
Dla zespołów celujących w nowoczesne .NET,IronPDF dostarcza kompleksową, aktywnie utrzymywaną podstawę, która działa we wszystkich typach aplikacji .NET, eliminując ograniczenie Windows Forms i złożoność natywnych binariów.
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
# RemovePdfiumViewerpackages
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Native.x86.v8-xfa
dotnet remove package PdfiumViewer.Native.x86_64.v8-xfa
# Install IronPDF
dotnet add package IronPdf
# RemovePdfiumViewerpackages
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Native.x86.v8-xfa
dotnet remove package PdfiumViewer.Native.x86_64.v8-xfa
# Install IronPDF
dotnet add package IronPdf
Konfiguracja licencji
// 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"
Zidentyfikuj zastosowanie PdfiumViewer
# FindPdfiumViewerusage
grep -r "PdfiumViewer\|PdfViewer\|PdfDocument\.Load" --include="*.cs" .
# Find native binary references
grep -r "pdfium\.dll\|Native\.x86\|Native\.x64" --include="*.csproj" .
# Find viewer control usage
grep -r "PdfViewer" --include="*.cs" --include="*.Designer.cs" .
# FindPdfiumViewerusage
grep -r "PdfiumViewer\|PdfViewer\|PdfDocument\.Load" --include="*.cs" .
# Find native binary references
grep -r "pdfium\.dll\|Native\.x86\|Native\.x64" --include="*.csproj" .
# Find viewer control usage
grep -r "PdfViewer" --include="*.cs" --include="*.Designer.cs" .
Kompletna dokumentacija API
Zmiany w przestrzeni nazw
// PdfiumViewer
using PdfiumViewer;
// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
// PdfiumViewer
using PdfiumViewer;
// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
Imports PdfiumViewer
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Editing
Mapowania klas podstawowych
| PdfiumViewer | IronPDF |
|---|---|
PdfDocument |
PdfDocument |
PdfViewer |
(brak odpowiednika) |
PdfRenderer |
ChromePdfRenderer |
| (niedostępne) | HtmlHeaderFooter |
Mapowania ładowania dokumentów
| PdfiumViewer | IronPDF |
|---|---|
PdfDocument.Load(path) |
PdfDocument.FromFile(path) |
PdfDocument.Load(stream) |
PdfDocument.FromStream(stream) |
PdfDocument.Load(path, password) |
PdfDocument.FromFile(path, password) |
| (brak przeciążenia bajtów — opakuj w MemoryStream) | PdfDocument.FromBinaryData(bytes) |
Mapowanie właściwości dokumentu
| PdfiumViewer | IronPDF |
|---|---|
document.PageCount |
document.PageCount |
document.PageSizes[i] |
document.Pages[i].Width/Height |
document.GetPdfText(pageIndex) |
pdf.Pages[pageIndex].Text |
Mapowania renderowania stron
| PdfiumViewer | IronPDF |
|---|---|
document.Render(pageIndex, dpiX, dpiY, forPrinting) |
pdf.ToBitmap(pageIndex) |
document.Render(pageIndex, width, height, dpiX, dpiY, flags) |
pdf.RasterizeToImageFiles(path, dpi) |
Zapisywanie mapowań dokumentów
| PdfiumViewer | IronPDF |
|---|---|
document.Save(path) |
pdf.SaveAs(path) |
document.Save(stream) |
new MemoryStream(pdf.BinaryData) |
| (niedostępne) | pdf.BinaryData (zwraca byte[]) |
Nowe lub znacząco rozszerzone funkcje w porównaniu do PdfiumViewer
| Funkcja IronPDF | Opis |
|---|---|
pdf.ExtractAllText() |
Wyodrębnianie tekstu całego dokumentu (PdfiumViewer oferuje tylko wyodrębnianie na poziomie stron GetPdfText) |
pdf.ExtractTextFromPage(index) |
Wyodrębnij tekst z określonej strony |
ChromePdfRenderer.RenderHtmlAsPdf() |
Utwórz z HTML |
ChromePdfRenderer.RenderUrlAsPdf() |
Utwórz z adresu URL |
PdfDocument.Merge() |
Łączenie plików PDF |
pdf.CopyPages() |
Wyodrębnij strony |
pdf.RemovePages() |
Usuń strony |
pdf.ApplyWatermark() |
Dodaj znaki wodne |
pdf.AddHtmlHeaders() |
Dodaj nagłówki |
pdf.AddHtmlFooters() |
Dodaj stopki |
pdf.SecuritySettings |
Ochrona hasłem |
pdf.Form |
Wypełnianie formularzy |
Przykłady migracji kodu
Przykład 1: Pobieranie tekstu z pliku PDF
Przed (PdfiumViewer):
// NuGet: Install-Package PdfiumViewer
// Install-Package PdfiumViewer.Native.x86.v8-xfa
// Install-Package PdfiumViewer.Native.x86_64.v8-xfa
using PdfiumViewer;
using System;
using System.Text;
string pdfPath = "document.pdf";
//PdfiumViewerexposes page-level text extraction via
// PdfDocument.GetPdfText(int page). It returns raw page text only —
// no layout, no per-word coordinates, no OCR for image-only pages.
using (var document = PdfDocument.Load(pdfPath))
{
int pageCount = document.PageCount;
Console.WriteLine($"Total pages: {pageCount}");
var sb = new StringBuilder();
for (int i = 0; i < pageCount; i++)
{
string pageText = document.GetPdfText(i);
sb.AppendLine($"--- Page {i + 1} ---");
sb.AppendLine(pageText);
}
Console.WriteLine(sb.ToString());
}
// NuGet: Install-Package PdfiumViewer
// Install-Package PdfiumViewer.Native.x86.v8-xfa
// Install-Package PdfiumViewer.Native.x86_64.v8-xfa
using PdfiumViewer;
using System;
using System.Text;
string pdfPath = "document.pdf";
//PdfiumViewerexposes page-level text extraction via
// PdfDocument.GetPdfText(int page). It returns raw page text only —
// no layout, no per-word coordinates, no OCR for image-only pages.
using (var document = PdfDocument.Load(pdfPath))
{
int pageCount = document.PageCount;
Console.WriteLine($"Total pages: {pageCount}");
var sb = new StringBuilder();
for (int i = 0; i < pageCount; i++)
{
string pageText = document.GetPdfText(i);
sb.AppendLine($"--- Page {i + 1} ---");
sb.AppendLine(pageText);
}
Console.WriteLine(sb.ToString());
}
Imports PdfiumViewer
Imports System
Imports System.Text
Dim pdfPath As String = "document.pdf"
' PdfiumViewer exposes page-level text extraction via
' PdfDocument.GetPdfText(Integer page). It returns raw page text only —
' no layout, no per-word coordinates, no OCR for image-only pages.
Using document As PdfDocument = PdfDocument.Load(pdfPath)
Dim pageCount As Integer = document.PageCount
Console.WriteLine($"Total pages: {pageCount}")
Dim sb As New StringBuilder()
For i As Integer = 0 To pageCount - 1
Dim pageText As String = document.GetPdfText(i)
sb.AppendLine($"--- Page {i + 1} ---")
sb.AppendLine(pageText)
Next
Console.WriteLine(sb.ToString())
End Using
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
string pdfPath = "document.pdf";
// Open and extract text from PDF
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Extract text from all pages
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted Text:");
Console.WriteLine(allText);
// Extract text from specific page
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine($"\nFirst page text: {pageText}");
Console.WriteLine($"\nTotal pages: {pdf.PageCount}");
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
string pdfPath = "document.pdf";
// Open and extract text from PDF
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Extract text from all pages
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted Text:");
Console.WriteLine(allText);
// Extract text from specific page
string pageText = pdf.ExtractTextFromPage(0);
Console.WriteLine($"\nFirst page text: {pageText}");
Console.WriteLine($"\nTotal pages: {pdf.PageCount}");
Imports IronPdf
Imports System
Module Program
Sub Main()
Dim pdfPath As String = "document.pdf"
' Open and extract text from PDF
Dim pdf As PdfDocument = PdfDocument.FromFile(pdfPath)
' Extract text from all pages
Dim allText As String = pdf.ExtractAllText()
Console.WriteLine("Extracted Text:")
Console.WriteLine(allText)
' Extract text from specific page
Dim pageText As String = pdf.ExtractTextFromPage(0)
Console.WriteLine(vbCrLf & "First page text: " & pageText)
Console.WriteLine(vbCrLf & "Total pages: " & pdf.PageCount)
End Sub
End Module
API tekstuPdfiumViewerjest PdfDocument.GetPdfText(int page) — tylko surowy tekst na poziomie strony, brak rekonstrukcji układu, brak współrzędnych poszczególnych słów, brak wsparcia dla OCR dla PDF-ów zawierających tylko obrazy. Cokolwiek poza płaskim zrzutem tekstu wymaga albo niestandardowych heurystyk układu, albo innej biblioteki.
IronPDF zapewnia wyodrębnianie całego dokumentu poprzez ExtractAllText() oraz wyodrębnianie dla każdej strony poprzez ExtractTextFromPage(index) (lub pdf.Pages[index].Text). Dodatkowe opcje można znaleźć w dokumentacji dotyczącej ekstrakcji tekstu.
Przykład 2: Konwersja HTML do PDF
Przed (PdfiumViewer):
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
//PdfiumViewerwraps Google's PDFium — a renderer, not a generator.
// PDFium does not parse HTML, soPdfiumViewerhas no HTML-to-PDF path.
// To produce a PDF from HTML you need a separate generator (wkhtmltopdf,
// IronPDF, PuppeteerSharp, etc.), then load the result with PdfiumViewer
// for display or rasterization.
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// Pretend "output.pdf" was produced by another tool — load and render it.
string existingPdfPath = "output.pdf";
using (var document = PdfDocument.Load(existingPdfPath))
{
Console.WriteLine($"Loaded PDF with {document.PageCount} page(s).");
// PdfDocument.Render(int page, float dpiX, float dpiY, bool forPrinting)
var image = document.Render(0, 300, 300, true);
Console.WriteLine("Rendered page 1 at 300 DPI.");
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
//PdfiumViewerwraps Google's PDFium — a renderer, not a generator.
// PDFium does not parse HTML, soPdfiumViewerhas no HTML-to-PDF path.
// To produce a PDF from HTML you need a separate generator (wkhtmltopdf,
// IronPDF, PuppeteerSharp, etc.), then load the result with PdfiumViewer
// for display or rasterization.
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// Pretend "output.pdf" was produced by another tool — load and render it.
string existingPdfPath = "output.pdf";
using (var document = PdfDocument.Load(existingPdfPath))
{
Console.WriteLine($"Loaded PDF with {document.PageCount} page(s).");
// PdfDocument.Render(int page, float dpiX, float dpiY, bool forPrinting)
var image = document.Render(0, 300, 300, true);
Console.WriteLine("Rendered page 1 at 300 DPI.");
}
Imports PdfiumViewer
Imports System
' PdfiumViewer wraps Google's PDFium — a renderer, not a generator.
' PDFium does not parse HTML, so PdfiumViewer has no HTML-to-PDF path.
' To produce a PDF from HTML you need a separate generator (wkhtmltopdf,
' IronPDF, PuppeteerSharp, etc.), then load the result with PdfiumViewer
' for display or rasterization.
Dim htmlContent As String = "<h1>Hello World</h1><p>This is a test document.</p>"
' Pretend "output.pdf" was produced by another tool — load and render it.
Dim existingPdfPath As String = "output.pdf"
Using document As PdfDocument = PdfDocument.Load(existingPdfPath)
Console.WriteLine($"Loaded PDF with {document.PageCount} page(s).")
' PdfDocument.Render(int page, float dpiX, float dpiY, bool forPrinting)
Dim image = document.Render(0, 300, 300, True)
Console.WriteLine("Rendered page 1 at 300 DPI.")
End Using
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully!");
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully!");
Imports IronPdf
Imports System
Module Program
Sub Main()
Dim htmlContent As String = "<h1>Hello World</h1><p>This is a test document.</p>"
' Create a PDF from HTML string
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully!")
End Sub
End Module
To jest największa luka funkcji. PDFium nie analizuje HTML, więcPdfiumViewernie ma ścieżki HTML do PDF — musisz najpierw wygenerować PDF za pomocą innego narzędzia, a następnie użyćPdfiumViewerdo wyświetlenia lub rasteryzacji.
IronPDF zapewnia natywną konwersję HTML do PDF poprzez ChromePdfRenderer, który wewnętrznie używa silnika Chromium do renderowania HTML, CSS i JavaScript. RenderHtmlAsPdf() przekształca ciągi HTML, RenderUrlAsPdf() przechwytuje URL-e, a RenderHtmlFileAsPdf() odczytuje pliki HTML. Kompleksowe przykłady można znaleźć w dokumentacji dotyczącej konwersji HTML do PDF.
Przykład 3: Konwersja plików PDF na obrazy
Przed (PdfiumViewer):
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.Drawing;
using System.Drawing.Imaging;
string pdfPath = "document.pdf";
string outputImage = "page1.png";
// PDFiumViewer excels at rendering PDFs to images
using (var document = PdfDocument.Load(pdfPath))
{
// Render first page at 300 DPI
int dpi = 300;
using (var image = document.Render(0, dpi, dpi, true))
{
// Save as PNG
image.Save(outputImage, ImageFormat.Png);
Console.WriteLine($"Page rendered to {outputImage}");
}
// Render all pages
for (int i = 0; i < document.PageCount; i++)
{
using (var pageImage = document.Render(i, 150, 150, true))
{
pageImage.Save($"page_{i + 1}.png", ImageFormat.Png);
}
}
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.Drawing;
using System.Drawing.Imaging;
string pdfPath = "document.pdf";
string outputImage = "page1.png";
// PDFiumViewer excels at rendering PDFs to images
using (var document = PdfDocument.Load(pdfPath))
{
// Render first page at 300 DPI
int dpi = 300;
using (var image = document.Render(0, dpi, dpi, true))
{
// Save as PNG
image.Save(outputImage, ImageFormat.Png);
Console.WriteLine($"Page rendered to {outputImage}");
}
// Render all pages
for (int i = 0; i < document.PageCount; i++)
{
using (var pageImage = document.Render(i, 150, 150, true))
{
pageImage.Save($"page_{i + 1}.png", ImageFormat.Png);
}
}
}
Imports PdfiumViewer
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Module Module1
Sub Main()
Dim pdfPath As String = "document.pdf"
Dim outputImage As String = "page1.png"
' PDFiumViewer excels at rendering PDFs to images
Using document = PdfDocument.Load(pdfPath)
' Render first page at 300 DPI
Dim dpi As Integer = 300
Using image = document.Render(0, dpi, dpi, True)
' Save as PNG
image.Save(outputImage, ImageFormat.Png)
Console.WriteLine($"Page rendered to {outputImage}")
End Using
' Render all pages
For i As Integer = 0 To document.PageCount - 1
Using pageImage = document.Render(i, 150, 150, True)
pageImage.Save($"page_{i + 1}.png", ImageFormat.Png)
End Using
Next
End Using
End Sub
End Module
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Linq;
string pdfPath = "document.pdf";
string outputImage = "page1.png";
// Open PDF and convert to images
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Convert first page to image
var firstPageImage = pdf.ToBitmap(0);
firstPageImage[0].Save(outputImage);
Console.WriteLine($"Page rendered to {outputImage}");
// Convert all pages to images
var allPageImages = pdf.ToBitmap();
for (int i = 0; i < allPageImages.Length; i++)
{
allPageImages[i].Save($"page_{i + 1}.png");
Console.WriteLine($"Saved page {i + 1}");
}
Console.WriteLine($"Total pages converted: {pdf.PageCount}");
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Linq;
string pdfPath = "document.pdf";
string outputImage = "page1.png";
// Open PDF and convert to images
PdfDocument pdf = PdfDocument.FromFile(pdfPath);
// Convert first page to image
var firstPageImage = pdf.ToBitmap(0);
firstPageImage[0].Save(outputImage);
Console.WriteLine($"Page rendered to {outputImage}");
// Convert all pages to images
var allPageImages = pdf.ToBitmap();
for (int i = 0; i < allPageImages.Length; i++)
{
allPageImages[i].Save($"page_{i + 1}.png");
Console.WriteLine($"Saved page {i + 1}");
}
Console.WriteLine($"Total pages converted: {pdf.PageCount}");
Imports IronPdf
Imports System
Imports System.Linq
Module Module1
Sub Main()
Dim pdfPath As String = "document.pdf"
Dim outputImage As String = "page1.png"
' Open PDF and convert to images
Dim pdf As PdfDocument = PdfDocument.FromFile(pdfPath)
' Convert first page to image
Dim firstPageImage = pdf.ToBitmap(0)
firstPageImage(0).Save(outputImage)
Console.WriteLine($"Page rendered to {outputImage}")
' Convert all pages to images
Dim allPageImages = pdf.ToBitmap()
For i As Integer = 0 To allPageImages.Length - 1
allPageImages(i).Save($"page_{i + 1}.png")
Console.WriteLine($"Saved page {i + 1}")
Next
Console.WriteLine($"Total pages converted: {pdf.PageCount}")
End Sub
End Module
Jest to obszar, w którymPdfiumViewerwyróżnia się na tle innych — renderowanie plików PDF do obrazów jest jego główną zaletą. Obie biblioteki skutecznie radzą sobie z tym zadaniem, ale w różny sposób.
PdfiumViewer używa document.Render(pageIndex, dpiX, dpiY, forPrinting) z zagnieżdżonymi instrukcjami using do prawidłowego zarządzania pamięcią. Trzeba zaimportować System.Drawing i System.Drawing.Imaging dla wyliczenia ImageFormat.
IronPDF używa pdf.ToBitmap(pageIndex), który zwraca tablicę bitmap. Wzorzec jest prostszy bez zagnieżdżonych wymagań dotyczących usuwania. Dla operacji wsadowych, ToBitmap() bez parametrów zwraca wszystkie strony. Możesz również użyć RasterizeToImageFiles() do bezpośredniego zapisu pliku. Dodatkowe opcje renderowania można znaleźć w dokumentacji dotyczącej konwersji plików PDF na obrazy.
Usuwanie zależności natywnych
Jedną z istotnych korzyści wynikających z migracji zPdfiumViewerdo IronPDF jest wyeliminowanie zarządzania natywnymi plikami binarnymi.
Przed (PdfiumViewer) – Złożone wdrożenie
MyApp/
├── bin/
│ ├── MyApp.dll
│ ├── PdfiumViewer.dll
│ ├── x86/
│ │ └── pdfium.dll
│ └── x64/
│ └── pdfium.dll
Po (IronPDF) - Czyste wdrożenie
MyApp/
├── bin/
│ ├── MyApp.dll
│ └── IronPdf.dll # Wszystko w zestawie
Usuń odniesienia do plików binarnych
# Delete native PDFium binaries
rm -rf x86/x64/runtimes/
# Remove from .csproj native package references
# <PackageReference Include="PdfiumViewer.Native.x86.v8-xfa" />
# <PackageReference Include="PdfiumViewer.Native.x64.v8-xfa" />
# Delete native PDFium binaries
rm -rf x86/x64/runtimes/
# Remove from .csproj native package references
# <PackageReference Include="PdfiumViewer.Native.x86.v8-xfa" />
# <PackageReference Include="PdfiumViewer.Native.x64.v8-xfa" />
Ważne uwagi dotyczące migracji
Brak wbudowanego kontrolki przeglądarki
IronPDF koncentruje się na zapleczu i nie zawiera wizualnego kontrolki przeglądarki plików PDF:
// PdfiumViewer: Built-in viewer control
pdfViewer.Document = document;
// IronPDF: Use external viewer or web-based approach
pdf.SaveAs(tempPath);
Process.Start(new ProcessStartInfo(tempPath) { UseShellExecute = true });
// PdfiumViewer: Built-in viewer control
pdfViewer.Document = document;
// IronPDF: Use external viewer or web-based approach
pdf.SaveAs(tempPath);
Process.Start(new ProcessStartInfo(tempPath) { UseShellExecute = true });
' PdfiumViewer: Built-in viewer control
pdfViewer.Document = document
' IronPDF: Use external viewer or web-based approach
pdf.SaveAs(tempPath)
Process.Start(New ProcessStartInfo(tempPath) With {.UseShellExecute = True})
W przypadku potrzeb związanych z oglądaniem, rozważ użycie Process.Start() do otwierania w domyślnym przeglądarce PDF, kontrolki WebBrowser z podaną ścieżką do PDF, lub zewnętrznych kontrolek przeglądarki takich jak Syncfusion, DevExpress lub Telerik.
Zmiana metody ładowania dokumentów
// PdfiumViewer
PdfDocument.Load(path)
// IronPDF
PdfDocument.FromFile(path)
// PdfiumViewer
PdfDocument.Load(path)
// IronPDF
PdfDocument.FromFile(path)
' PdfiumViewer
PdfDocument.Load(path)
' IronPDF
PdfDocument.FromFile(path)
Zmiana metody zapisywania
// PdfiumViewer
document.Save(path)
// IronPDF
pdf.SaveAs(path)
// PdfiumViewer
document.Save(path)
// IronPDF
pdf.SaveAs(path)
Zmiana metody renderowania
// PdfiumViewer: Returns image with nested using
using (var image = document.Render(0, 150, 150, true))
{
image.Save("page.png", ImageFormat.Png);
}
// IronPDF: Returns bitmap array
var images = pdf.ToBitmap(0);
images[0].Save("page.png");
// PdfiumViewer: Returns image with nested using
using (var image = document.Render(0, 150, 150, true))
{
image.Save("page.png", ImageFormat.Png);
}
// IronPDF: Returns bitmap array
var images = pdf.ToBitmap(0);
images[0].Save("page.png");
Imports System.Drawing.Imaging
' PdfiumViewer: Returns image with nested using
Using image = document.Render(0, 150, 150, True)
image.Save("page.png", ImageFormat.Png)
End Using
' IronPDF: Returns bitmap array
Dim images = pdf.ToBitmap(0)
images(0).Save("page.png")
Zmiana dostępu do rozmiaru strony
// PdfiumViewer
var size = document.PageSizes[index];
Console.WriteLine($"{size.Width} x {size.Height}");
// IronPDF
var page = pdf.Pages[index];
Console.WriteLine($"{page.Width} x {page.Height}");
// PdfiumViewer
var size = document.PageSizes[index];
Console.WriteLine($"{size.Width} x {size.Height}");
// IronPDF
var page = pdf.Pages[index];
Console.WriteLine($"{page.Width} x {page.Height}");
' PdfiumViewer
Dim size = document.PageSizes(index)
Console.WriteLine($"{size.Width} x {size.Height}")
' IronPDF
Dim page = pdf.Pages(index)
Console.WriteLine($"{page.Width} x {page.Height}")
Podsumowanie porównania funkcji
| Funkcja | PdfiumViewer | IronPDF |
|---|---|---|
| Pobierz plik PDF | ✓ | ✓ |
| Renderuj do obrazu | ✓ | ✓ |
| Wbudowana przeglądarka | ✓ | ✗ |
| PRINT PDF | ✓ | ✓ |
| Wyodrębnij tekst | Na poziomie strony (GetPdfText) |
Pełny (ExtractAllText, na każdej stronie) |
| Utwórz z HTML | ✗ | ✓ |
| Utwórz z adresu URL | ✗ | ✓ |
| Łączenie plików PDF | ✗ | ✓ |
| Podział plików PDF | ✗ | ✓ |
| Dodaj znaki wodne | ✗ | ✓ |
| Nagłówki/stopki | ✗ | ✓ |
| Wypełnianie formularzy | ✗ | ✓ |
| Ochrona hasłem | ✗ | ✓ |
| Obsługa WinForms | ✓ | ✓ |
| Obsługa ASP.NET | ✗ | ✓ |
| Obsługa .NET Core | Ograniczone (System.Drawing związane z Windows) | ✓ |
| Aktywna konserwacja | Zarchiwizowane 2019-08-02 | ✓ |
Lista kontrolna migracji
Przed migracją
- Zidentyfikuj wszystkie przypadki użyciaPdfiumViewerw kodzie źródłowym
- Lista WinForms wykorzystujących kontrolkę PdfViewer
- Dokumentuj aktualne ustawienia DPI renderowania
- Sprawdź, czy nie ma odniesień do plików binarnych
- Określ wykorzystanie funkcji drukowania
- Strategia wymiany kontrolki przeglądarki planów
- Uzyskaj klucz licencyjny IronPDF
Zmiany w pakiecie
- Usuń pakiet NuGet
PdfiumViewer - Usuń pakiet
PdfiumViewer.Native.x86.v8-xfa - Usuń pakiet
PdfiumViewer.Native.x86_64.v8-xfa - Usuń natywne pliki binarne pdfium.dll z folderów x86/i x64/
- Zainstaluj pakiet NuGet:
IronPdf:dotnet add package IronPdf
Zmiany w kodzie
- Dodaj konfigurację klucza licencyjnego podczas uruchamiania
- Zastąp
PdfDocument.Load()przezPdfDocument.FromFile() - Zastąp
document.Save()przezpdf.SaveAs() - Zastąp
document.Render()zpdf.ToBitmap()lubRasterizeToImageFiles() - Zastąp
document.PageSizes[i]zpdf.Pages[i].Width/Height - Zastąp kontrolkę PdfViewer zewnętrzną przeglądarką lub Process.Start()
- Dodaj nowe funkcje (wyodrębnianie tekstu, konwersja HTML do PDF itp.)
Po migracji
- Sprawdź jakość renderowania
- Funkcja drukowania testowego
- Testy na platformach docelowych
- Dodaj nowe funkcje (HTML do PDF, scałanie, znaki wodne, zabezpieczenia)
- Aktualizacja dokumentacji

