如何在 C# 中從 PdfiumViewer 轉移到 IronPDF
從PdfiumViewer轉換到 IronPDF,可將您的 .NET PDF 工作流程從一個具有本機二進位依賴關係的 Windows Forms 檢視專用函式庫,轉換為一個全面的 PDF 解決方案,可在所有 .NET 應用程式類型中處理建立、處理、文字萃取和渲染。本指南提供了一個完整、逐步的遷移路徑,可消除平台限制,同時增加PdfiumViewer無法提供的功能。
為何要從PdfiumViewer轉移到 IronPDF?
瞭解 PdfiumViewer
PdfiumViewer 是 PDFium 的 .NET wrapper,PDFium 是 Google 在 Chrome 瀏覽器中使用的 PDF 渲染引擎。 它提供了一個簡單但強大的解決方案,可將 PDF 檢視直接整合到 Windows Forms 應用程式中,提供高效能、高逼真度的 PDF 呈現能力。
然而,重要的是要記住PdfiumViewer只是一個檢視器。 它不支援 PDF 的建立、編輯或處理,這可能會限制對檢視功能有更多需求的應用程式。 此外,其不確定的維護狀態也為生產應用程式帶來風險 - 其持續開發與維護存在一定的不確定性,這可能是長期專案的顧慮。
PdfiumViewer的關鍵限制
1.僅查看功能:無法從 HTML、圖像或透過程式設計方式建立 PDF。PdfiumViewer的功能僅限於檢視 PDF - 與IronPDF等函式庫不同,它不支援 PDF 的建立、編輯、合併或其他操作功能。
- Windows Forms 特有:該程式庫專注於 Windows Forms 應用程序,不提供對其他使用者介面框架的支援。
3.不支援 PDF 操作:無法合併、拆分或修改 PDF 內容。
4.本機二進位依賴項:需要特定於平台的 PDFium 二進位(x86 和 x64 pdfium.dll 檔案)。
5.維護不確定:更新有限,長期支援不明確。
6.無文字擷取:PdfiumViewer沒有內建的文字擷取功能-您需要使用 OCR 或其他函式庫。 它只能將頁面呈現為影像。
7.不支援 HTML 轉 PDF:PdfiumViewer主要是 PDF 檢視器/渲染器,而不是生成器。 無法直接將 HTML 轉換成 PDF。 您需要使用 wkhtmltopdf 或類似的其他函式庫。
8.無頁首/頁尾:無法新增頁碼或重複內容。
9.無浮水印:無法在有疊加層的文件上加蓋水印。
10.無安全功能:無法加密或密碼保護 PDF 檔案。
PdfiumViewer與IronPDF的比較
| 範疇 | PdfiumViewer | IronPDF |
|---|---|---|
| 主要焦點 | WinForms PDF 檢視器 | 完整的 PDF 解決方案 |
| 執照 | Apache 2.0 | 商業的 |
| PDF製作 | ✗ | ✓(HTML、URL、圖片) |
| PDF 操作 | ✗ | ✓(合併、分割、編輯) |
| HTML至PDF | ✗ | ✓(Chromium 引擎) |
| 文字萃取 | ✗ | ✓ |
| 水印。 | ✗ | ✓ |
| 頁首/頁腳 | ✗ | ✓ |
| 安全性 | ✗ | ✓ |
| 內建檢視器 | ✓ | ✗(注重後端) |
| 平台支援 | 僅限 Windows Forms | 控制台、Web、桌面 |
| 框架支援 | .NET Framework | .NET Framework, Core, 5+。 |
| 維護 | 不確定 | 積極的 |
對於計劃在 2025 年和 2026 年之前採用 .NET 10 和 C# 14 的團隊而言,IronPDF for .NET 提供了一個全面的、積極維護的基礎,可在所有 .NET 應用程式類型中運作,消除了 Windows Forms 限制和本機二進位的複雜性。
開始之前
先決條件
- .NET環境:.NET Framework4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取權限:能夠安裝NuGet套件
- IronPDF許可證:請從IronPDF取得您的許可證密鑰。
NuGet 套件變更
# RemovePdfiumViewerpackages
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Native.x86.v8-xfa
dotnet remove package PdfiumViewer.Native.x64.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.x64.v8-xfa
# Install IronPDF
dotnet add package IronPdf
授權組態
// 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"
辨識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" .
完整的 API 參考資料
命名空間變更
// 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
核心類對應
| PdfiumViewer | IronPDF |
|---|---|
PdfDocument |
PdfDocument |
PdfViewer |
(無對應詞) |
PdfRenderer |
ChromePdfRenderer |
| (不可用) | HtmlHeaderFooter |
文件載入映射
| PdfiumViewer | IronPDF |
|---|---|
PdfDocument.Load(path) |
PdfDocument.FromFile(path) |
PdfDocument.Load(stream) |
PdfDocument.FromStream(stream) |
PdfDocument.Load(bytes) |
PdfDocument.FromBinaryData(bytes) |
文件屬性對應
| PdfiumViewer | IronPDF |
|---|---|
document.PageCount |
document.PageCount |
document.PageSizes |
document.Pages[i].Width/Height |
document.GetPageSize(index) |
document.Pages[index].Width/Height |
頁面渲染對應
| PdfiumViewer | IronPDF |
|---|---|
document.Render(pageIndex, dpiX, dpiY, forPrinting) |
pdf.ToBitmap(pageIndex) |
document.Render(pageIndex, width, height, dpiX, dpiY, flags) |
pdf.RasterizeToImageFiles(path, dpi) |
儲存文件映射
| PdfiumViewer | IronPDF |
|---|---|
document.Save(path) |
pdf.SaveAs(path) |
document.Save(stream) |
pdf.Stream |
| (不可用) | pdf.BinaryData |
PdfiumViewer中沒有的新功能
| IronPDF 特點 | 說明 |
|---|---|
pdf.ExtractAllText() |
從所有頁面中擷取文字 |
pdf.ExtractTextFromPage(index) |
從特定頁面擷取文字 |
ChromePdfRenderer.RenderHtmlAsPdf() |
從 HTML 建立 |
ChromePdfRenderer.RenderUrlAsPdf() |
從 URL 建立 |
PdfDocument.Merge() |
結合 PDF |
pdf.CopyPages() |
摘錄頁面 |
pdf.RemovePages() |
刪除頁面 |
pdf.ApplyWatermark() |
加入水印 |
pdf.AddHtmlHeaders() |
新增標頭 |
pdf.AddHtmlFooters() |
新增頁腳 |
pdf.SecuritySettings |
密碼保護 |
pdf.Form |
表格填寫 |
程式碼遷移範例
範例 1:從 PDF 擷取文字
之前 (PdfiumViewer):
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.Text;
string pdfPath = "document.pdf";
// PDFiumViewer has limited text extraction capabilities
// It's primarily designed for rendering, not text extraction
using (var document = PdfDocument.Load(pdfPath))
{
int pageCount = document.PageCount;
Console.WriteLine($"Total pages: {pageCount}");
// PDFiumViewer does not have built-in text extraction
// You would need to use OCR or another library
// It can only render pages as images
for (int i = 0; i < pageCount; i++)
{
var pageImage = document.Render(i, 96, 96, false);
Console.WriteLine($"Rendered page {i + 1}");
}
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.Text;
string pdfPath = "document.pdf";
// PDFiumViewer has limited text extraction capabilities
// It's primarily designed for rendering, not text extraction
using (var document = PdfDocument.Load(pdfPath))
{
int pageCount = document.PageCount;
Console.WriteLine($"Total pages: {pageCount}");
// PDFiumViewer does not have built-in text extraction
// You would need to use OCR or another library
// It can only render pages as images
for (int i = 0; i < pageCount; i++)
{
var pageImage = document.Render(i, 96, 96, false);
Console.WriteLine($"Rendered page {i + 1}");
}
}
Imports PdfiumViewer
Imports System
Imports System.Text
Dim pdfPath As String = "document.pdf"
' PDFiumViewer has limited text extraction capabilities
' It's primarily designed for rendering, not text extraction
Using document = PdfDocument.Load(pdfPath)
Dim pageCount As Integer = document.PageCount
Console.WriteLine($"Total pages: {pageCount}")
' PDFiumViewer does not have built-in text extraction
' You would need to use OCR or another library
' It can only render pages as images
For i As Integer = 0 To pageCount - 1
Dim pageImage = document.Render(i, 96, 96, False)
Console.WriteLine($"Rendered page {i + 1}")
Next
End Using
After (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
這個範例突顯了一個基本的能力差距。PdfiumViewer 明確指出它"沒有內建文字擷取功能",而且"您需要使用 OCR 或其他函式庫"- 它只能將頁面呈現為影像。 如果您需要使用PdfiumViewer從 PDF 中取得文字,您必須渲染為影像並執行 OCR。
IronPDF 提供原生文本提取功能,使用 ExtractAllText() 提取整個文件的文本,或使用 ExtractTextFromPage(index) 提取特定頁面的文本。 無需 OCR、無需變通 - 直接從 PDF 內容擷取文字。 有關其他選項,請參閱文字擷取文件。
範例 2:HTML 到 PDF 的轉換
之前 (PdfiumViewer):
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System.IO;
using System.Drawing.Printing;
// PDFiumViewer is primarily a PDF viewer/renderer, not a generator
// It cannot directly convert HTML to PDF
// You would need to use another library to first create the PDF
// Then use PDFiumViewer to display it:
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// This functionality is NOT available in PDFiumViewer
// You would need a different library like wkhtmltopdf or similar
// PDFiumViewer can only open and display existing PDFs:
string existingPdfPath = "output.pdf";
using (var document = PdfDocument.Load(existingPdfPath))
{
// Can only render/display existing PDF
var image = document.Render(0, 300, 300, true);
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System.IO;
using System.Drawing.Printing;
// PDFiumViewer is primarily a PDF viewer/renderer, not a generator
// It cannot directly convert HTML to PDF
// You would need to use another library to first create the PDF
// Then use PDFiumViewer to display it:
string htmlContent = "<h1>Hello World</h1><p>This is a test document.</p>";
// This functionality is NOT available in PDFiumViewer
// You would need a different library like wkhtmltopdf or similar
// PDFiumViewer can only open and display existing PDFs:
string existingPdfPath = "output.pdf";
using (var document = PdfDocument.Load(existingPdfPath))
{
// Can only render/display existing PDF
var image = document.Render(0, 300, 300, true);
}
Imports PdfiumViewer
Imports System.IO
Imports System.Drawing.Printing
' PDFiumViewer is primarily a PDF viewer/renderer, not a generator
' It cannot directly convert HTML to PDF
' You would need to use another library to first create the PDF
' Then use PDFiumViewer to display it:
Dim htmlContent As String = "<h1>Hello World</h1><p>This is a test document.</p>"
' This functionality is NOT available in PDFiumViewer
' You would need a different library like wkhtmltopdf or similar
' PDFiumViewer can only open and display existing PDFs:
Dim existingPdfPath As String = "output.pdf"
Using document = PdfDocument.Load(existingPdfPath)
' Can only render/display existing PDF
Dim image = document.Render(0, 300, 300, True)
End Using
After (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
本範例展示了兩個函式庫最顯著的功能差異。PdfiumViewer明確指出"PDFiumViewer 並無此功能"以及"您需要不同的函式庫,例如 wkhtmltopdf 或類似的函式庫"-PdfiumViewer 只能開啟並顯示現有的 PDF。
IronPDF 透過 ChromePdfRenderer 提供原生 HTML 到 PDF 的轉換,它內部使用 Chromium 引擎來精確渲染 HTML、CSS 和 JavaScript。 RenderHtmlAsPdf() 方法直接將 HTML 字串轉換為 PDF 文件。IronPDF也可以渲染帶有 RenderUrlAsPdf() 的 URL 和帶有 RenderHtmlFileAsPdf() 的 HTML 檔案。 請參閱 HTML to PDF 文件,以瞭解全面的範例。
範例 3:PDF 至圖片轉換
之前 (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
After (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
這是PdfiumViewer的優勢之一-PDF 圖檔渲染為影像是它的主要強項。 這兩個函式庫都能有效處理這項任務,但模式不同。
PdfiumViewer 使用 document.Render(pageIndex, dpiX, dpiY, forPrinting) 和嵌套的 using 語句進行正確處置。 您需要為 System.Drawing 和 System.Drawing.Imaging 導入 ImageFormat 枚舉。
IronPDF 使用 pdf.ToBitmap(pageIndex),它傳回位圖陣列。 模式較簡單,沒有嵌套處置的要求。 對於批次操作,不帶參數的 ToBitmap() 將傳回所有頁面。 您也可以使用 RasterizeToImageFiles() 直接輸出檔案。 請參閱 PDF轉圖像文件,以瞭解其他渲染選項。
原生依賴移除
從PdfiumViewer轉移到IronPDF的顯著好處之一,就是省去原生二進位管理。
之前 (PdfiumViewer) - 複雜的部署。
MyApp/
├─── bin/
│ ├── MyApp.dll
│ ├── PdfiumViewer.dll
│ ├── x86/
│ │ └── pdfium.dll
│ └── x64/
│ └── pdfium.dll
After (IronPDF) - 乾淨部署。
MyApp/
├─── bin/
│ ├── MyApp.dll
│ └─── IronPDF.dll # 包含的所有內容
移除原生二進位引用
# 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" />
關鍵遷移注意事項
無內建檢視器控制
IronPDF 著重於後端,不包含可視化 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})
對於查看需求,請考慮使用 Process.Start() 在預設 PDF 檢視器中打開,或使用帶有 PDF 路徑的 WebBrowser 控件,或使用 Syncfusion、DevExpress 或 Telerik 等第三方檢視器控制項。
文件載入方法變更
// PdfiumViewer
PdfDocument.Load(path)
// IronPDF
PdfDocument.FromFile(path)
// PdfiumViewer
PdfDocument.Load(path)
// IronPDF
PdfDocument.FromFile(path)
' PdfiumViewer
PdfDocument.Load(path)
' IronPDF
PdfDocument.FromFile(path)
儲存方法變更
// PdfiumViewer
document.Save(path)
// IronPDF
pdf.SaveAs(path)
// PdfiumViewer
document.Save(path)
// IronPDF
pdf.SaveAs(path)
渲染方法變更
// 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")
頁面大小存取變更
// 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}")
功能比較摘要
| 特點 | PdfiumViewer | IronPDF |
|---|---|---|
| 載入 PDF | ✓ | ✓ |
| 渲染至圖片 | ✓ | ✓ |
| 內建檢視器 | ✓ | ✗ |
| 列印 PDF | ✓ | ✓ |
| 擷取文字 | ✗ | ✓ |
| 從 HTML 建立 | ✗ | ✓ |
| 從 URL 建立 | ✗ | ✓ |
| 合併 PDF | ✗ | ✓ |
| 分割 PDF | ✗ | ✓ |
| 添加水印 | ✗ | ✓ |
| 頁首/頁尾 | ✗ | ✓ |
| 表格填寫 | ✗ | ✓ |
| 密碼保護 | ✗ | ✓ |
| WinForms 支援 | ✓ | ✓ |
| ASP.NET 支援 | ✗ | ✓ |
| .NET Core 支援 | 限額 | ✓ |
| 主動維護 | 不確定 | ✓ |
遷移清單
預遷移
- 識別程式碼庫中所有PdfiumViewer的使用情況
- 使用 PdfViewer 控制項列出 WinForms
- 記錄目前渲染 DPI 設定
- 檢查本地二進位引用
- 確定列印功能使用情況
- 計畫檢視器控制項取代策略
- 取得IronPDF許可證密鑰
套件變更
- 刪除
PdfiumViewerNuGet 套件 - 刪除
PdfiumViewer.Native.x86.v8-xfa套件 - 刪除
PdfiumViewer.Native.x64.v8-xfa套件 - 從 x86/ 和 x64/ 資料夾中刪除原生 pdfium.dll 二進位文件
安裝
IronPdfNuGet 套件:dotnet add package IronPdf
程式碼變更
- 在啟動時新增許可證金鑰配置
- 將
PdfDocument.Load()替換為PdfDocument.FromFile() - 將
document.Save()替換為pdf.SaveAs() - 將
document.Render()替換為pdf.ToBitmap()或RasterizeToImageFiles() - 將
document.PageSizes[i]替換為pdf.Pages[i].Width/Height - 將 PdfViewer 控制項替換為外部檢視器或 Process.Start()
- 新增功能(文字擷取、HTML 轉 PDF 等)
後遷移
- 測試渲染輸出質量
- 測試列印功能
- 在目標平台上進行測試
- 新增功能(HTML 轉 PDF、合併、浮水印、安全功能)
- 更新文件

