跳過到頁腳內容
MIGRATION GUIDES

Migrate from Foxit PDF SDK to IronPDF: (.NET Guide)

從 Foxit PDF SDK 遷移到 IronPDF 可以簡化您的 .NET PDF 生成工作流程,因為它用現代化的、對開發人員友好的模式取代了複雜的、面向企業的 API。 本指南提供了完整的、逐步的遷移路徑,可以刪除不必要的程式碼,並簡化整個程式碼庫中的 PDF 操作。

為什麼要從 Foxit PDF SDK 遷移到 IronPDF

Foxit PDF挑戰賽

Foxit PDF SDK 是一個功能強大的企業級函式庫,但它也存在相當大的複雜性,可能會減慢開發速度:

1.複雜的授權系統:多種產品、SKU 和授權類型(按開發者、按伺服器、OEM 等)使得為您的專案選擇合適的選項變得困難。

2.企業定價:定價是為大型組織量身定制的,對於較小的團隊或個人開發人員來說可能難以承受。

3.手動安裝: Foxit PDF SDK 需要手動 DLL 引用或私有 NuGet 來源-沒有簡單的公共 NuGet 套件可用。

4.冗長的 API:使用Library.Initialize()進行函式庫初始化、錯誤代碼檢查以及明確呼叫Library.Release()會為每個操作增加大量的樣板程式碼。

5.單獨的 HTML 轉換外掛: HTML 到 PDF 的轉換需要額外購買外掛程式-它不包含在基本 SDK 中。

6.複雜配置:設定需要詳細的物件配置(例如, HTML2PDFSettingData ),具有多個屬性。

  1. C++ 傳承: API 模式反映了 C++ 的起源,在現代 C# 應用程式中感覺不太自然。

Foxit PDF 與 IronPDF 對比

方面 Foxit PDF SDK IronPDF
安裝 手動 DLL/私有源 簡單的 NuGet 套件
授權 複雜、以企業為中心的 透明,適合所有尺寸
初始化 Library.Initialize(sn, key) 設定一次許可證密鑰
錯誤處理 ErrorCode 枚舉 標準 .NET 異常
HTML 轉 PDF 單獨購買附加元件 內建鉻合金引擎
API 風格 C++ 傳承,冗長 現代 .NET 模式
資源清理 手動Close() / Release() 一次性/自動
文件 企業文件門戶 公開教學

成本效益分析

從 Foxit PDF 遷移到 IronPDF 可帶來切實的開發優勢:透過更簡單的 API 降低複雜性,透過直覺的方法加快開發速度,相容於現代 .NET,包括 async/await 和 LINQ 支持,採用 HTML 優先方法利用現有的 Web 技能,以及無需單獨購買附加元件即可獲得所有功能。 當您計劃於 2025 年和 2026 年採用 .NET 10 和 C# 14 時,IronPDF 為 PDF 生成提供了一個面向未來的基礎。


開始之前

先決條件

  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 Foxit PDF SDK to IronPDF migration"
# Create a backup branch
git checkout -b pre-ironpdf-migration
git add .
git commit -m "Backup before Foxit PDF SDK to IronPDF migration"
SHELL

識別所有 Foxit PDF 使用情況

# Find all Foxit PDF SDK references
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 Foxit PDF SDK references
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

文件當前功能

遷移前,目錄:

  • 您使用 Foxit PDF 的哪些功能(HTML 轉換、註解、表單、安全)?
  • 許可證金鑰位置和初始化程式碼
  • 自訂配置和設定
  • 使用 ErrorCode 列舉的錯誤處理模式

快速入門遷移

步驟 1:更新 NuGet 套件

# Foxit PDF SDK typically requires manual removal of DLL references
# Check your .csproj for Foxit references and remove them

# Install IronPDF
dotnet add package IronPdf
# Foxit PDF SDK typically requires manual removal of DLL references
# Check your .csproj for Foxit references and remove them

# Install IronPDF
dotnet add package IronPdf
SHELL

如果 .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>
XML

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

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

步驟 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");
$vbLabelText   $csharpLabel

完整 API 參考

命名空間映射

Foxit PDF 命名空間 IronPDF當量 筆記
foxit IronPdf 主命名空間
foxit.common IronPdf 常見類型
foxit.common.fxcrt 不適用 低級(非必需)
foxit.pdf IronPdf PDF文件操作
foxit.pdf.annots IronPdf.Editing 註解
foxit.addon.conversion IronPdf.Rendering HTML/圖片轉換

