C#でiTextからIronPDFに移行する方法
iTextからIronPDFへの移行は、.NET PDFワークフローをParagraph, Table,Cellオブジェクトの手作業を必要とするプログラム的APIから、CSS3とJavaScriptを完全にサポートするモダンなHTMLファーストのアプローチに変えます。 このガイドでは、プロの.NET開発者向けに、AGPLライセンスに関する懸念や、pdfHTMLアドオンを別途用意する必要性を排除した、包括的でステップバイステップの移行パスを提供します。
なぜiTextからIronPDFに移行するのか
AGPLライセンスの罠
iTextは、商用アプリケーションに深刻な法的リスクとビジネスリスクをもたらします:
- AGPL ウイルス ライセンス: Web アプリケーションで iText を使用する場合、AGPL では、PDF コードだけでなく、コードベース全体を含むアプリケーション全体をオープンソース化する必要があります。 これは、ほとんどの商用ソフトウェアでは不可能です。
2.永久ライセンスなし: iText では永久ライセンスが廃止され、年間サブスクリプションの更新が強制され、総所有コストが増加します。
- pdfHTML アドオンのコスト: HTML から PDF への機能には、基本ライセンスに加えて追加料金で別売りされている pdfHTML アドオンが必要です。
4.複雑なライセンス監査:企業での導入では、ライセンスの複雑さと監査リスクが伴い、プロジェクトの遅延や法的リスクが生じる可能性があります。
5.プログラム専用 API: iText では、<コード>段落</コード、<コード>表</コード、 Cellオブジェクトを使用して手動で低レベルの PDF を構築する必要があります。これは、複雑なレイアウトでは面倒でエラーが発生しやすくなります。
6.最新の Web レンダリングの制限: pdfHTML を使用した場合でも、複雑な CSS およびJavaScriptコンテンツには、かなりの追加作業が必要です。
iTextとIronPDFの比較
| フィーチャー | iText 7 / iTextSharp | IronPDF |
|---|---|---|
| ライセンス | AGPL(バイラル)または高価なサブスクリプション | 商用、永久オプション |
| HTMLからPDFへ | pdfHTMLアドオン | 内蔵Chromiumレンダラー |
| CSSサポート | 基本的なCSS | フルCSS3、フレックスボックス、グリッド |
| JavaScript | なし | 完全な実行 |
| APIパラダイム | プログラム(段落、表、セル) | CSSによるHTMLファースト |
| 学習曲線 | スティープ(PDF座標系) | ウェブ開発者フレンドリー |
| オープンソースリスク | オープンソースのウェブアプリケーションが必要 | ウイルス要件なし |
| 価格モデル | サブスクリプションのみ | 永久またはサブスクリプション |
2025年、2026年まで.NET 10とC# 14の導入を計画しているチームにとって、IronPDFはすでにチームが持っているウェブ開発スキルを活用するHTMLファーストのアプローチで、将来を見据えた基盤を提供します。
マイグレーションの複雑さの評価
機能別の見積もり作業
| フィーチャー | 移行の複雑さ | ノート |
|---|---|---|
| HTMLからPDFへ | 低レベル | メソッドの直接置換 |
| PDFのマージ | 低レベル | よりシンプルなAPI |
| テキストと画像 | 低レベル | HTMLはプログラム的なものに取って代わる |
| 表 | 中規模 | HTMLテーブルがiTextテーブルクラスを置き換える |
| ヘッダー/フッター | 中規模 | イベントハンドラ → HTMLテンプレート |
| セキュリティ/暗号化 | 低レベル | プロパティベースのAPI |
パラダイムシフト
この iText 移行における基本的な変化は、プログラムによる PDF 構築からHTML ファーストのレンダリングへの変化です。
iText: PdfWriter → PdfDocument → Document → Add(Paragraph) → Add(Table)
IronPDF: ChromePdfRenderer → RenderHtmlAsPdf(htmlString) → SaveAs()iTextのオブジェクトモデルを学ぶ代わりに、ウェブ開発者がすでに持っているHTMLとCSSのスキルを使うのです。
始める前に
前提条件
- .NET 環境: .NET Framework 4.6.2+ または .NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGetアクセス: NuGetパッケージをインストールする機能
- IronPDFライセンス: ironpdf.comからライセンスキーを取得します。
NuGetパッケージの変更
# Remove iText packages
dotnet remove package itext7
dotnet remove package itext7.pdfhtml
dotnet remove package itextsharp
# Install IronPDF
dotnet add package IronPdf# Remove iText packages
dotnet remove package itext7
dotnet remove package itext7.pdfhtml
dotnet remove package itextsharp
# Install IronPDF
dotnet add package IronPdfライセンス構成
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";iTextの使用方法を確認する
# Find all iText references
grep -r "using iText\|using iTextSharp" --include="*.cs" .
grep -r "PdfWriter\|PdfDocument\|Document\|Paragraph\|Table\|Cell" --include="*.cs" .
grep -r "HtmlConverter\|ConverterProperties" --include="*.cs" .# Find all iText references
grep -r "using iText\|using iTextSharp" --include="*.cs" .
grep -r "PdfWriter\|PdfDocument\|Document\|Paragraph\|Table\|Cell" --include="*.cs" .
grep -r "HtmlConverter\|ConverterProperties" --include="*.cs" .完全な API リファレンス
クラスマッピング
| iText 7 クラス | iTextSharpクラス | IronPDF 同等物 |
|---|---|---|
| <コード>PdfWriter</コード | <コード>PdfWriter</コード | <コード>ChromePdfRenderer</コード |
| <コード>PdfDocument</コード | <コード>ドキュメント</コード | <コード>PdfDocument</コード |
| <コード>ドキュメント</コード | <コード>ドキュメント</コード | ChromePdfRenderer.RenderHtmlAsPdf()のようになります。 |
| <コード>段落</コード | <コード>段落</コード | HTML <p>、<h1>など。 |
| <コード>表</コード | <コード>PdfPTable</コード | HTML <コード><テーブル><コード></コード |
| <コード>セル</コード | <コード>PdfPCell</コード | HTML <td>、<th>。 |
| <コード>イメージ</コード | <コード>イメージ</コード | HTML <img> |
| <コード>PdfReader</コード | <コード>PdfReader</コード | PdfDocument.FromFile()を使用してください。 |
| <コード>PdfMerger</コード | 該当なし | PdfDocument.Merge()を使用してください。 |
名前空間マッピング
| iText 7 名前空間 | IronPDF 同等物 |
|---|---|
iText.Kernel.Pdf(英語 | IronPdf(アイアンPDF |
| <コード>iText.Layout</コード | IronPdf(アイアンPDF |
| <コード>iText.Layout.Element</コード | HTML要素の使用 |
iText.Html2Pdf(英語 | <コード>IronPdf</コード> (組み込み) |
| <コード>iText.IO.Image</コード | HTML <img>を使用してください。 |
iText.Kernel.Utils(英語 | IronPdf(アイアンPDF |
コード移行の例
例1: HTMLからPDFへの変換
導入前(iText 7):
// NuGet: Install-Package itext7
using iText.Html2pdf;
using System.IO;
class Program
{
static void Main()
{
string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>";
string outputPath = "output.pdf";
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
HtmlConverter.ConvertToPdf(html, fs);
}
}
}// NuGet: Install-Package itext7
using iText.Html2pdf;
using System.IO;
class Program
{
static void Main()
{
string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>";
string outputPath = "output.pdf";
using (FileStream fs = new FileStream(outputPath, FileMode.Create))
{
HtmlConverter.ConvertToPdf(html, fs);
}
}
}翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}iTextのアプローチでは、別個のiText.Html2pdfパッケージ(pdfHTMLアドオン、別売り)を必要とし、FileStreamを作成し、適切な処理のためにusingステートメントですべてを包みます。 HtmlConverter.ConvertToPdf()メソッドはストリームに直接書き込みます。
IronPDFのアプローチはすっきりしています: ChromePdfRendererを作成し、HTML文字列でRenderHtmlAsPdf()を呼び出し、結果のPdfDocumentでSaveAs()を呼び出します。 別パッケージ、ストリーム管理は不要で、Chromiumレンダリングエンジンは優れたCSS3とJavaScriptをサポートします。 その他のレンダリングオプションについては、HTML to PDF documentationを参照してください。
例2: 複数のPDFをマージする
導入前(iText 7):
// NuGet: Install-Package itext7
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using System.IO;
class Program
{
static void Main()
{
string outputPath = "merged.pdf";
string[] inputFiles = { "document1.pdf", "document2.pdf", "document3.pdf" };
using (PdfWriter writer = new PdfWriter(outputPath))
using (PdfDocument pdfDoc = new PdfDocument(writer))
{
PdfMerger merger = new PdfMerger(pdfDoc);
foreach (string file in inputFiles)
{
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file)))
{
merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
}
}
}
}
}// NuGet: Install-Package itext7
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using System.IO;
class Program
{
static void Main()
{
string outputPath = "merged.pdf";
string[] inputFiles = { "document1.pdf", "document2.pdf", "document3.pdf" };
using (PdfWriter writer = new PdfWriter(outputPath))
using (PdfDocument pdfDoc = new PdfDocument(writer))
{
PdfMerger merger = new PdfMerger(pdfDoc);
foreach (string file in inputFiles)
{
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file)))
{
merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
}
}
}
}
}翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdfDocuments = new List<PdfDocument>
{
PdfDocument.FromFile("document1.pdf"),
PdfDocument.FromFile("document2.pdf"),
PdfDocument.FromFile("document3.pdf")
};
var merged = PdfDocument.Merge(pdfDocuments);
merged.SaveAs("merged.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdfDocuments = new List<PdfDocument>
{
PdfDocument.FromFile("document1.pdf"),
PdfDocument.FromFile("document2.pdf"),
PdfDocument.FromFile("document3.pdf")
};
var merged = PdfDocument.Merge(pdfDocuments);
merged.SaveAs("merged.pdf");
}
}iText のマージ操作では、出力用に<コード>PdfWriter</コードを作成し、それを<コード>PdfDocument</コードでラップし、PdfMerger を作成し、各<コード>PdfDocument</コードと<コード>PdfReader</コードに対して入れ子になった using ステートメントでソース ファイルを繰り返し処理するという、かなりの定型文が必要です。 また、merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages())でページ範囲を指定する必要があります。
IronPDFはこれを3つのステップに減らします: PdfDocument.FromFile()でドキュメントをロードし、静的なPdfDocument.Merge()メソッドをリストで呼び出し、保存します。 マージ作業全体が読みやすく、保守しやすくなります。 PDFのマージと分割については、こちらをご覧ください。
例3: テキストと画像でPDFを作成する
導入前(iText 7):
// NuGet: Install-Package itext7
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
class Program
{
static void Main()
{
string outputPath = "document.pdf";
using (PdfWriter writer = new PdfWriter(outputPath))
using (PdfDocument pdf = new PdfDocument(writer))
using (Document document = new Document(pdf))
{
document.Add(new Paragraph("Sample PDF Document"));
document.Add(new Paragraph("This document contains text and an image."));
Image img = new Image(ImageDataFactory.Create("image.jpg"));
img.SetWidth(200);
document.Add(img);
}
}
}// NuGet: Install-Package itext7
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
class Program
{
static void Main()
{
string outputPath = "document.pdf";
using (PdfWriter writer = new PdfWriter(outputPath))
using (PdfDocument pdf = new PdfDocument(writer))
using (Document document = new Document(pdf))
{
document.Add(new Paragraph("Sample PDF Document"));
document.Add(new Paragraph("This document contains text and an image."));
Image img = new Image(ImageDataFactory.Create("image.jpg"));
img.SetWidth(200);
document.Add(img);
}
}
}翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Sample PDF Document</h1>
<p>This document contains text and an image.</p>
<img src='image.jpg' width='200' />";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("document.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Sample PDF Document</h1>
<p>This document contains text and an image.</p>
<img src='image.jpg' width='200' />";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("document.pdf");
}
}この例は、パラダイムシフトを最も明確に示しています。 iTextが必要です:
- トリプルネストの
usingステートメント (PdfWriter,PdfDocument,Document) new Paragraph()で各テキスト要素にParagraphオブジェクトを作成する。ImageDataFactory.Create()を使用して画像を読み込むImageオブジェクトを作成し、SetWidth()を個別に呼び出します。- 各要素に対して
document.Add()を呼び出します。
IronPDFは標準的なHTMLを使用します:見出しには<h1>、段落には<p>、画像には<img>(width属性付き)を使用します。 ウェブ開発者は既存のスキルをすぐに活用でき、デザイナーはすでに知っているCSSを使ってドキュメントをスタイリングできます。
重要な移行に関する注意事項
パラダイムシフト:プログラムからHTMLファーストへ
このiTextの移行で最も大きな変更は、概念的なものです。 iTextはプログラムでPDFを作成します:
// iText approach
document.Add(new Paragraph("Title")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(24)
.SetBold());
var table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();
table.AddHeaderCell(new Cell().Add(new Paragraph("ID")));
table.AddHeaderCell(new Cell().Add(new Paragraph("Name")));
// ... many more lines// iText approach
document.Add(new Paragraph("Title")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(24)
.SetBold());
var table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();
table.AddHeaderCell(new Cell().Add(new Paragraph("ID")));
table.AddHeaderCell(new Cell().Add(new Paragraph("Name")));
// ... many more linesIronPDFはHTMLとCSSを使用しています:
//IronPDFapproach
string html = @"
<style>
h1 { text-align: center; font-size: 24px; font-weight: bold; }
table { width: 100%; border-collapse: collapse; }
th { background-color: #4CAF50; color: white; padding: 8px; }
</style>
<h1>Title</h1>
<table>
<tr><th>ID</th><th>Name</th></tr>
</table>";
var pdf = renderer.RenderHtmlAsPdf(html);//IronPDFapproach
string html = @"
<style>
h1 { text-align: center; font-size: 24px; font-weight: bold; }
table { width: 100%; border-collapse: collapse; }
th { background-color: #4CAF50; color: white; padding: 8px; }
</style>
<h1>Title</h1>
<table>
<tr><th>ID</th><th>Name</th></tr>
</table>";
var pdf = renderer.RenderHtmlAsPdf(html);AGPLライセンスは廃止されました。
iTextのAGPLライセンスでは、Webアプリケーション全体をオープンソース化するか、高価な商用ライセンスを購入する必要があります。 IronPdfの商用ライセンスにより、ウイルスライセンス要件なしにプロプライエタリなソフトウェアに導入することができます。
pdfHTMLアドオンは必要ありません。
iTextはHTMLからPDFへの変換のためにpdfHTMLアドオンが必要です。IronPdfにはChromiumベースのHTMLレンダリング機能が含まれています。
メソッド置換パターン
| iTextパターン | IronPDFの置き換え | |
|---|---|---|
SetTextAlignment(TextAlignment.CENTER)を設定してください。 | CSS <コード>text-align: center | |
SetFontSize(24)を設定してください。 | CSS フォントサイズ: 24px | |
| <コード>SetBold()</コード | CSS <コード>font-weight: bold | |
new Table(3). | HTML <コード><テーブル><コード></コード | |
AddHeaderCell(新しいCell().Add(新しいParagraph()))を追加します。 | HTML <コード> | </コード |
AddCell(新しいCell().Add(新しいParagraph()))。 | HTML <td> |
トラブルシューティング
問題 1: PdfWriter/Document パターン
問題:コードでは、<コード>PdfWriter</コード→<コード>PdfDocument</コード→ Documentネスト パターンが使用されています。
解決策: ChromePdfRendererに置き換えます:
// Delete this iText pattern:
// using (var writer = new PdfWriter(outputPath))
// using (var pdfDoc = new PdfDocument(writer))
// using (var document = new Document(pdfDoc))
// Replace with:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);// Delete this iText pattern:
// using (var writer = new PdfWriter(outputPath))
// using (var pdfDoc = new PdfDocument(writer))
// using (var document = new Document(pdfDoc))
// Replace with:
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);課題2:HtmlConverterが見つかりません
問題:コードは、pdfHTML アドオンを必要とするiText.Html2pdf.HtmlConverterを使用します。
解決策:IronPDFの組み込み HTML レンダリングを使用します。
// iText (requires pdfHTML add-on)
HtmlConverter.ConvertToPdf(html, fileStream);
//IronPDF(built-in)
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);// iText (requires pdfHTML add-on)
HtmlConverter.ConvertToPdf(html, fileStream);
//IronPDF(built-in)
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs(outputPath);問題 3: PdfMerger の複雑さ
問題: iText のPdfMergerでは、ネストされたリーダーとページ範囲の指定が必要です。
解決策:IronPDFの静的マージメソッドを使用します。
// iText merger pattern (delete this)
// using (PdfDocument pdfDoc = new PdfDocument(writer))
// {
// PdfMerger merger = new PdfMerger(pdfDoc);
// foreach (string file in inputFiles)
// {
// using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file)))
// {
// merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
// }
// }
// }
//IronPDF(simple)
var merged = PdfDocument.Merge(pdfDocuments);
merged.SaveAs("merged.pdf");// iText merger pattern (delete this)
// using (PdfDocument pdfDoc = new PdfDocument(writer))
// {
// PdfMerger merger = new PdfMerger(pdfDoc);
// foreach (string file in inputFiles)
// {
// using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file)))
// {
// merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
// }
// }
// }
//IronPDF(simple)
var merged = PdfDocument.Merge(pdfDocuments);
merged.SaveAs("merged.pdf");移行チェックリスト
移行前
- コードベース内のすべての iText API 呼び出しをインベントリする
- プログラムによる PDF 構築パターン (段落、表、セル) を識別する
- ドキュメント HtmlConverter の使用法 (pdfHTML アドオン)
- AGPLコンプライアンスリスクを評価する
- IronPDFライセンスキーを取得する
コードの移行
- iText NuGet パッケージを削除します:
dotnet remove package itext7 - IronPdf NuGet パッケージをインストールします:
dotnet add package IronPdf - 名前空間のインポートを更新します (
using iText.*→using IronPdf) -<コード>PdfWriter</コード/DocumentパターンをChromePdfRendererに置き換えます -<コード>段落</コード/<コード>表</コード/<コード>セル</コードHTML要素に変換する HtmlConverter.ConvertToPdf()をRenderHtmlAsPdf()に置き換えます- マージ操作を
PdfDocument.Merge()に更新します - 起動時にライセンスキーの初期化を追加
テスティング
- すべてのPDF生成パスをテストする
- 視覚的な出力が期待通りであることを確認する
- 複雑なHTML/CSSコンテンツでテストする
- ベンチマークパフォーマンス
移行後
- iTextのライセンスファイルと参照を削除します
- ドキュメントの更新
- iTextサブスクリプションをキャンセルする(該当する場合)
- レガシー iText コードをアーカイブする






