如何使用 WaitFor 來延遲 C# PDF 渲染
從PDFium .NET包轉移到IronPDF,將您的.NET PDF工作流程從一個以渲染為重點的引擎(需要本機二進制依賴)轉變為一個完整的PDF解決方案,能夠處理建立、操作和渲染,並且無需複雜的平臺特定依賴。 本指南提供逐步的遷移路徑,消除了本機依賴管理,同時增加了PDFium本身不提供的功能。
為什麼從PDFium包遷移到IronPDF
了解PDFium .NET包
PDFium是Google的BSD-3-Clause C++ PDF渲染和解析引擎—這就是驅動Chrome內建PDF查看器的引擎。 沒有由Google提供的官方.NET綁定; 相反,.NET生態系統通過社區包消耗PDFium。 四個最常被引用的NuGet包是PdfiumViewer(pvginkel; Apache 2.0; 2019年8月存檔,最後一次發布在2017年11月,版本2.13.0,.NET Framework 2.0+),PdfiumViewer.Updated(一個維護的分支,被移植到.NET Core / .NET 6),PDFiumCore(Dtronix; .NET Standard 2.1 P/Invoke綁定),和Pdfium.Net.SDK(Patagames; 商業的,永久許可的SDK)。
它們的API不同,但它們共有一個特點:PDFium本身是一個渲染和解析引擎,而不是HTML到PDF或文件創作引擎。PDFium沒有HTML解析器,因此沒有包可以自行將HTML轉換為PDF。 本指南使用PdfiumViewer作為"前"片段的代表包,因為其API是最常被引用的; 遷移方法對四者來說都是一樣的。
PDFium包的主要限制
-
渲染為主: PDFium沒有HTML解析器,因此沒有包可以從HTML、URL或任意圖像建立PDF。
-
開源包的操作有限: PdfiumViewer / PDFiumCore暴露查看和每頁文字抽取; 在Patagames Pdfium.Net.SDK中的合併/拆分/表單編輯是部分存在的,而在免費包中基本不存在。
-
本機二進制依賴:需要平台特定的PDFium二進制(
pdfium.dll/.so/.dylib)每個RID。 -
部署複雜性:必須按平台捆綁和管理本機二進制,使用x86、x64和運行時資料夾。
-
基本文字抽取: PdfiumViewer通過
GetPdfText(int)暴露每頁的原始文字; 布局/格式元資料未暴露。 -
無HTML到PDF: PDFium沒有HTML解析器; 無法直接轉換網頁內容。
-
無內建頁首/頁尾:沒有高階的頁面覆蓋API。
-
無內建水印:沒有蓋章原語。
-
表單:在免費包中為只讀或缺席; 在Patagames Pdfium.Net.SDK中可用。
- 安全性:免費包未暴露加密/權限; 在Patagames Pdfium.Net.SDK中可用。
PDFium包與IronPDF比較
| 方面 | PDFium .NET包 | IronPDF |
|---|---|---|
| 主要重點 | 渲染/視覺 | 完整的PDF解決方案 |
| 渲染保真度 | 高保真渲染 | 高,特別是對HTML/CSS/JS |
| 從HTML建立PDF | None | 是(HTML,網址,圖像) |
| PDF 操作 | 免費包:無; Patagames:部分 | 是(合併,分割,編輯) |
| HTML 到 PDF | None | 是(Chromium引擎) |
| 水印 | 未內建 | 是 |
| 頁首/頁尾 | 未內建 | 是 |
| 填寫表單 | 免費包:無; Patagames:是 | 是 |
| 安全性 | 免費包:無; Patagames:是 | 是 |
| 本機相依性 | 必需的 | 捆綁/ NuGet管理 |
| 跨平台 | 手動本機二進制管理 | 自動 |
| 部署的便利性 | 由於本機依賴而變得複雜 | 更簡單; 依賴複雜性減少 |
對於目標現代.NET的團隊,IronPDF提供了一個全面管理的基礎,消除了本機二進制管理,同時增加了全面的PDF建立和操作功能。
開始之前
前提條件
- .NET環境:.NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取: 有能力安裝NuGet套件
- IronPDF授權: 從ironpdf.com獲取您的授權金鑰
NuGet包變更
# Remove whichever PDFium wrapper you used:
# PdfiumViewer (Apache 2.0 - archived 2019, .NET Framework only)
# PdfiumViewer.Updated (community fork, .NET Core / .NET 6)
# PDFiumCore (Dtronix - .NET Standard 2.1 P/Invoke bindings)
# Pdfium.Net.SDK (Patagames - commercial, perpetual license)
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Updated
dotnet remove package PDFiumCore
dotnet remove package Pdfium.Net.SDK
# Install IronPDF
dotnet add package IronPdf
# Remove whichever PDFium wrapper you used:
# PdfiumViewer (Apache 2.0 - archived 2019, .NET Framework only)
# PdfiumViewer.Updated (community fork, .NET Core / .NET 6)
# PDFiumCore (Dtronix - .NET Standard 2.1 P/Invoke bindings)
# Pdfium.Net.SDK (Patagames - commercial, perpetual license)
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Updated
dotnet remove package PDFiumCore
dotnet remove package Pdfium.Net.SDK
# 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"
確認PDFium包的使用
# Find PDFium wrapper usage
grep -rE "PdfiumViewer|PDFiumCore|Patagames\.Pdf|PdfDocument\.Load|\.Render\(" --include="*.cs" .
# Find native binary references
grep -rE "pdfium\.dll|libpdfium\.(so|dylib)" --include="*.csproj" --include="*.config" .
# Find platform-specific code
grep -rE "#if.*64|WIN32|WIN64|LINUX|OSX" --include="*.cs" .
# Find PDFium wrapper usage
grep -rE "PdfiumViewer|PDFiumCore|Patagames\.Pdf|PdfDocument\.Load|\.Render\(" --include="*.cs" .
# Find native binary references
grep -rE "pdfium\.dll|libpdfium\.(so|dylib)" --include="*.csproj" --include="*.config" .
# Find platform-specific code
grep -rE "#if.*64|WIN32|WIN64|LINUX|OSX" --include="*.cs" .
完整API參考
名稱空間變更
// PDFium wrappers (use the one you installed)
using PdfiumViewer; // PdfiumViewer / PdfiumViewer.Updated
using PDFiumCore; // Dtronix PDFiumCore (P/Invoke bindings)
using Patagames.Pdf; // Patagames Pdfium.Net.SDK
using Patagames.Pdf.Net; // Patagames Pdfium.Net.SDK
// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
// PDFium wrappers (use the one you installed)
using PdfiumViewer; // PdfiumViewer / PdfiumViewer.Updated
using PDFiumCore; // Dtronix PDFiumCore (P/Invoke bindings)
using Patagames.Pdf; // Patagames Pdfium.Net.SDK
using Patagames.Pdf.Net; // Patagames Pdfium.Net.SDK
// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
Imports PdfiumViewer ' PdfiumViewer / PdfiumViewer.Updated
Imports PDFiumCore ' Dtronix PDFiumCore (P/Invoke bindings)
Imports Patagames.Pdf ' Patagames Pdfium.Net.SDK
Imports Patagames.Pdf.Net ' Patagames Pdfium.Net.SDK
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Editing
核心類別映射
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
PdfiumViewer.PdfDocument |
IronPdf.PdfDocument |
在不同命名空間中同樣的簡單名稱 |
PdfRenderer(WinForms控制) |
(不適用) | PdfiumViewer也提供UI控制; IronPDF是無頭的 |
| (不可用) | ChromePdfRenderer |
HTML / URL → PDF |
| (不可用) | HtmlHeaderFooter |
頁首/頁尾 |
文件載入映射
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
PdfDocument.Load(path) |
PdfDocument.FromFile(path) |
從檔案載入 |
PdfDocument.Load(stream) |
PdfDocument.FromStream(stream) |
從流載入 |
| (將字節包裹在MemoryStream中) | PdfDocument.FromBinaryData(bytes) |
從字節載入 |
文件屬性對應
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
document.PageCount |
document.PageCount |
一樣 |
IList<SizeF>) |
document.Pages[index].Width / Height |
PdfiumViewer暴露尺寸; pages are not first-class objects |
document.PageSizes[i].Width |
document.Pages[i].Width |
每頁寬度以點為單位 |
文字提取映射
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
document.GetPdfText(pageIndex) |
document.Pages[index].Text |
每頁 |
| (手動迴圈) | document.ExtractAllText() |
所有頁面 |
儲存文件映射
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
document.Save(stream) |
document.SaveAs(path) |
PdfiumViewer需要一個流; IronPDF需要一個路徑 |
| (不可用) | document.Stream |
存取原始流 |
| (不可用) | document.BinaryData |
獲取字節 |
頁面渲染映射
| PdfiumViewer | IronPDF | 注釋 |
|---|---|---|
document.Render(page, width, height, dpiX, dpiY, flags) |
pdf.RasterizeToImageFiles(path, DPI) |
光柵化 |
在document.Render(i, ...)上的手動迴圈 |
pdf.RasterizeToImageFiles("page_*.png") |
批量渲染所有頁面 |
| 手動比例數學 | DPI = 72 * scale |
比例到DPI的轉換 |
PDFium包中不可用的新功能
| IronPDF功能 | 說明 |
|---|---|
ChromePdfRenderer.RenderHtmlAsPdf() |
從HTML建立 |
ChromePdfRenderer.RenderUrlAsPdf() |
從URL建立 |
ChromePdfRenderer.RenderHtmlFileAsPdf() |
從HTML文件建立 |
PdfDocument.Merge() |
合併PDF |
pdf.CopyPages() |
提取頁面 |
pdf.RemovePages() |
刪除頁面 |
pdf.InsertPdf() |
在位置插入PDF |
pdf.ApplyWatermark() |
新增水印 |
pdf.AddHtmlHeaders() |
新增頁首 |
pdf.AddHtmlFooters() |
新增頁尾 |
pdf.SecuritySettings |
密碼保護 |
pdf.SignWithDigitalSignature() |
數位簽名 |
pdf.Form |
表單填寫 |
程式碼遷移範例
範例1:從PDF中提取文字
之前(PdfiumViewer):
// NuGet: Install-Package PdfiumViewer (Apache 2.0; archived Aug 2019, .NET Framework 2.0+)
// or Install-Package PdfiumViewer.Updated (maintained fork, .NET Core / .NET 6)
// or Install-Package PDFiumCore (Dtronix, .NET Standard 2.1 P/Invoke)
// or Install-Package Pdfium.Net.SDK (Patagames, commercial perpetual license)
using PdfiumViewer;
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
using (var document = PdfDocument.Load(pdfPath))
{
StringBuilder text = new StringBuilder();
for (int i = 0; i < document.PageCount; i++)
{
// PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
// No layout / format metadata is exposed.
string pageText = document.GetPdfText(i);
text.AppendLine(pageText);
}
Console.WriteLine(text.ToString());
}
}
}
// NuGet: Install-Package PdfiumViewer (Apache 2.0; archived Aug 2019, .NET Framework 2.0+)
// or Install-Package PdfiumViewer.Updated (maintained fork, .NET Core / .NET 6)
// or Install-Package PDFiumCore (Dtronix, .NET Standard 2.1 P/Invoke)
// or Install-Package Pdfium.Net.SDK (Patagames, commercial perpetual license)
using PdfiumViewer;
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
string pdfPath = "document.pdf";
using (var document = PdfDocument.Load(pdfPath))
{
StringBuilder text = new StringBuilder();
for (int i = 0; i < document.PageCount; i++)
{
// PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
// No layout / format metadata is exposed.
string pageText = document.GetPdfText(i);
text.AppendLine(pageText);
}
Console.WriteLine(text.ToString());
}
}
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Text
Module Program
Sub Main()
Dim pdfPath As String = "document.pdf"
Using document = PdfDocument.Load(pdfPath)
Dim text As New StringBuilder()
For i As Integer = 0 To document.PageCount - 1
' PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
' No layout / format metadata is exposed.
Dim pageText As String = document.GetPdfText(i)
text.AppendLine(pageText)
Next
Console.WriteLine(text.ToString())
End Using
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
string pdfPath = "document.pdf";
var pdf = PdfDocument.FromFile(pdfPath);
string text = pdf.ExtractAllText();
Console.WriteLine(text);
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
string pdfPath = "document.pdf";
var pdf = PdfDocument.FromFile(pdfPath);
string text = pdf.ExtractAllText();
Console.WriteLine(text);
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim pdfPath As String = "document.pdf"
Dim pdf = PdfDocument.FromFile(pdfPath)
Dim text As String = pdf.ExtractAllText()
Console.WriteLine(text)
End Sub
End Class
這裏的差異顯著。 PdfiumViewer需要手動迴圈遍歷每個頁面,使用using語句以正確處理—返回的文字是每頁的原始文字,沒有布局/格式元資料。
IronPDF簡化為幾行程式碼:使用PdfDocument.FromFile() 載入,使用ExtractAllText()提取並輸出。 The ExtractAllText() 方法自動處理所有頁面。 如果需要每頁提取,您可以使用pdf.Pages[index].Text。 查看文字提取文件以獲取更多選擇。
範例2:PDF合併
之前(PdfiumViewer):
// NuGet: Install-Package PdfiumViewer (representative open-source PDFium wrapper)
// PdfiumViewer and PDFiumCore do not expose PDF merge APIs - PDFium itself
// is a rendering / parsing engine, not a document-authoring engine.
// (Patagames Pdfium.Net.SDK exposes some document-edit operations but is commercial.)
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> pdfFiles = new List<string>
{
"document1.pdf",
"document2.pdf",
"document3.pdf"
};
// To merge PDFs from a PDFium wrapper you must reach for another library
// (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
// document-edit APIs.
Console.WriteLine("PDF merging is not supported by PdfiumViewer / PDFiumCore.");
}
}
// NuGet: Install-Package PdfiumViewer (representative open-source PDFium wrapper)
// PdfiumViewer and PDFiumCore do not expose PDF merge APIs - PDFium itself
// is a rendering / parsing engine, not a document-authoring engine.
// (Patagames Pdfium.Net.SDK exposes some document-edit operations but is commercial.)
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> pdfFiles = new List<string>
{
"document1.pdf",
"document2.pdf",
"document3.pdf"
};
// To merge PDFs from a PDFium wrapper you must reach for another library
// (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
// document-edit APIs.
Console.WriteLine("PDF merging is not supported by PdfiumViewer / PDFiumCore.");
}
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Collections.Generic
Module Program
Sub Main()
Dim pdfFiles As New List(Of String) From {
"document1.pdf",
"document2.pdf",
"document3.pdf"
}
' To merge PDFs from a PDFium wrapper you must reach for another library
' (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
' document-edit APIs.
Console.WriteLine("PDF merging is not supported by PdfiumViewer / PDFiumCore.")
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
List<string> pdfFiles = new List<string>
{
"document1.pdf",
"document2.pdf",
"document3.pdf"
};
// PdfDocument.Merge accepts an IEnumerable<PdfDocument>, so load each file first.
var docs = pdfFiles.Select(path => PdfDocument.FromFile(path)).ToList();
var merged = PdfDocument.Merge(docs);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
List<string> pdfFiles = new List<string>
{
"document1.pdf",
"document2.pdf",
"document3.pdf"
};
// PdfDocument.Merge accepts an IEnumerable<PdfDocument>, so load each file first.
var docs = pdfFiles.Select(path => PdfDocument.FromFile(path)).ToList();
var merged = PdfDocument.Merge(docs);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.Linq
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim pdfFiles As New List(Of String) From {
"document1.pdf",
"document2.pdf",
"document3.pdf"
}
' PdfDocument.Merge accepts an IEnumerable(Of PdfDocument), so load each file first.
Dim docs = pdfFiles.Select(Function(path) PdfDocument.FromFile(path)).ToList()
Dim merged = PdfDocument.Merge(docs)
merged.SaveAs("merged.pdf")
Console.WriteLine("PDFs merged successfully")
End Sub
End Module
此範例顯示了功能差距。免費PDFium包(PdfiumViewer、PDFiumCore)不提供合併API;Patagames Pdfium.Net.SDK提供一些文件編輯操作,但它是商業的。
IronPDF提供了本地合併,使用靜態PdfDocument.FromFile()載入每個輸入文件,然後合併生成的文件。 結果是一個新的SaveAs()保存。 了解更多合併和拆分PDF的資訊。
範例3:HTML到PDF轉換
之前(PdfiumViewer):
// NuGet: Install-Package PdfiumViewer (representative PDFium wrapper)
// PDFium is a PDF rendering / parsing engine. It has NO HTML parser, so no
// PDFium .NET wrapper (PdfiumViewer, PdfiumViewer.Updated, PDFiumCore,
// Pdfium.Net.SDK) can convert HTML to PDF on its own.
using PdfiumViewer;
using System;
using System.IO;
using System.Drawing.Printing;
class Program
{
static void Main()
{
// PDFium has no native HTML-to-PDF capability.
// Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
// IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
string htmlContent = "<h1>Hello World</h1>";
Console.WriteLine("HTML to PDF conversion is not supported by PDFium.");
}
}
// NuGet: Install-Package PdfiumViewer (representative PDFium wrapper)
// PDFium is a PDF rendering / parsing engine. It has NO HTML parser, so no
// PDFium .NET wrapper (PdfiumViewer, PdfiumViewer.Updated, PDFiumCore,
// Pdfium.Net.SDK) can convert HTML to PDF on its own.
using PdfiumViewer;
using System;
using System.IO;
using System.Drawing.Printing;
class Program
{
static void Main()
{
// PDFium has no native HTML-to-PDF capability.
// Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
// IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
string htmlContent = "<h1>Hello World</h1>";
Console.WriteLine("HTML to PDF conversion is not supported by PDFium.");
}
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Drawing.Printing
Module Program
Sub Main()
' PDFium has no native HTML-to-PDF capability.
' Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
' IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
Dim htmlContent As String = "<h1>Hello World</h1>"
Console.WriteLine("HTML to PDF conversion is not supported by PDFium.")
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string htmlContent = "<h1>Hello World</h1>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string htmlContent = "<h1>Hello World</h1>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim htmlContent As String = "<h1>Hello World</h1>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Module
此範例演示了最顯著的功能差異。 PDFium沒有HTML解析器,因此沒有圍繞它的.NET包可以自行將HTML轉換為PDF。
IronPDF以ChromePdfRenderer提供本地HTML到PDF轉換,該方法內部使用Chromium引擎,以便準確渲染HTML、CSS和JavaScript。 RenderHtmlAsPdf()方法將HTML字串直接轉換為PDF文件。 IronPDF也可以用RenderHtmlFileAsPdf()渲染HTML文件。 查看更多HTML到PDF文件以獲得完整的範例。
取消本機依賴
從PDFium包遷移到IronPDF的最大優勢之一是取消本機二進制管理。
之前(PDFium包)- 複雜的部署
MyApp/
├── bin/
│ ├── MyApp.dll
│ ├── PdfiumViewer.dll # or PDFiumCore.dll / Patagames.Pdf.dll
│ ├── x86/
│ │ └── pdfium.dll
│ └── x64/
│ └── pdfium.dll
├── runtimes/
│ ├── win-x86/native/
│ │ └── pdfium.dll
│ ├── win-x64/native/
│ │ └── pdfium.dll
│ ├── linux-x64/native/
│ │ └── libpdfium.so
│ └── osx-x64/native/
│ └── libpdfium.dylib
之後(IronPDF)- 清晰的部署
MyApp/
├── bin/
│ ├── MyApp.dll
│ └── IronPdf.dll # 包含所有內容
取消本機二進制引用
# Delete native PDFium binaries
rm -rf x86/x64/runtimes/
# Remove from .csproj
# Delete any <Content Include="pdfium.dll" /> entries
# Delete any <None Include="x86/pdfium.dll" /> entries
# Delete native PDFium binaries
rm -rf x86/x64/runtimes/
# Remove from .csproj
# Delete any <Content Include="pdfium.dll" /> entries
# Delete any <None Include="x86/pdfium.dll" /> entries
關鍵遷移注意事項
比例到DPI的轉換
PdfiumViewer的Render(...)需要顯式dpiX/dpiY,而PDFium的底層渲染是按比例驅動的(1.0 = 72 DPI)。 IronPDF直接使用DPI:
// Formula:IronPDFDPI = 72 × PDFium scale
// PDFium scale 2.0 →IronPDFDPI 144
pdf.RasterizeToImageFiles("*.png", DPI: 144);
// Formula:IronPDFDPI = 72 × PDFium scale
// PDFium scale 2.0 →IronPDFDPI 144
pdf.RasterizeToImageFiles("*.png", DPI: 144);
' Formula: IronPDFDPI = 72 × PDFium scale
' PDFium scale 2.0 → IronPDFDPI 144
pdf.RasterizeToImageFiles("*.png", DPI:=144)
文件載入方法更改
// 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 (takes a Stream)
document.Save(stream)
//IronPDF(takes a path)
pdf.SaveAs(path)
// PdfiumViewer (takes a Stream)
document.Save(stream)
//IronPDF(takes a path)
pdf.SaveAs(path)
簡化的處理模式
// PdfiumViewer: explicit disposal of document and the rendered Image
using (var document = PdfDocument.Load(path))
using (var bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None))
{
bitmap.Save("output.png");
}
// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
// PdfiumViewer: explicit disposal of document and the rendered Image
using (var document = PdfDocument.Load(path))
using (var bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None))
{
bitmap.Save("output.png");
}
// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
Imports PdfiumViewer
' PdfiumViewer: explicit disposal of document and the rendered Image
Using document = PdfDocument.Load(path)
Using bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None)
bitmap.Save("output.png")
End Using
End Using
' IronPDF: Simplified
Dim pdf = PdfDocument.FromFile(path)
pdf.RasterizeToImageFiles("output.png")
移除平臺特定程式碼
// PDFium wrapper: required platform detection / RID-specific binaries
#if WIN64
// Load x64 pdfium.dll
#else
// Load x86 pdfium.dll
#endif
// IronPDF: Remove all platform-specific code
// Just use the API directly
// PDFium wrapper: required platform detection / RID-specific binaries
#if WIN64
// Load x64 pdfium.dll
#else
// Load x86 pdfium.dll
#endif
// IronPDF: Remove all platform-specific code
// Just use the API directly
功能比較總結
| 功能 | PDFium .NET包 | IronPDF |
|---|---|---|
| 載入PDF | 是 | 是 |
| 渲染到圖像 | 是 | 是 |
| 提取文字 | 是(基本的,每頁) | 是(邏輯/視覺順序) |
| 頁面資訊 | 是 | 是 |
| 從HTML建立 | None | 是 |
| 從URL建立 | None | 是 |
| 合併PDF | 免費包:無;Patagames:部分 | 是 |
| 拆分PDF | 免費包:無;Patagames:部分 | 是 |
| 新增水印 | 未內建 | 是 |
| 頁眉/頁腳 | 未內建 | 是 |
| 表單填寫 | 免費包:無;Patagames:是 | 是 |
| 數位簽名 | 免費包:無;Patagames:是 | 是 |
| 密碼保護 | 免費包:無;Patagames:是 | 是 |
| 本機依賴 | 必需的 | 捆綁/ NuGet管理 |
| 跨平臺 | 複雜的(每RID本機二進制) | 自動 |
| 記憶體管理 | 手動處理 | 簡化 |
遷移檢查表
遷移前
- 確認使用的PDFium包是什麼(PdfiumViewer / PdfiumViewer.Updated / PDFiumCore / Pdfium.Net.SDK)
- 記錄當前渲染尺寸/DPI/使用的比例
- 列出項目中的本機二進制位置(每RID)
- 檢查平臺特定的載入程式碼
- 確認PDF建立需求(目前是否使用單獨的工具?)
- 審查轉換的處理模式
- 獲得IronPDF授權金鑰
套件變更
- 刪除PDFium包NuGet包:
Pdfium.Net.SDK - 從
pdfium.dll/.so/.dylib二進制文件 - 移除平臺特定的條件編譯
- 更新.csproj以移除本機二進制引用
- 安裝
IronPdfNuGet包:dotnet add package IronPdf
程式碼變更
- 在啟動時加入授權金鑰配置
- 將
PdfDocument.FromFile() - 將
pdf.SaveAs() - 將
pdf.ExtractAllText() - 將比例因數轉換為DPI值(DPI = 72 × 比例)
- 簡化的處理模式(移除巢狀的using語句)
- 移除平臺特定程式碼
遷移後
- 測試渲染輸出質量
- 比較文字提取結果
- 測試跨平台部署
- 新增新功能(HTML到PDF,合併,水印,安全性)
- 更新文件

