How to Convert Images to a PDF in C#
Foxit PDF SDK からIronPDFに移行すると、複雑なエンタープライズ向けの API が最新の開発者向けパターンに置き換えられ、 .NET PDF 生成ワークフローが簡素化されます。 このガイドでは、不要なコードを削除し、コードベース全体で PDF 操作を容易にする、完全なステップバイステップの移行パスを提供します。
なぜFoxit PDF SDKからIronPDFに移行するのですか?
フォクシットPDFの課題
Foxit PDF SDK は強力なエンタープライズ レベルのライブラリですが、非常に複雑なため、開発が遅くなる可能性があります。
1.複雑なライセンス システム:複数の製品、SKU、ライセンス タイプ (開発者ごと、サーバーごと、OEM など) があるため、プロジェクトに適切なオプションを選択するのが困難です。
2.エンタープライズ価格設定:価格は大規模な組織向けに調整されており、小規模なチームや個人の開発者にとっては高額になる可能性があります。
-
重いネイティブパッケージ: 公開NuGetパッケージ (
Foxit.SDK.Dotnet)は大きく(約240 MB)、古いプロジェクトでは直接DLL参照やプライベートフィード設定を保持しているかもしれません。 -
冗長なAPI:
Library.Release()呼び出しにより、全ての操作に定型文が追加されます。 -
別々のHTML2PDFエンジン: HTMLからPDFへの変換にはHTML2PDFエンジンのバイナリが必要で、NuGetパッケージにバンドルされるのではなくFoxitサポート/販売から別に配布されます。
-
複雑な設定: 設定には詳細なオブジェクト設定(例:
HTML2PDFSettingData)と複数のプロパティが必要です。 - C++ の伝統: API パターンは C++ の起源を反映しており、最新の C# アプリケーションでは自然な感じがしません。
Foxit PDFとIronPDFの比較
| アスペクト | Foxit PDF SDK | IronPDF |
|---|---|---|
| インストール | Foxit.SDK.Dotnet (~240 MB) + HTML2PDFエンジン分離 |
シンプルなNuGetパッケージ |
| ライセンス | 販売主導、開発者毎プラットフォーム毎 | 透明で、あらゆるサイズに対応 |
| 初期設定 | Library.Initialize(sn, key) |
ライセンスキーを一度設定 |
| エラー処理 | エラーコード列挙型 | .NET Standardの例外 |
| HTMLからPDFへ | 別々のエンジンのダウンロード | 内蔵Chromiumエンジン |
| APIスタイル | C#の遺産、冗長性 | 最新 for .NETパターン |
| リソースのクリーンアップ | 手動Release() |
IDisposable/automatic |
| ドキュメンテーション | エンタープライズドキュメントポータル | 公開チュートリアル。 |
費用便益分析
Foxit PDFからIronPDFへの移行は具体的な開発上の利点を提供します。シンプルなAPIを通じた複雑さの削減、直感的なメソッドを活用した高速な開発、非同期/待機およびLINQサポートを含むモダンな.NETの互換性、既存のWebスキルを利用するHTMLファーストアプローチ、および別々のエンジンダウンロード不要のHTML変換が含まれます。 IronPDFは最新 for .NETバージョンで動作し、モダンなC#パターンとスムーズに組み合わせることができます。
始める前に
前提条件
- .NET環境: IronPDFは.NET Framework 4.6.2以降、 .NET Core 3.1以降、 .NET 5/6/7/8/9以降をサポートしています。
- NuGetアクセス: NuGetからパッケージをインストールできることを確認する 3.ライセンスキー: IronPDFから本番環境で使用する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"
すべての 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"
ドキュメント現在の機能
移行前のカタログ
- 使用している Foxit 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
古い直接的な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>
また、個別に解凍された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
ステップ3: IronPDFを初期化する
このFoxit PDFの移行における最も重要な改善点の1つは、複雑な初期化とクリーンアップのパターンをなくしたことです:
// 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
ステップ 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")
完全な API リファレンス
名前空間マッピング
| Foxit PDF 名前空間 | IronPDF 同等物 |
|---|---|
foxit |
IronPdf |
foxit.common |
IronPdf |
foxit.common.fxcrt |
該当なし |
foxit.pdf |
IronPdf |
foxit.pdf.annots |
IronPdf.Editing |
foxit.addon.conversion |
IronPdf.Rendering |
コア クラス マッピング
| Foxit 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のメソッド
| Foxit 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() またはusingステートメント |
doc.InsertDocument() |
PdfDocument.Merge() |
HTML2PDF / 変換
| Foxit 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) |
ウォーターマーク設定
| Foxit ウォーターマーク | 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への変換
以前(Foxit PDF SDK):
// NuGet: Install-Package Foxit.SDK.Dotnet
// HTML-to-PDF requires the separate Foxit HTML2PDF engine (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 Foxit HTML2PDF engine (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
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
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 Class
翻訳後(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 documentationを参照してください。
例2: URLからPDFへの変換
以前(Foxit 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
翻訳後(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: 透かしを追加する
以前(Foxit 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
翻訳後(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 documentationの全文をご覧ください。
例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
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;
// Foxit 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();
}
}
}
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
' Foxit 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
翻訳後(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 のセキュリティと暗号化
以前(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("");
// 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
翻訳後(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) 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
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-LICENSE-KEY";
// Foxit PDF
Library.Initialize(sn, key);
// IronPDF - just set license key once at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
課題2: エラーコード処理
問題: コードはErrorCode.e_ErrSuccessをチェックしますが、IronPDFにはこれがありません。
解決策:標準 for .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()メソッドがありません。
解決策: 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
移行チェックリスト
移行前
- 使用されているすべてのFoxit PDF SDK機能をインベントリします
- ドキュメントのライセンスキーの場所
- すべての
Library.Release()呼び出しを記録する - カスタム設定(ページ サイズ、余白など)を一覧表示します
- ErrorCode を使用してエラー処理パターンを識別する
- バージョン管理にプロジェクトをバックアップする
- IronPDFライセンスキーを取得する
パッケージの移行
- .csproj からFoxit PDF SDKDLL 参照を削除します
- プライベートNuGetフィード構成を削除します
-IronPDFNuGetパッケージをインストールする:
dotnet add package IronPdf - 名前空間のインポートを更新する
- 起動時にIronPDFライセンスキーを設定する
コードの移行
Library.Release()呼び出しを削除するErrorCodeチェックをtry/catchに置き換えるPdfDocumentに置き換えるChromePdfRendererに置き換える- ページアクセスを
Pages[i]に更新する SaveAs(path)に置き換えるDispose()またはusingステートメントに置き換える- ウォーターマークコードを
TextStamperを使用するように更新する - 単位をポイントからミリメートルに変換する
テスティング
- HTMLからPDFへの出力が期待通りであることを確認する
- PDFの読み込みとテキスト抽出をテストする
- マージ機能の検証
- 透かしの外観を確認する
- セキュリティ/暗号化機能をテストする
- フォームフィールドの操作を検証する
- パフォーマンステスト
移行後
-Foxit PDF SDKDLLを削除する
- Foxit関連の設定ファイルを削除する
- ドキュメントの更新
- 未使用のヘルパーコードをクリーンアップする

