跳至页脚内容
迁移指南

从 Foxit PDF SDK 迁移到 IronPDF:(.NET指南)

从福昕 PDF SDK迁移到IronPDF可以简化您的 .NET PDF 生成工作流程,因为它用现代化的、对开发人员友好的模式取代了复杂的、面向企业的 API。 本指南提供了一个完整的、分步的迁移路径,可以删除不必要的代码,并简化整个代码库中的 PDF 操作。

为什么要从福昕 PDF SDK 迁移到 IronPDF.

福昕 PDF 的挑战

Foxit PDF SDK 是一个功能强大的企业级库,但它也存在相当大的复杂性,可能会减慢开发速度:

1.复杂的许可系统:多种产品、SKU 和许可类型(按开发者、按服务器、OEM 等)使得为您的项目选择合适的选项变得困难。

2.企业定价:定价是为大型组织量身定制的,对于较小的团队或个人开发人员来说可能难以承受。

  1. 重型本机包:公共NuGet包(Foxit.SDK.Dotnet)很大(约240MB),旧项目可能仍然携带直接DLL引用或私人源配置。

  2. 冗长的API:使用Library.Release()调用为每个操作增加了样板代码。

  3. 单独的 HTML2PDF 引擎: HTML 到 PDF 转换需要 HTML2PDF 引擎二进制文件,而不是捆绑在 NuGet 包中,而是由 Foxit 技术支持和销售独立分发。

  4. 复杂配置:设置需要详细的对象配置(例如,HTML2PDFSettingData)和多个属性。

  5. C++ 传承: API 模式反映了 C++ 的起源,在现代 C# 应用程序中感觉不太自然。

福昕 PDF 与IronPDF对比

方面 福昕 PDF SDK IronPDF
安装 Foxit.SDK.Dotnet(约240MB)+独立的HTML2PDF引擎 简单的 NuGet 软件包
许可 销售主导,每开发者每平台 透明,适合各种尺寸
初始化 Library.Initialize(sn, key) 设置一次许可证密钥
错误处理 ErrorCode 枚举 .NET Standard例外情况
HTML 至 PDF 单独的引擎下载 内置 Chromium 引擎
API 风格 C++ 遗产,冗长 现代 .NET 模式
资源清理 手动Release() IDisposable/automatic
文档 企业文档门户 公共教程

成本效益分析

从 Foxit PDF 移动到IronPDF提供了切实的开发优势:通过更简单的 API 减少复杂性、更快速的开发、与现代 .NET 的兼容性(支持 async/await 和 LINQ)、 HTML 优先的方式使用现有的 Web 技能,以及不需要单独引擎下载的 HTML 转换。IronPDF在当前 .NET 版本上运行,并与现代 C# 模式完美结合。


开始之前

前提条件

  1. .NET 环境:IronPDF支持 .NET Framework 4.6.2+、.NET Core 3.1+ 和 .NET 5/6/7/8/9+。
  2. 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"
SHELL

识别所有福昕 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"
SHELL

文档当前功能

在迁移之前,编目:

  • 您使用的福昕 PDF 功能(HTML 转换、注释、表单、安全性)
  • 许可证密钥位置和初始化代码
  • 自定义配置和设置
  • 使用 ErrorCode 枚举的错误处理模式

快速启动迁移

步骤 1:更新 NuGet 软件包

# Remove the Foxit NuGet package
dotnet remove package Foxit.SDK.Dotnet

# Install IronPDF
dotnet add package IronPdf
# Remove the Foxit NuGet package
dotnet remove package Foxit.SDK.Dotnet

# Install IronPDF
dotnet add package IronPdf
SHELL

如果您有旧的直接 Foxit DLL 引用在 .csproj 中(旧版本),请手动删除它们:


<Reference Include="fsdk_dotnet">
    <HintPath>..\libs\Foxit\fsdk_dotnet.dll</HintPath>
</Reference>

<Reference Include="fsdk_dotnet">
    <HintPath>..\libs\Foxit\fsdk_dotnet.dll</HintPath>
</Reference>
XML

还请删除任何单独解压的 HTML2PDF 引擎文件夹。

步骤 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
$vbLabelText   $csharpLabel

第 3 步:初始化 IronPDF.

此次福昕 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-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-LICENSE-KEY";
// That's it! No Release() needed
Imports System

' 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-LICENSE-KEY"
' That's it! No Release() needed
$vbLabelText   $csharpLabel

步骤 4:基本转换模式

