フッターコンテンツにスキップ
移行ガイド

C#でiTextからIronPDFに移行する方法

iTextからIronPDFへの移行は、.NET PDFワークフローをParagraph, Table,Cellオブジェクトの手作業を必要とするプログラム的APIから、CSS3とJavaScriptを完全にサポートするモダンなHTMLファーストのアプローチに変えます。 このガイドでは、プロの.NET開発者向けに、AGPLライセンスに関する懸念や、pdfHTMLアドオンを別途用意する必要性を排除した、包括的でステップバイステップの移行パスを提供します。

なぜiTextからIronPDFに移行するのか

AGPLライセンスの罠

iTextは、商用アプリケーションに深刻な法的リスクとビジネスリスクをもたらします:

  1. AGPL ウイルス ライセンス: Web アプリケーションで iText を使用する場合、AGPL では、PDF コードだけでなく、コードベース全体を含むアプリケーション全体をオープンソース化する必要があります。 これは、ほとんどの商用ソフトウェアでは不可能です。

2.永久ライセンスなし: iText では永久ライセンスが廃止され、年間サブスクリプションの更新が強制され、総所有コストが増加します。

  1. pdfHTML アドオンのコスト: HTML から PDF への機能には、基本ライセンスに加えて追加料金で別売りされている pdfHTML アドオンが必要です。

4.複雑なライセンス監査:企業での導入では、ライセンスの複雑さと監査リスクが伴い、プロジェクトの遅延や法的リスクが生じる可能性があります。

5.プログラム専用 API: iText では、<コード>段落</コード、<コード>表</コード、 Cellオブジェクトを使用して手動で低レベルの PDF を構築する必要があります。これは、複雑なレイアウトでは面倒でエラーが発生しやすくなります。

6.最新の Web レンダリングの制限: pdfHTML を使用した場合でも、複雑な CSS およびJavaScriptコンテンツには、かなりの追加作業が必要です。

iTextとIronPDFの比較

フィーチャーiText 7 / iTextSharpIronPDF
ライセンス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のスキルを使うのです。


始める前に

前提条件

  1. .NET 環境: .NET Framework 4.6.2+ または .NET Core 3.1+ / .NET 5/6/7/8/9+
  2. NuGetアクセス: NuGetパッケージをインストールする機能
  3. 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
SHELL

ライセンス構成

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

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" .
SHELL

完全な 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);
        }
    }
}
$vbLabelText   $csharpLabel

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

iTextのアプローチでは、別個のiText.Html2pdfパッケージ(pdfHTMLアドオン、別売り)を必要とし、FileStreamを作成し、適切な処理のためにusingステートメントですべてを包みます。 HtmlConverter.ConvertToPdf()メソッドはストリームに直接書き込みます。

IronPDFのアプローチはすっきりしています: ChromePdfRendererを作成し、HTML文字列でRenderHtmlAsPdf()を呼び出し、結果のPdfDocumentSaveAs()を呼び出します。 別パッケージ、ストリーム管理は不要で、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());
                }
            }
        }
    }
}
$vbLabelText   $csharpLabel

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

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

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

この例は、パラダイムシフトを最も明確に示しています。 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 lines
$vbLabelText   $csharpLabel

IronPDFは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);
$vbLabelText   $csharpLabel

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

課題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);
$vbLabelText   $csharpLabel

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

移行チェックリスト

移行前

  • コードベース内のすべての 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 コードをアーカイブする

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。