核心類別映射

Foxit PDF SDK 類 IronPDF當量 筆記
Library 不適用 IronPDF 自動管理
PDFDoc PdfDocument 主文檔類
PDFPage PdfDocument.Pages[i] 頁面訪問
HTML2PDF ChromePdfRenderer HTML轉換
TextPage pdf.ExtractTextFromPage(i) 文字擷取
Watermark TextStamper / ImageStamper 水印
Security SecuritySettings PDF 安全性
Form pdf.Form 表單字段
Metadata pdf.MetaData 文件元數據

PDFDoc 方法

Foxit PDFDoc IronPDF PDF文檔 筆記
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()或 using 語句 清理
doc.InsertDocument() PdfDocument.Merge() 合併文檔

HTML2PDF轉換

Foxit HTML2PDF IronPDF當量 筆記
new HTML2PDFSettingData() new ChromePdfRenderer() 建立渲染器
settings.page_width RenderingOptions.PaperSize 標準尺寸
settings.page_height RenderingOptions.SetCustomPaperSize() 客製尺寸
html2pdf.Convert(html, path) renderer.RenderHtmlAsPdf(html) HTML字串
html2pdf.ConvertFromURL(url, path) renderer.RenderUrlAsPdf(url) URL轉換

水印設定

Foxit浮水印 IronPDF當量 筆記
new Watermark(doc, text, font, size, color) new TextStamper() 文字浮水印
WatermarkSettings.position VerticalAlignment + HorizontalAlignment 位置
WatermarkSettings.rotation Rotation 旋轉角度
WatermarkSettings.opacity Opacity 0-100%
watermark.InsertToAllPages() pdf.ApplyStamp(stamper) 適用於所有

程式碼範例

範例 1:HTML 轉 PDF

(Foxit 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();
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

IronPDF 方法將 15 行以上的配置程式碼減少到僅 4 行。 無需庫初始化,無需明確清理,無需複雜的設定物件。 如需更多 HTML 渲染選項,請參閱HTML 轉 PDF 文件

範例 2:URL 轉 PDF

(Foxit 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();
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

IronPDF 內建的 Chromium 引擎可自動處理 JavaScript 執行、CSS 渲染和動態內容。 了解更多關於URL轉PDF的資訊

範例 3:添加浮水印

(Foxit 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();
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

IronPDF 的TextStamper提供基於屬性的直覺配置,而不是單獨的設定物件和手動頁面迭代。 有關更多選項,請參閱完整的水印文件

範例 4:帶有頁首和頁尾的 PDF 的 URL

(Foxit 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;

            // Foxit PDF SDK has 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;

            // Foxit PDF SDK has 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();
        }
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$vbLabelText   $csharpLabel

IronPDF 提供原生頁首和頁尾支持,支援 HTML 樣式和動態頁碼佔位符。

範例 5:PDF 安全性與加密

(Foxit 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();
        }
    }
}
$vbLabelText   $csharpLabel

(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");
    }
}
$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;
}
$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) to IronPDF millimeters
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) 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
$vbLabelText   $csharpLabel

故障排除

問題 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";
$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}");
}
$vbLabelText   $csharpLabel

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

遷移清單

遷移前

  • 清點所有已使用的 Foxit PDF SDK 功能
  • 文件許可證密鑰位置
  • 注意所有Library.Initialize()Library.Release()調用
  • 列出自訂設定(頁面大小、邊距等)
  • 使用 ErrorCode 識別錯誤處理模式
  • 將項目備份到版本控制系統
  • 取得 IronPDF 許可證密鑰

軟體包遷移

  • 從 .csproj 檔案移除 Foxit PDF SDK DLL 引用
  • 刪除所有私有 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()或 using 語句
  • 更新水印程式碼,使用TextStamper
  • 將單位從點轉換為毫米

測試

  • 驗證 HTML 轉 PDF 輸出是否符合預期
  • 測試 PDF 載入和文字擷取
  • 驗證合併功能
  • 檢查水印外觀
  • 測試安全/加密功能
  • 驗證表單欄位操作
  • 效能測試

移民後

  • 刪除 Foxit PDF SDK DLL 文件
  • 刪除與 Foxit 相關的設定檔
  • 更新文檔
  • 清理未使用的輔助程式碼

遷移。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。