// Before (Foxit PDF)
Library.Initialize(sn, key);
HTML2PDFSettingData settings = new HTML2PDFSettingData();
settings.page_width = 612.0f;
settings.page_height = 792.0f;
Convert.FromHTML(htmlContent, @"C:\Foxit\html2pdf_engine", "",
                 settings, "output.pdf", 30);
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;
Convert.FromHTML(htmlContent, @"C:\Foxit\html2pdf_engine", "",
                 settings, "output.pdf", 30);
Library.Release();

// After (IronPDF)
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
' Before (Foxit PDF)
Library.Initialize(sn, key)
Dim settings As New HTML2PDFSettingData()
settings.page_width = 612.0F
settings.page_height = 792.0F
Convert.FromHTML(htmlContent, "C:\Foxit\html2pdf_engine", "", settings, "output.pdf", 30)
Library.Release()

' After (IronPDF)
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

完整的 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()
Convert.FromHTML(html, engine, ...) renderer.RenderHtmlAsPdf(html)
Convert.FromHTML(url, engine, ...) 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.Dotnet
// HTML-to-PDF requires the separate福昕 HTML2PDFengine (engine_path),
// obtained from Foxit support/sales — not in the NuGet package.
using foxit;
using foxit.common;
using foxit.addon.conversion;
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;

        Convert.FromHTML(
            "<html><body><h1>Hello World</h1></body></html>",
            @"C:\Foxit\html2pdf_engine",   // engine_path (separate download)
            "",                              // cookies path
            settingData,
            "output.pdf",
            30);                             // timeout (seconds)

        Library.Release();
    }
}
// NuGet: Install-Package Foxit.SDK.Dotnet
// HTML-to-PDF requires the separate福昕 HTML2PDFengine (engine_path),
// obtained from Foxit support/sales — not in the NuGet package.
using foxit;
using foxit.common;
using foxit.addon.conversion;
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;

        Convert.FromHTML(
            "<html><body><h1>Hello World</h1></body></html>",
            @"C:\Foxit\html2pdf_engine",   // engine_path (separate download)
            "",                              // cookies path
            settingData,
            "output.pdf",
            30);                             // timeout (seconds)

        Library.Release();
    }
}
Imports foxit
Imports foxit.common
Imports foxit.addon.conversion
Imports System

Module Program
    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

        Convert.FromHTML(
            "<html><body><h1>Hello World</h1></body></html>",
            "C:\Foxit\html2pdf_engine",   ' engine_path (separate download)
            "",                           ' cookies path
            settingData,
            "output.pdf",
            30)                           ' timeout (seconds)

        Library.Release()
    End Sub
End Module
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

IronPDF 方法将 15 行以上的配置代码减少到仅 4 行。 没有库初始化,没有显式清理,没有复杂的设置对象。 有关更多 HTML 渲染选项,请参阅 HTML to PDF 文档

示例 2:URL 到 PDF 的转换

之前(福昕 PDF SDK):

// NuGet: Install-Package Foxit.SDK.Dotnet
// Convert.FromHTML accepts either a URL or a literal HTML string in the
// first argument; the URL form is what does URL-to-PDF.
using foxit;
using foxit.common;
using foxit.addon.conversion;
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;

        Convert.FromHTML(
            "https://www.example.com",
            @"C:\Foxit\html2pdf_engine",   // engine_path (separate download)
            "",                              // cookies path
            settingData,
            "output.pdf",
            30);                             // timeout (seconds)

        Library.Release();
    }
}
// NuGet: Install-Package Foxit.SDK.Dotnet
// Convert.FromHTML accepts either a URL or a literal HTML string in the
// first argument; the URL form is what does URL-to-PDF.
using foxit;
using foxit.common;
using foxit.addon.conversion;
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;

        Convert.FromHTML(
            "https://www.example.com",
            @"C:\Foxit\html2pdf_engine",   // engine_path (separate download)
            "",                              // cookies path
            settingData,
            "output.pdf",
            30);                             // timeout (seconds)

        Library.Release();
    }
}
Imports foxit
Imports foxit.common
Imports foxit.addon.conversion
Imports System

Module Program
    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

        Convert.FromHTML(
            "https://www.example.com",
            "C:\Foxit\html2pdf_engine",   ' engine_path (separate download)
            "",                           ' cookies path
            settingData,
            "output.pdf",
            30)                          ' timeout (seconds)

        Library.Release()
    End Sub
End Module
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

IronPDF 内置的 Chromium 引擎可自动处理 JavaScript 执行、CSS 渲染和动态内容。 了解有关 URL 至 PDF 转换的更多信息。

示例 3:添加水印

