從 Foxit PDF SDK 轉移到 IronPDF:(.NET指南)
從福昕 PDF SDK遷移到IronPDF可以簡化您的 .NET PDF 生成工作流程,因為它用現代化的、對開發人員友好的模式取代了複雜的、面向企業的 API。 本指南提供了一個完整的、逐步的遷移路徑,可以刪除不必要的程式碼,並簡化整個程式碼庫中的 PDF 操作。
為什麼要從福昕 PDF SDK轉移到 IronPDF?
福昕 PDF 的挑戰
Foxit PDF SDK 是一個功能強大的企業級函式庫,但它也存在相當大的複雜性,可能會減慢開發速度:
1.複雜的授權系統:多種產品、SKU 和授權類型(按開發者、按伺服器、OEM 等)使得為您的專案選擇合適的選項變得困難。
2.企業定價:定價是為大型組織量身定制的,對於較小的團隊或個人開發人員來說可能難以承受。
3.手動安裝:福昕 PDF SDK需要手動 DLL 引用或私有 NuGet 來源-沒有簡單的公共 NuGet 套件可用。
4.冗長的 API:使用 Library.Initialize() 進行庫初始化、錯誤代碼檢查以及顯式的 Library.Release() 調用,會為每個操作增加大量的樣板程式碼。
5.單獨的 HTML 轉換外掛: HTML 到 PDF 的轉換需要額外購買外掛程式-它不包含在基本 SDK 中。
6.複雜配置:設定需要詳細的物件配置(例如,HTML2PDFSettingData),具有多個屬性。
- C++ 傳承: API 模式反映了 C++ 的起源,在現代 C# 應用程式中感覺不太自然。
福昕 PDF 與IronPDF比較
| 範疇 | 福昕 PDF SDK | IronPDF |
|---|---|---|
| 安裝 | 手動 DLL/私人饋送 | 簡單的 NuGet 套件 |
| 授權 | 複雜、以企業為重點 | 透明,適合各種尺寸 |
| 初始化 | Library.Initialize(sn, key) |
設定一次授權金鑰 |
| 錯誤處理 | ErrorCode 枚舉 | 標準的 .NET 例外情況 |
| HTML 至 PDF | 另外購買附加元件 | 內建 Chromium 引擎 |
| API 風格 | C++ 傳承、冗贅 | 現代 .NET 模式 |
| 資源清理 | 手冊 Close()/Release() |
IDisposable/automatic |
| 文件 | 企業文件入口網站 | 公開教學。 |
成本效益分析
從 Foxit PDF 遷移到IronPDF可帶來切實的開發優勢:透過更簡單的 API 降低複雜性,透過直覺的方法加快開發速度,相容於現代 .NET,包括 async/await 和 LINQ 支持,採用 HTML 優先方法利用現有的 Web 技能,以及無需單獨購買附加元件即可獲得所有功能。 當您計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 時,IronPDF 為 PDF 生成提供了一個面向未來的基礎。
開始之前
先決條件
- .NET 環境:IronPDF支援 .NET Framework 4.6.2+、.NET Core 3.1+ 和 .NET 5/6/7/8/9+。
- NuGet 存取權限:確保您可以從 NuGet 安裝套件。 3.許可證密鑰:請從ironpdf.com取得用於生產環境的IronPDF許可證密鑰。
備份您的專案
# Create a backup branch
git checkout -b pre-ironpdf-migration
git add .
git commit -m "Backup before福昕 PDF SDKtoIronPDFmigration"
# Create a backup branch
git checkout -b pre-ironpdf-migration
git add .
git commit -m "Backup before福昕 PDF SDKtoIronPDFmigration"
識別所有福昕 PDF 使用方式
# Find all福昕 PDF SDKreferences
grep -r "foxit\|PDFDoc\|PDFPage\|Library.Initialize\|Library.Release" --include="*.cs" --include="*.csproj" .
# Find Foxit DLL references
find . -name "*.csproj" | xargs grep -l "Foxit\|fsdk"
# Find all福昕 PDF SDKreferences
grep -r "foxit\|PDFDoc\|PDFPage\|Library.Initialize\|Library.Release" --include="*.cs" --include="*.csproj" .
# Find Foxit DLL references
find . -name "*.csproj" | xargs grep -l "Foxit\|fsdk"
文件目前的功能
在轉移之前,請先編目:
- 您使用福昕 PDF 的哪些功能(HTML 轉換、註釋、表單、安全性)
- 授權金鑰位置與初始化程式碼
- 自訂組態與設定
- 使用 ErrorCode 枚舉的錯誤處理模式
快速啟動遷移
步驟 1:更新 NuGet 套件
#福昕 PDF SDKtypically requires manual removal of DLL references
# Check your .csproj for Foxit references and remove them
# Install IronPDF
dotnet add package IronPdf
#福昕 PDF SDKtypically requires manual removal of DLL references
# Check your .csproj for Foxit references and remove them
# Install IronPDF
dotnet add package IronPdf
如果您在 .csproj 中有 Foxit PDF 引用,請手動移除它們:。
<Reference Include="fsdk_dotnet">
<HintPath>..\libs\Foxit\fsdk_dotnet.dll</HintPath>
</Reference>
<Reference Include="fsdk_dotnet">
<HintPath>..\libs\Foxit\fsdk_dotnet.dll</HintPath>
</Reference>
步驟 2:更新命名空間
// Before (Foxit PDF)
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.annots;
using foxit.addon.conversion;
// After (IronPDF)
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
// Before (Foxit PDF)
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.annots;
using foxit.addon.conversion;
// After (IronPDF)
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Editing
步驟 3:初始化 IronPDF
此次 Foxit PDF 移轉中最顯著的改進之一是消除了複雜的初始化和清理模式:
// Before (Foxit PDF)
string sn = "YOUR_SERIAL_NUMBER";
string key = "YOUR_LICENSE_KEY";
ErrorCode error_code = Library.Initialize(sn, key);
if (error_code != ErrorCode.e_ErrSuccess)
{
throw new Exception("Failed to initialize Foxit PDF SDK");
}
// ... your code ...
Library.Release(); // Don't forget this!
// After (IronPDF)
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
// That's it! No Release() needed
// Before (Foxit PDF)
string sn = "YOUR_SERIAL_NUMBER";
string key = "YOUR_LICENSE_KEY";
ErrorCode error_code = Library.Initialize(sn, key);
if (error_code != ErrorCode.e_ErrSuccess)
{
throw new Exception("Failed to initialize Foxit PDF SDK");
}
// ... your code ...
Library.Release(); // Don't forget this!
// After (IronPDF)
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
// That's it! No Release() needed
' Before (Foxit PDF)
Dim sn As String = "YOUR_SERIAL_NUMBER"
Dim key As String = "YOUR_LICENSE_KEY"
Dim error_code As ErrorCode = Library.Initialize(sn, key)
If error_code <> ErrorCode.e_ErrSuccess Then
Throw New Exception("Failed to initialize Foxit PDF SDK")
End If
' ... your code ...
Library.Release() ' Don't forget this!
' After (IronPDF)
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY"
' That's it! No Release() needed
步驟 4:基本轉換模式
// Before (Foxit PDF)
Library.Initialize(sn, key);
HTML2PDFSettingData settings = new HTML2PDFSettingData();
settings.page_width = 612.0f;
settings.page_height = 792.0f;
using (HTML2PDF html2pdf = new HTML2PDF(settings))
{
html2pdf.Convert(htmlContent, "output.pdf");
}
Library.Release();
// After (IronPDF)
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
// Before (Foxit PDF)
Library.Initialize(sn, key);
HTML2PDFSettingData settings = new HTML2PDFSettingData();
settings.page_width = 612.0f;
settings.page_height = 792.0f;
using (HTML2PDF html2pdf = new HTML2PDF(settings))
{
html2pdf.Convert(htmlContent, "output.pdf");
}
Library.Release();
// After (IronPDF)
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Imports FoxitPDF
Imports IronPdf
' Before (Foxit PDF)
Library.Initialize(sn, key)
Dim settings As New HTML2PDFSettingData()
settings.page_width = 612.0F
settings.page_height = 792.0F
Using html2pdf As New HTML2PDF(settings)
html2pdf.Convert(htmlContent, "output.pdf")
End Using
Library.Release()
' After (IronPDF)
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
完整的 API 參考資料
命名空間對應
| 福昕 PDF 命名空間 | IronPDF 同等級產品 |
|---|---|
foxit |
IronPdf |
foxit.common |
IronPdf |
foxit.common.fxcrt |
不適用 |
foxit.pdf |
IronPdf |
foxit.pdf.annots |
IronPdf.Editing |
foxit.addon.conversion |
IronPdf.Rendering |
核心類映射
| 福昕 PDF SDK 類別 | IronPDF 同等級產品 |
|---|---|
Library |
不適用 |
PDFDoc |
PdfDocument |
PDFPage |
PdfDocument.Pages[i] |
HTML2PDF |
ChromePdfRenderer |
TextPage |
pdf.ExtractTextFromPage(i) |
Watermark |
TextStamper / ImageStamper |
Security |
SecuritySettings |
Form |
pdf.Form |
Metadata |
pdf.MetaData |
PDFDoc 方法
| 福昕PDFDoc | IronPDF PdfDocument |
|---|---|
new PDFDoc(path) |
PdfDocument.FromFile(path) |
doc.LoadW(password) |
PdfDocument.FromFile(path, password) |
doc.GetPageCount() |
pdf.PageCount |
doc.GetPage(index) |
pdf.Pages[index] |
doc.SaveAs(path, flags) |
pdf.SaveAs(path) |
doc.Close() |
pdf.Dispose() 或使用語句 |
doc.InsertDocument() |
PdfDocument.Merge() |
HTML2PDF/轉換
| 福昕 HTML2PDF | IronPDF 同等級產品 |
|---|---|
new HTML2PDFSettingData() |
new ChromePdfRenderer() |
settings.page_width |
RenderingOptions.PaperSize |
settings.page_height |
RenderingOptions.SetCustomPaperSize() |
html2pdf.Convert(html, path) |
renderer.RenderHtmlAsPdf(html) |
html2pdf.ConvertFromURL(url, path) |
renderer.RenderUrlAsPdf(url) |
水印設定
| 福昕水印 | IronPDF 同等級產品 |
|---|---|
new Watermark(doc, text, font, size, color) |
new TextStamper() |
WatermarkSettings.position |
VerticalAlignment + HorizontalAlignment |
WatermarkSettings.rotation |
Rotation |
WatermarkSettings.opacity |
Opacity |
watermark.InsertToAllPages() |
pdf.ApplyStamp(stamper) |
程式碼範例
範例 1:HTML 到 PDF 的轉換
之前(福昕 PDF SDK):
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFConversion;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
HTML2PDFSettingData settingData = new HTML2PDFSettingData();
settingData.page_width = 612.0f;
settingData.page_height = 792.0f;
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage;
using (HTML2PDF html2pdf = new HTML2PDF(settingData))
{
html2pdf.Convert("<html><body><h1>Hello World</h1></body></html>", "output.pdf");
}
Library.Release();
}
}
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFConversion;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
HTML2PDFSettingData settingData = new HTML2PDFSettingData();
settingData.page_width = 612.0f;
settingData.page_height = 792.0f;
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage;
using (HTML2PDF html2pdf = new HTML2PDF(settingData))
{
html2pdf.Convert("<html><body><h1>Hello World</h1></body></html>", "output.pdf");
}
Library.Release();
}
}
Imports Foxit.SDK
Imports Foxit.SDK.Common
Imports Foxit.SDK.PDFConversion
Imports System
Class Program
Shared Sub Main()
Library.Initialize("sn", "key")
Dim settingData As New HTML2PDFSettingData()
settingData.page_width = 612.0F
settingData.page_height = 792.0F
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage
Using html2pdf As New HTML2PDF(settingData)
html2pdf.Convert("<html><body><h1>Hello World</h1></body></html>", "output.pdf")
End Using
Library.Release()
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello World</h1></body></html>");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello World</h1></body></html>");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hello World</h1></body></html>")
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 的方法可將 15 行以上的配置代碼減少至僅 4 行。 無庫初始化、無明顯清潔、無複雜設定物件。 如需更多 HTML 呈現選項,請參閱 HTML to PDF 文件。
範例 2:URL 到 PDF 的轉換
之前(福昕 PDF SDK):
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFConversion;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
HTML2PDFSettingData settingData = new HTML2PDFSettingData();
settingData.page_width = 612.0f;
settingData.page_height = 792.0f;
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage;
using (HTML2PDF html2pdf = new HTML2PDF(settingData))
{
html2pdf.ConvertFromURL("https://www.example.com", "output.pdf");
}
Library.Release();
}
}
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFConversion;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
HTML2PDFSettingData settingData = new HTML2PDFSettingData();
settingData.page_width = 612.0f;
settingData.page_height = 792.0f;
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage;
using (HTML2PDF html2pdf = new HTML2PDF(settingData))
{
html2pdf.ConvertFromURL("https://www.example.com", "output.pdf");
}
Library.Release();
}
}
Imports Foxit.SDK
Imports Foxit.SDK.Common
Imports Foxit.SDK.PDFConversion
Imports System
Class Program
Shared Sub Main()
Library.Initialize("sn", "key")
Dim settingData As New HTML2PDFSettingData()
settingData.page_width = 612.0F
settingData.page_height = 792.0F
settingData.page_mode = HTML2PDFPageMode.e_HTML2PDFPageModeSinglePage
Using html2pdf As New HTML2PDF(settingData)
html2pdf.ConvertFromURL("https://www.example.com", "output.pdf")
End Using
Library.Release()
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 內建的 Chromium 引擎可自動處理 JavaScript 執行、CSS 呈現和動態內容。 進一步瞭解 URL 至 PDF 轉換。
範例 3:新增水印
之前(福昕 PDF SDK):
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFDoc;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
using (PDFDoc doc = new PDFDoc("input.pdf"))
{
doc.Load("");
Watermark watermark = new Watermark(doc, "Confidential",
new Font(Font.StandardID.e_StdIDHelvetica), 48.0f, 0xFF0000FF);
WatermarkSettings settings = new WatermarkSettings();
settings.flags = Watermark.e_WatermarkFlagASPageContents;
settings.position = Watermark.Position.e_PosCenter;
settings.rotation = -45.0f;
settings.opacity = 0.5f;
watermark.SetSettings(settings);
watermark.InsertToAllPages();
doc.SaveAs("output.pdf", PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
}
Library.Release();
}
}
// NuGet: Install-Package Foxit.SDK
using Foxit.SDK;
using Foxit.SDK.Common;
using Foxit.SDK.PDFDoc;
using System;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
using (PDFDoc doc = new PDFDoc("input.pdf"))
{
doc.Load("");
Watermark watermark = new Watermark(doc, "Confidential",
new Font(Font.StandardID.e_StdIDHelvetica), 48.0f, 0xFF0000FF);
WatermarkSettings settings = new WatermarkSettings();
settings.flags = Watermark.e_WatermarkFlagASPageContents;
settings.position = Watermark.Position.e_PosCenter;
settings.rotation = -45.0f;
settings.opacity = 0.5f;
watermark.SetSettings(settings);
watermark.InsertToAllPages();
doc.SaveAs("output.pdf", PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
}
Library.Release();
}
}
Imports Foxit.SDK
Imports Foxit.SDK.Common
Imports Foxit.SDK.PDFDoc
Imports System
Class Program
Shared Sub Main()
Library.Initialize("sn", "key")
Using doc As New PDFDoc("input.pdf")
doc.Load("")
Dim watermark As New Watermark(doc, "Confidential",
New Font(Font.StandardID.e_StdIDHelvetica), 48.0F, &HFF0000FF)
Dim settings As New WatermarkSettings()
settings.flags = Watermark.e_WatermarkFlagASPageContents
settings.position = Watermark.Position.e_PosCenter
settings.rotation = -45.0F
settings.opacity = 0.5F
watermark.SetSettings(settings)
watermark.InsertToAllPages()
doc.SaveAs("output.pdf", PDFDoc.SaveFlags.e_SaveFlagNoOriginal)
End Using
Library.Release()
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
pdf.ApplyWatermark(new TextStamper()
{
Text = "Confidential",
FontSize = 48,
Opacity = 50,
Rotation = -45,
VerticalAlignment = VerticalAlignment.Middle,
HorizontalAlignment = HorizontalAlignment.Center
});
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
pdf.ApplyWatermark(new TextStamper()
{
Text = "Confidential",
FontSize = 48,
Opacity = 50,
Rotation = -45,
VerticalAlignment = VerticalAlignment.Middle,
HorizontalAlignment = HorizontalAlignment.Center
});
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports IronPdf.Editing
Imports System
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("input.pdf")
pdf.ApplyWatermark(New TextStamper() With {
.Text = "Confidential",
.FontSize = 48,
.Opacity = 50,
.Rotation = -45,
.VerticalAlignment = VerticalAlignment.Middle,
.HorizontalAlignment = HorizontalAlignment.Center
})
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 的 TextStamper 提供基於屬性的直覺配置,而不是單獨的設定物件和手動頁面迭代。 有關其他選項,請參閱完整的 watermarking 文件。
範例 4:將 URL 轉換為帶有頁首和頁尾的 PDF 文件
之前(福昕 PDF SDK):
using foxit;
using foxit.addon.conversion;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
try
{
HTML2PDFSettingData settings = new HTML2PDFSettingData();
settings.page_width = 595.0f; // A4
settings.page_height = 842.0f;
settings.page_margin_top = 100.0f;
settings.page_margin_bottom = 100.0f;
//福昕 PDF SDKhas limited header/footer support
// Often requires post-processing or additional code
using (HTML2PDF html2pdf = new HTML2PDF(settings))
{
html2pdf.ConvertFromURL("https://www.example.com", "webpage.pdf");
}
}
finally
{
Library.Release();
}
}
}
using foxit;
using foxit.addon.conversion;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
try
{
HTML2PDFSettingData settings = new HTML2PDFSettingData();
settings.page_width = 595.0f; // A4
settings.page_height = 842.0f;
settings.page_margin_top = 100.0f;
settings.page_margin_bottom = 100.0f;
//福昕 PDF SDKhas limited header/footer support
// Often requires post-processing or additional code
using (HTML2PDF html2pdf = new HTML2PDF(settings))
{
html2pdf.ConvertFromURL("https://www.example.com", "webpage.pdf");
}
}
finally
{
Library.Release();
}
}
}
Imports foxit
Imports foxit.addon.conversion
Class Program
Shared Sub Main()
Library.Initialize("sn", "key")
Try
Dim settings As New HTML2PDFSettingData()
settings.page_width = 595.0F ' A4
settings.page_height = 842.0F
settings.page_margin_top = 100.0F
settings.page_margin_bottom = 100.0F
'福昕 PDF SDK has limited header/footer support
' Often requires post-processing or additional code
Using html2pdf As New HTML2PDF(settings)
html2pdf.ConvertFromURL("https://www.example.com", "webpage.pdf")
End Using
Finally
Library.Release()
End Try
End Sub
End Class
After (IronPDF):
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.WaitFor.RenderDelay(3000); // Wait for JS
// Built-in header/footer support
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center; font-size:12pt;'>Company Report</div>",
DrawDividerLine = true
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right; font-size:10pt;'>Page {page} of {total-pages}</div>",
DrawDividerLine = true
};
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.WaitFor.RenderDelay(3000); // Wait for JS
// Built-in header/footer support
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:center; font-size:12pt;'>Company Report</div>",
DrawDividerLine = true
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right; font-size:10pt;'>Page {page} of {total-pages}</div>",
DrawDividerLine = true
};
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PrintHtmlBackgrounds = True
renderer.RenderingOptions.WaitFor.RenderDelay(3000) ' Wait for JS
' Built-in header/footer support
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
.HtmlFragment = "<div style='text-align:center; font-size:12pt;'>Company Report</div>",
.DrawDividerLine = True
}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
.HtmlFragment = "<div style='text-align:right; font-size:10pt;'>Page {page} of {total-pages}</div>",
.DrawDividerLine = True
}
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
pdf.SaveAs("webpage.pdf")
End Sub
End Class
IronPDF 提供原生的 頁首和頁尾支援,並具備 HTML 風格和動態頁碼占位符。
範例 5:PDF 安全性與加密
之前(福昕 PDF SDK):
using foxit;
using foxit.pdf;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
try
{
using (PDFDoc doc = new PDFDoc("input.pdf"))
{
doc.LoadW("");
StdSecurityHandler securityHandler = new StdSecurityHandler();
securityHandler.Initialize(
StdSecurityHandler.EncryptAlgorithm.e_CipherAES,
"user_password",
"owner_password",
PDFDoc.Permission.e_PermPrint | PDFDoc.Permission.e_PermModify,
128);
doc.SetSecurityHandler(securityHandler);
doc.SaveAs("encrypted.pdf", (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
}
}
finally
{
Library.Release();
}
}
}
using foxit;
using foxit.pdf;
class Program
{
static void Main()
{
Library.Initialize("sn", "key");
try
{
using (PDFDoc doc = new PDFDoc("input.pdf"))
{
doc.LoadW("");
StdSecurityHandler securityHandler = new StdSecurityHandler();
securityHandler.Initialize(
StdSecurityHandler.EncryptAlgorithm.e_CipherAES,
"user_password",
"owner_password",
PDFDoc.Permission.e_PermPrint | PDFDoc.Permission.e_PermModify,
128);
doc.SetSecurityHandler(securityHandler);
doc.SaveAs("encrypted.pdf", (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
}
}
finally
{
Library.Release();
}
}
}
Imports foxit
Imports foxit.pdf
Class Program
Shared Sub Main()
Library.Initialize("sn", "key")
Try
Using doc As New PDFDoc("input.pdf")
doc.LoadW("")
Dim securityHandler As New StdSecurityHandler()
securityHandler.Initialize(StdSecurityHandler.EncryptAlgorithm.e_CipherAES,
"user_password",
"owner_password",
PDFDoc.Permission.e_PermPrint Or PDFDoc.Permission.e_PermModify,
128)
doc.SetSecurityHandler(securityHandler)
doc.SaveAs("encrypted.pdf", CInt(PDFDoc.SaveFlags.e_SaveFlagNoOriginal))
End Using
Finally
Library.Release()
End Try
End Sub
End Class
After (IronPDF):
using IronPdf;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
// Set passwords
pdf.SecuritySettings.OwnerPassword = "owner_password";
pdf.SecuritySettings.UserPassword = "user_password";
// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.EditAll;
pdf.SecuritySettings.AllowUserCopyPasteContent = true;
pdf.SecuritySettings.AllowUserAnnotations = true;
pdf.SaveAs("encrypted.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("input.pdf");
// Set passwords
pdf.SecuritySettings.OwnerPassword = "owner_password";
pdf.SecuritySettings.UserPassword = "user_password";
// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.EditAll;
pdf.SecuritySettings.AllowUserCopyPasteContent = true;
pdf.SecuritySettings.AllowUserAnnotations = true;
pdf.SaveAs("encrypted.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("input.pdf")
' Set passwords
pdf.SecuritySettings.OwnerPassword = "owner_password"
pdf.SecuritySettings.UserPassword = "user_password"
' Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights
pdf.SecuritySettings.AllowUserEdits = IronPdf.Security.PdfEditSecurity.EditAll
pdf.SecuritySettings.AllowUserCopyPasteContent = True
pdf.SecuritySettings.AllowUserAnnotations = True
pdf.SaveAs("encrypted.pdf")
End Sub
End Class
效能考量
重複使用 ChromePdfRenderer
為了在 Foxit PDF 遷移過程中獲得最佳效能,請重複使用 ChromePdfRenderer 實例—它是執行緒安全的:
// GOOD - Reuse renderer (thread-safe)
public class PdfService
{
private static readonly ChromePdfRenderer _renderer = new ChromePdfRenderer();
public byte[] Generate(string html) => _renderer.RenderHtmlAsPdf(html).BinaryData;
}
// BAD - Creates new instance each time
public byte[] GenerateBad(string html)
{
var renderer = new ChromePdfRenderer(); // Wasteful
return renderer.RenderHtmlAsPdf(html).BinaryData;
}
// GOOD - Reuse renderer (thread-safe)
public class PdfService
{
private static readonly ChromePdfRenderer _renderer = new ChromePdfRenderer();
public byte[] Generate(string html) => _renderer.RenderHtmlAsPdf(html).BinaryData;
}
// BAD - Creates new instance each time
public byte[] GenerateBad(string html)
{
var renderer = new ChromePdfRenderer(); // Wasteful
return renderer.RenderHtmlAsPdf(html).BinaryData;
}
Imports System
Public Class PdfService
Private Shared ReadOnly _renderer As New ChromePdfRenderer()
Public Function Generate(html As String) As Byte()
Return _renderer.RenderHtmlAsPdf(html).BinaryData
End Function
' BAD - Creates new instance each time
Public Function GenerateBad(html As String) As Byte()
Dim renderer As New ChromePdfRenderer() ' Wasteful
Return renderer.RenderHtmlAsPdf(html).BinaryData
End Function
End Class
單元轉換輔助工具
Foxit PDF SDK 使用點;IronPDF使用毫米。 在遷移時使用此輔助工具:
public static class UnitConverter
{
public static double PointsToMm(double points) => points * 0.352778;
public static double MmToPoints(double mm) => mm / 0.352778;
public static double InchesToMm(double inches) => inches * 25.4;
}
// Usage: Convert Foxit's 72 points (1 inch) toIronPDFmillimeters
renderer.RenderingOptions.MarginTop = UnitConverter.PointsToMm(72); // ~25.4mm
public static class UnitConverter
{
public static double PointsToMm(double points) => points * 0.352778;
public static double MmToPoints(double mm) => mm / 0.352778;
public static double InchesToMm(double inches) => inches * 25.4;
}
// Usage: Convert Foxit's 72 points (1 inch) toIronPDFmillimeters
renderer.RenderingOptions.MarginTop = UnitConverter.PointsToMm(72); // ~25.4mm
Public Module UnitConverter
Public Function PointsToMm(points As Double) As Double
Return points * 0.352778
End Function
Public Function MmToPoints(mm As Double) As Double
Return mm / 0.352778
End Function
Public Function InchesToMm(inches As Double) As Double
Return inches * 25.4
End Function
End Module
' Usage: Convert Foxit's 72 points (1 inch) to IronPDF millimeters
renderer.RenderingOptions.MarginTop = UnitConverter.PointsToMm(72) ' ~25.4mm
正確的資源處理方式
// GOOD - Using statement for automatic cleanup
using (var pdf = PdfDocument.FromFile("large.pdf"))
{
string text = pdf.ExtractAllText();
} // pdf is disposed automatically
// GOOD - Using statement for automatic cleanup
using (var pdf = PdfDocument.FromFile("large.pdf"))
{
string text = pdf.ExtractAllText();
} // pdf is disposed automatically
Imports PdfDocument
' GOOD - Using block for automatic cleanup
Using pdf = PdfDocument.FromFile("large.pdf")
Dim text As String = pdf.ExtractAllText()
End Using ' pdf is disposed automatically
疑難排解
問題 1:Library.Initialize() 未找到
問題:IronPDF中不存在 Library.Initialize()。
解決方案:IronPDF使用了更簡單的初始化模式:
// Foxit PDF
Library.Initialize(sn, key);
//IronPDF- just set license key once at startup
IronPdf.License.LicenseKey = "YOUR-KEY";
// Foxit PDF
Library.Initialize(sn, key);
//IronPDF- just set license key once at startup
IronPdf.License.LicenseKey = "YOUR-KEY";
問題 2:錯誤碼處理
問題:代碼檢查 ErrorCode.e_ErrSuccess,但IronPDF沒有此內容。
解決方案:使用標準的.NET異常處理:
// Foxit PDF
ErrorCode err = doc.LoadW("");
if (err != ErrorCode.e_ErrSuccess) { /* handle error */ }
// IronPDF
try
{
var pdf = PdfDocument.FromFile("input.pdf");
}
catch (IOException ex)
{
Console.WriteLine($"Failed to load PDF: {ex.Message}");
}
// Foxit PDF
ErrorCode err = doc.LoadW("");
if (err != ErrorCode.e_ErrSuccess) { /* handle error */ }
// IronPDF
try
{
var pdf = PdfDocument.FromFile("input.pdf");
}
catch (IOException ex)
{
Console.WriteLine($"Failed to load PDF: {ex.Message}");
}
Imports System
Imports System.IO
' Foxit PDF
Dim err As ErrorCode = doc.LoadW("")
If err <> ErrorCode.e_ErrSuccess Then
' handle error
End If
' IronPDF
Try
Dim pdf = PdfDocument.FromFile("input.pdf")
Catch ex As IOException
Console.WriteLine($"Failed to load PDF: {ex.Message}")
End Try
問題 3:PDFDoc.Close() 未找到
問題:IronPDF中不存在 doc.Close() 方法。
解:使用 Dispose() 或 using 語句:
// Foxit PDF
doc.Close();
// IronPDF
pdf.Dispose();
// or better: wrap in using statement
// Foxit PDF
doc.Close();
// IronPDF
pdf.Dispose();
// or better: wrap in using statement
遷移清單
預遷移
- 清點所有已使用的福昕 PDF SDK功能
- 文件許可證密鑰位置
- 注意所有
Library.Initialize()和Library.Release()調用 - 列出自訂設定(頁面大小、邊距等)
- 使用 ErrorCode 識別錯誤處理模式
- 將項目備份到版本控制系統
- 取得IronPDF許可證密鑰
套件遷移
- 從 .csproj 檔案移除福昕 PDF SDKDLL 引用
- 刪除所有私有 NuGet 來源配置
- 安裝 IronPdf NuGet 套件:
dotnet add package IronPdf - 更新命名空間匯入
- 啟動時設定IronPDF許可證密鑰
程式碼遷移
- 刪除
Library.Initialize()和Library.Release()調用 - 將
ErrorCode檢查替換為 try/catch - 將
PDFDoc替換為PdfDocument - 將
HTML2PDF替換為ChromePdfRenderer - 將頁面存取權限從
GetPage(i)更新為Pages[i] - 將
SaveAs(path, flags)替換為SaveAs(path) - 將
Close()替換為Dispose()或使用語句 - 更新水印代碼,使用
TextStamper - 將單位從點轉換為毫米
測試
- 驗證 HTML 轉 PDF 輸出是否符合預期
- 測試 PDF 載入和文字擷取
- 驗證合併功能
- 檢查水印外觀
- 測試安全/加密功能
- 驗證表單欄位操作
- 效能測試
後遷移
- 刪除福昕 PDF SDKDLL 文件
- 刪除與 Foxit 相關的設定檔
- 更新文件
- 清理未使用的輔助程式碼
遷移。