之前(福昕 PDF SDK):

// NuGet: Install-Package Foxit.SDK.Dotnet
using foxit;
using foxit.common;
using foxit.pdf;
using System;

class Program
{
    static void Main()
    {
        Library.Initialize("sn", "key");

        using (PDFDoc doc = new PDFDoc("input.pdf"))
        {
            doc.Load("");

            WatermarkSettings settings = new WatermarkSettings();
            settings.flags = (int)Watermark.Flags.e_FlagASPageContents;
            settings.position = Position.e_PosCenter;
            settings.rotation = -45.0f;
            settings.opacity = 50;     // 0-100 in newer SDKs

            WatermarkTextProperties props = new WatermarkTextProperties();
            props.font = new Font(Font.StandardID.e_StdIDHelvetica);
            props.font_size = 48.0f;
            props.color = 0xFF0000;
            props.alignment = Alignment.e_AlignmentCenter;

            Watermark watermark = new Watermark(doc, "Confidential", props, settings);

            // No InsertToAllPages helper — iterate pages explicitly.
            for (int i = 0; i < doc.GetPageCount(); i++)
            {
                using (PDFPage page = doc.GetPage(i))
                {
                    watermark.InsertToPage(page);
                }
            }

            doc.SaveAs("output.pdf", (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
        }

        Library.Release();
    }
}
// NuGet: Install-Package Foxit.SDK.Dotnet
using foxit;
using foxit.common;
using foxit.pdf;
using System;

class Program
{
    static void Main()
    {
        Library.Initialize("sn", "key");

        using (PDFDoc doc = new PDFDoc("input.pdf"))
        {
            doc.Load("");

            WatermarkSettings settings = new WatermarkSettings();
            settings.flags = (int)Watermark.Flags.e_FlagASPageContents;
            settings.position = Position.e_PosCenter;
            settings.rotation = -45.0f;
            settings.opacity = 50;     // 0-100 in newer SDKs

            WatermarkTextProperties props = new WatermarkTextProperties();
            props.font = new Font(Font.StandardID.e_StdIDHelvetica);
            props.font_size = 48.0f;
            props.color = 0xFF0000;
            props.alignment = Alignment.e_AlignmentCenter;

            Watermark watermark = new Watermark(doc, "Confidential", props, settings);

            // No InsertToAllPages helper — iterate pages explicitly.
            for (int i = 0; i < doc.GetPageCount(); i++)
            {
                using (PDFPage page = doc.GetPage(i))
                {
                    watermark.InsertToPage(page);
                }
            }

            doc.SaveAs("output.pdf", (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
        }

        Library.Release();
    }
}
Imports foxit
Imports foxit.common
Imports foxit.pdf
Imports System

Module Program
    Sub Main()
        Library.Initialize("sn", "key")

        Using doc As New PDFDoc("input.pdf")
            doc.Load("")

            Dim settings As New WatermarkSettings()
            settings.flags = CInt(Watermark.Flags.e_FlagASPageContents)
            settings.position = Position.e_PosCenter
            settings.rotation = -45.0F
            settings.opacity = 50 ' 0-100 in newer SDKs

            Dim props As New WatermarkTextProperties()
            props.font = New Font(Font.StandardID.e_StdIDHelvetica)
            props.font_size = 48.0F
            props.color = &HFF0000
            props.alignment = Alignment.e_AlignmentCenter

            Dim watermark As New Watermark(doc, "Confidential", props, settings)

            ' No InsertToAllPages helper — iterate pages explicitly.
            For i As Integer = 0 To doc.GetPageCount() - 1
                Using page As PDFPage = doc.GetPage(i)
                    watermark.InsertToPage(page)
                End Using
            Next

            doc.SaveAs("output.pdf", CInt(PDFDoc.SaveFlags.e_SaveFlagNoOriginal))
        End Using

        Library.Release()
    End Sub
End Module
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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

            Convert.FromHTML(
                "https://www.example.com",
                @"C:\Foxit\html2pdf_engine",  // engine_path (separate download)
                "",                            // cookies path
                settings,
                "webpage.pdf",
                30);                           // timeout (seconds)
        }
        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

            Convert.FromHTML(
                "https://www.example.com",
                @"C:\Foxit\html2pdf_engine",  // engine_path (separate download)
                "",                            // cookies path
                settings,
                "webpage.pdf",
                30);                           // timeout (seconds)
        }
        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

            Convert.FromHTML(
                "https://www.example.com",
                "C:\Foxit\html2pdf_engine",  ' engine_path (separate download)
                "",                          ' cookies path
                settings,
                "webpage.pdf",
                30)                          ' timeout (seconds)
        Finally
            Library.Release()
        End Try
    End Sub
End Class
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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("");

                // Build the encryption data (cipher, key length, permissions)
                using (StdEncryptData encryptData = new StdEncryptData(
                    true,                                    // is_encrypt_metadata
                    (int)(PDFDoc.UserPermissions.e_PermPrint |
                          PDFDoc.UserPermissions.e_PermModify),
                    SecurityHandler.CipherType.e_CipherAES,
                    16))                                     // key length (bytes) -> AES-128
                using (StdSecurityHandler securityHandler = new StdSecurityHandler())
                {
                    securityHandler.Initialize(encryptData, "user_password", "owner_password");
                    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("");

                // Build the encryption data (cipher, key length, permissions)
                using (StdEncryptData encryptData = new StdEncryptData(
                    true,                                    // is_encrypt_metadata
                    (int)(PDFDoc.UserPermissions.e_PermPrint |
                          PDFDoc.UserPermissions.e_PermModify),
                    SecurityHandler.CipherType.e_CipherAES,
                    16))                                     // key length (bytes) -> AES-128
                using (StdSecurityHandler securityHandler = new StdSecurityHandler())
                {
                    securityHandler.Initialize(encryptData, "user_password", "owner_password");
                    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("")

                ' Build the encryption data (cipher, key length, permissions)
                Using encryptData As New StdEncryptData(True, CType(PDFDoc.UserPermissions.e_PermPrint Or PDFDoc.UserPermissions.e_PermModify, Integer), SecurityHandler.CipherType.e_CipherAES, 16) ' key length (bytes) -> AES-128
                    Using securityHandler As New StdSecurityHandler()
                        securityHandler.Initialize(encryptData, "user_password", "owner_password")
                        doc.SetSecurityHandler(securityHandler)
                    End Using
                End Using

                doc.SaveAs("encrypted.pdf", CType(PDFDoc.SaveFlags.e_SaveFlagNoOriginal, Integer))
            End Using
        Finally
            Library.Release()
        End Try
    End Sub
End Class
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

性能考虑

重用 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
$vbLabelText   $csharpLabel

单元转换助手

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
$vbLabelText   $csharpLabel

正确的资源处置

// 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
$vbLabelText   $csharpLabel

故障排除

问题 1:Library.Initialize() 未找到

问题: Library.Initialize()在IronPDF中不存在。

解决方案:IronPDF使用了一种更简单的初始化模式:

// Foxit PDF
Library.Initialize(sn, key);

//IronPDF- just set license key once at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Foxit PDF
Library.Initialize(sn, key);

//IronPDF- just set license key once at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
$vbLabelText   $csharpLabel

问题 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
$vbLabelText   $csharpLabel

问题 3:PDFDoc.Close() 未找到

问题: doc.Close()方法在IronPDF中不存在。

解决方案:使用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
$vbLabelText   $csharpLabel

迁移清单

迁移前

  • 清点所有已使用的福昕 PDF SDK功能
  • 文档许可证密钥位置
  • 记录所有Library.Release()调用
  • 列出自定义设置(页面大小、边距等)
  • 使用 ErrorCode 识别错误处理模式
  • 将项目备份到版本控制系统
  • 获取IronPDF许可证密钥

软件包迁移

  • 从 .csproj 文件中移除福昕 PDF SDKDLL 引用
  • 删除所有私有 NuGet 源配置
  • 安装IronPDF NuGet包: dotnet add package IronPdf
  • 更新命名空间导入
  • 在启动时设置IronPDF许可证密钥

代码迁移

  • 移除Library.Release()调用
  • 用try/catch替换ErrorCode检查
  • PDFDoc
  • Convert.FromHTML(...)
  • 更新页面访问从Pages[i]
  • SaveAs(path, flags)
  • Close()
  • 更新水印代码以使用TextStamper
  • 将单位从点转换为毫米

测试

  • 验证 HTML 转 PDF 输出是否符合预期
  • 测试 PDF 加载和文本提取
  • 验证合并功能
  • 检查水印外观
  • 测试安全/加密功能
  • 验证表单字段操作
  • 性能测试

后迁移

  • 删除福昕 PDF SDKDLL 文件
  • 删除与 Foxit 相关的配置文件
  • 更新文档
  • 清理未使用的辅助代码

请注意Foxit PDF SDK 和 Foxit 是 Foxit Software Incorporated 的注册商标。 本网站与Foxit Software无关、未获得其认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映撰写时公开可用的信息。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我