フッターコンテンツにスキップ
製品比較

iTextsharp HTML to PDF With CSS Styles C# Example vs IronPDF

HTMLをPDFに変換することは、レポートや請求書の作成、ドキュメントの生成など、多くの現代のソフトウェア開発ワークフローで重要なタスクです。 C#開発者として、このプロセスを効率化するためのライブラリがいくつか利用可能です。

In this article, we'll compare two of the most popular libraries within the .NET ecosystem: IronPDF and iTextSharp. 両方のライブラリは強力な機能を提供しますが、使いやすさ、PDF作成ツール、CSSスタイリングのサポート、ライセンスなどの重要な点で異なります。 初心者であっても経験豊富な開発者であっても、このガイドはこれらのライブラリの核心的な機能を理解し、あなたのニーズやプロジェクトの要件に基づいてどちらが最適かを決定するのに役立ちます。

一緒に進行しますか? IronPDFの強力な機能を自分で探求するために、IronPDF無料トライアルをダウンロードしてください。

トップライブラリの比較:iTextSharpとIronPDF

iTextSharpIronPDFの両方が、C#でのHTMLからPDFへの変換に必要なツールを開発者に提供します。 ただし、それぞれに独自の強みと弱点があります。

  • iTextSharpは、長い歴史を持つオープンソースのライブラリです。柔軟性と広範なカスタマイズオプションを提供しますが、特に高度なHTMLとCSSのレンダリングを扱う際にはセットアップと使用がやや複雑になることがあります。 ただし、iTextSharpはレガシープロダクトであり、現在はセキュリティ関連の更新のみを受け取っています。

  • 一方、IronPDFは、Iron Softwareによって開発された商用製品です。 使いやすいインターフェース、堅牢なCSSサポート、使いやすさで知られています。 C#アプリケーションとのシームレスな統合を提供し、品質を犠牲にすることなく迅速で効率的なPDF生成が必要な開発者にとって最適な選択肢です。

これら2つのライブラリを使ってHTMLをPDFフォーマットに変換する方法を掘り下げる前に、まずこれらの2つのライブラリがCSSとJavaScriptを多用するウェブページ/HTMLコンテンツをPDFドキュメントに変換する際の違いを示す基本的な例を見てみましょう。

iTextSharp vs IronPDF URL to PDF

結果として得られたPDFドキュメントからわかるように、これはiTextSharpが提供されたURLの生HTMLコンテンツのみを処理できるのに対し、IronPDFは元のCSSレイアウトとスタイリングを維持でき、生成されたPDFドキュメントが元のウェブページに近似することを示しています。

ステップバイステップインストールガイド

iTextSharpのセットアップ

  1. NuGetコンソール経由でインストール: iTextSharpを使用し始めるには、NuGetから直接インストールできます。 Visual Studioプロジェクトを開き、NuGetパッケージマネージャーに移動し、次のコマンドを実行してください。
Install-Package iTextSharp

NuGetコンソール経由でのiTextSharpのインストール

  1. NuGetパッケージマネージャー経由でインストール: 代わりに、ソリューション画面のNuGetパッケージマネージャーを介してインストールすることもできます。 これを行うには、「ツール > NuGetパッケージマネージャ > ソリューションのNuGetパッケージを管理」へ移動します。

ツールドロップダウンメニュー

次に、iTextSharpライブラリを検索し、「インストール」をクリックします。

iTextSharp NuGet画面

  1. リファレンスの追加: インストール後、特にHTMLからPDFへの変換に関連する必要なリファレンスをプロジェクトに追加します。
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
$vbLabelText   $csharpLabel

IronPDFのセットアップ

  1. NuGetコンソール経由でインストール: IronPDFを使用し始めるには、NuGetから直接インストールできます。 Visual Studioプロジェクトを開き、NuGetパッケージマネージャーに移動し、次のコマンドを実行してください。
Install-Package IronPdf
  1. NuGetパッケージマネージャー経由でインストール: 代わりに、上記のiTextSharpの手順と同様に、ソリューション画面のNuGetパッケージマネージャーを介してインストールすることができます。しかし、今回はIronPDFを検索し、「インストール」をクリックしてください。

    IronPDFのNuGet画面

  2. リファレンスの追加: インストール後、プロジェクトにIronPDFをインポートします。
using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

IronPDFは商業利用/開発外での使用にはライセンスキーが必要であることを覚えておいてください。

HTMLからPDFへの変換の実装

iTextSharpの使用

iTextSharpをセットアップしたら、HTMLコンテンツからPDFを作成できるようにするために、プロジェクト内でitextsharp.xmlworkerパッケージがインストールされていることを確認してください。 しかし、より複雑なHTML、特にCSSスタイルが関与している場合には、課題に直面するでしょう。 iTextSharpは、IronPDFと比較して完璧なスタイリングを達成するのに追加の努力が必要になることが多いです。 iTextSharpは基本的なHTML/CSS2のみを処理します(フレックスボックスなし、グリッドなし、CSSの制限あり)ということを覚えておくことが重要です。

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

// Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

// Define HTML content with CSS styling
string html = @"
<html>
<head>
  <style>
    body { font-family: Arial; color: #333; }
    h1 { background: #3498db; color: #fff; padding: 10px; }
    table { width: 100%; border-collapse: collapse; margin-top: 15px; }
    th, td { border: 1px solid #ccc; padding: 6px; }
    th { background: #2980b9; color: #fff; }
    .footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
  </style>
</head>
<body>
  <h1>April Report</h1>
  <p>Here’s a quick overview of this month’s performance metrics.</p>
  <table>
    <tr><th>Metric</th><th>Value</th></tr>
    <tr><td>Features</td><td>12</td></tr>
    <tr><td>Bugs Fixed</td><td>89</td></tr>
  </table>
  <p class='footer'>Generated by iTextSharp</p>
</body>
</html>";

// Create PDF from HTML content
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
    Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
    pdfDoc.Open();
    using (StringReader sr = new StringReader(html))
    {
        // Use XMLWorkerHelper to parse and add HTML content to the PDF document
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
    }
    pdfDoc.Close();
    writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

// Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

// Define HTML content with CSS styling
string html = @"
<html>
<head>
  <style>
    body { font-family: Arial; color: #333; }
    h1 { background: #3498db; color: #fff; padding: 10px; }
    table { width: 100%; border-collapse: collapse; margin-top: 15px; }
    th, td { border: 1px solid #ccc; padding: 6px; }
    th { background: #2980b9; color: #fff; }
    .footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
  </style>
</head>
<body>
  <h1>April Report</h1>
  <p>Here’s a quick overview of this month’s performance metrics.</p>
  <table>
    <tr><th>Metric</th><th>Value</th></tr>
    <tr><td>Features</td><td>12</td></tr>
    <tr><td>Bugs Fixed</td><td>89</td></tr>
  </table>
  <p class='footer'>Generated by iTextSharp</p>
</body>
</html>";

// Create PDF from HTML content
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
    Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
    pdfDoc.Open();
    using (StringReader sr = new StringReader(html))
    {
        // Use XMLWorkerHelper to parse and add HTML content to the PDF document
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
    }
    pdfDoc.Close();
    writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml

' Register provider for specific code pages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)

' Define HTML content with CSS styling
Dim html As String = "
<html>
<head>
  <style>
    body { font-family: Arial; color: #333; }
    h1 { background: #3498db; color: #fff; padding: 10px; }
    table { width: 100%; border-collapse: collapse; margin-top: 15px; }
    th, td { border: 1px solid #ccc; padding: 6px; }
    th { background: #2980b9; color: #fff; }
    .footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
  </style>
</head>
<body>
  <h1>April Report</h1>
  <p>Here’s a quick overview of this month’s performance metrics.</p>
  <table>
    <tr><th>Metric</th><th>Value</th></tr>
    <tr><td>Features</td><td>12</td></tr>
    <tr><td>Bugs Fixed</td><td>89</td></tr>
  </table>
  <p class='footer'>Generated by iTextSharp</p>
</body>
</html>"

' Create PDF from HTML content
Using stream As New FileStream("report.pdf", FileMode.Create)
	Dim pdfDoc As New Document(PageSize.A4, 25, 25, 30, 30)
	Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
	pdfDoc.Open()
	Using sr As New StringReader(html)
		' Use XMLWorkerHelper to parse and add HTML content to the PDF document
		XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
	End Using
	pdfDoc.Close()
	writer.Close()
End Using
Console.WriteLine("PDF generation completed successfully.")
$vbLabelText   $csharpLabel

出力PDFファイル

iTextSharp HTML to PDF出力

IronPDFを使用する

IronPDFはPDF生成プロセスを簡素化し、わずか数行のコードでHTMLコンテンツを新しいPDFドキュメントに簡単に変換できます。次のコードサンプルで実証されています。 これは、スタイリングに使用されるCSSファイル、HTML文字列、CSS/JavaScriptが多用されるウェブコンテンツを伴う高度なHTMLドキュメントでも動作します。

これは、インラインCSSを含むシンプルな例です:

using IronPdf;

class Program
{
    static void Main()
    {
        // Define HTML content with inline CSS styling
        var content = @"
        <html>
        <head>
            <style>
                body {
                    font-family: 'Segoe UI', sans-serif;
                    margin: 40px;
                    background-color: #f8f9fa;
                }
                h1 {
                    color: #2c3e50;
                    border-bottom: 2px solid #2980b9;
                    padding-bottom: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin-top: 20px;
                }
                table, th, td {
                    border: 1px solid #ccc;
                }
                th {
                    background-color: #2980b9;
                    color: white;
                    padding: 10px;
                }
                td {
                    padding: 10px;
                    background-color: #ecf0f1;
                }
                .footer {
                    margin-top: 40px;
                    text-align: center;
                    font-size: 0.9em;
                    color: #7f8c8d;
                }
                img {
                    width: 120px;
                    float: right;
                }
            </style>
        </head>
        <body>
            <img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
            <h1>Monthly Report - April 2025</h1>
            <p>This report outlines performance statistics and key updates for the development team.</p>

            <table>
                <tr>
                    <th>Metric</th>
                    <th>Value</th>
                    <th>Change</th>
                </tr>
                <tr>
                    <td>Feature Releases</td>
                    <td>12</td>
                    <td style='color: green;'>+20%</td>
                </tr>
                <tr>
                    <td>Bugs Resolved</td>
                    <td>89</td>
                    <td style='color: green;'>+45%</td>
                </tr>
                <tr>
                    <td>Downtime</td>
                    <td>2 hrs</td>
                    <td style='color: red;'>+15%</td>
                </tr>
            </table>

            <div class='footer'>
                Generated with IronPDF | © 2025 DevCorp
            </div>
        </body>
        </html>";

        // Use ChromePdfRenderer to render the HTML content as a PDF
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(content);
        pdf.SaveAs("AdvancedStyledReport.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Define HTML content with inline CSS styling
        var content = @"
        <html>
        <head>
            <style>
                body {
                    font-family: 'Segoe UI', sans-serif;
                    margin: 40px;
                    background-color: #f8f9fa;
                }
                h1 {
                    color: #2c3e50;
                    border-bottom: 2px solid #2980b9;
                    padding-bottom: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin-top: 20px;
                }
                table, th, td {
                    border: 1px solid #ccc;
                }
                th {
                    background-color: #2980b9;
                    color: white;
                    padding: 10px;
                }
                td {
                    padding: 10px;
                    background-color: #ecf0f1;
                }
                .footer {
                    margin-top: 40px;
                    text-align: center;
                    font-size: 0.9em;
                    color: #7f8c8d;
                }
                img {
                    width: 120px;
                    float: right;
                }
            </style>
        </head>
        <body>
            <img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
            <h1>Monthly Report - April 2025</h1>
            <p>This report outlines performance statistics and key updates for the development team.</p>

            <table>
                <tr>
                    <th>Metric</th>
                    <th>Value</th>
                    <th>Change</th>
                </tr>
                <tr>
                    <td>Feature Releases</td>
                    <td>12</td>
                    <td style='color: green;'>+20%</td>
                </tr>
                <tr>
                    <td>Bugs Resolved</td>
                    <td>89</td>
                    <td style='color: green;'>+45%</td>
                </tr>
                <tr>
                    <td>Downtime</td>
                    <td>2 hrs</td>
                    <td style='color: red;'>+15%</td>
                </tr>
            </table>

            <div class='footer'>
                Generated with IronPDF | © 2025 DevCorp
            </div>
        </body>
        </html>";

        // Use ChromePdfRenderer to render the HTML content as a PDF
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(content);
        pdf.SaveAs("AdvancedStyledReport.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Define HTML content with inline CSS styling
		Dim content = "
        <html>
        <head>
            <style>
                body {
                    font-family: 'Segoe UI', sans-serif;
                    margin: 40px;
                    background-color: #f8f9fa;
                }
                h1 {
                    color: #2c3e50;
                    border-bottom: 2px solid #2980b9;
                    padding-bottom: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin-top: 20px;
                }
                table, th, td {
                    border: 1px solid #ccc;
                }
                th {
                    background-color: #2980b9;
                    color: white;
                    padding: 10px;
                }
                td {
                    padding: 10px;
                    background-color: #ecf0f1;
                }
                .footer {
                    margin-top: 40px;
                    text-align: center;
                    font-size: 0.9em;
                    color: #7f8c8d;
                }
                img {
                    width: 120px;
                    float: right;
                }
            </style>
        </head>
        <body>
            <img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
            <h1>Monthly Report - April 2025</h1>
            <p>This report outlines performance statistics and key updates for the development team.</p>

            <table>
                <tr>
                    <th>Metric</th>
                    <th>Value</th>
                    <th>Change</th>
                </tr>
                <tr>
                    <td>Feature Releases</td>
                    <td>12</td>
                    <td style='color: green;'>+20%</td>
                </tr>
                <tr>
                    <td>Bugs Resolved</td>
                    <td>89</td>
                    <td style='color: green;'>+45%</td>
                </tr>
                <tr>
                    <td>Downtime</td>
                    <td>2 hrs</td>
                    <td style='color: red;'>+15%</td>
                </tr>
            </table>

            <div class='footer'>
                Generated with IronPDF | © 2025 DevCorp
            </div>
        </body>
        </html>"

		' Use ChromePdfRenderer to render the HTML content as a PDF
		Dim renderer = New ChromePdfRenderer()
		Dim pdf = renderer.RenderHtmlAsPdf(content)
		pdf.SaveAs("AdvancedStyledReport.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

出力PDFファイル

IronPDF HTML to PDF出力

IronPDFを使用すると、上記のコード例で見られるように、正確なCSSスタイリングを持つ洗練されたPDFファイルを期待できます。これは、複雑なHTMLファイル、文字列などで作業する多くの開発者にとっての頼りになるものです。 単純なHTMLからPDFドキュメントへの変換タスクを超えて、IronPDFは高度なPDF操作タスクやPDFセキュリティが可能です。 これにより、それはオールインワンのPDFライブラリとして優れています。

重要な違いと競合環境

iTextSharpとIronPDFを評価するときは、その機能だけでなく、競合環境も考慮することが重要です。 ApryseやAspose.PDFのような他の競合他社も同様のHTMLからPDFへのソリューションを提供していますが、価格や機能に関してはそれぞれのトレードオフがあります。

特徴 IronPDF iTextSharp Apryse Aspose.PDF
使いやすさ 高い 中程度 高い 中程度
CSSサポート 完全 部分的 完全 完全
ライセンス Commercial オープンソース Commercial Commercial
サポート 優れた コミュニティ プレミアム プレミアム
価格 $799から 無料/商業ライセンス 見積もりベース 年間1,679ドルから

IronPDF は、現代のHTML5およびCSS3に対する包括的なサポートにより、今日のほとんどの開発者にとって重要です。 PDFドキュメントの操作、PDFドキュメント作成の制御、変換プロセスの容易さなどの拡張サポートにより、IronPDFは人気のあるPDFライブラリです。

結論と推奨事項

In conclusion, both IronPDF and iTextSharp offer strong capabilities for HTML to PDF conversion in C#, but they cater to different types of developers. オープンソースのソリューションを求め、強力なコミュニティサポートを望む場合には、iTextSharpが適した選択肢となるかもしれません。しかしながら、使いやすさ、強力なCSSサポート、商業的解決策を必要とする開発者にとって、IronPDFはより合理化され、機能が豊富な体験を提供します。 見積り書の自動生成、ブランド化されたPDFドキュメントの作成、またはウェブページ全体をPDFファイルに変換するツールをお探しなら、IronPDFが責任を持って処理します。

今日、IronPDFのユーザーフレンドリーな機能を試してみてください– 無料トライアル をダウンロードして、HTMLからPDFへの変換がどれほど簡単であるかを実際に体験してみましょう。

ご注意iTextSharpはその所有者の登録商標です。 このサイトはiTextSharpと関係がない、または推奨、スポンサーされていません。すべての製品名、ロゴ、およびブランドは、それぞれの所有者の財産です。 比較は情報提供のみを目的としており、執筆時点で公開されている情報を反映しています。

よくある質問

C# で HTML を PDF に変換するにはどうすればいいですか?

IronPDF を使用すると、HTML 文字列には RenderHtmlAsPdf メソッドを、HTML ファイルには RenderHtmlFileAsPdf メソッドを使用して、HTML を PDF に変換できます。このライブラリは、最新の HTML5 と CSS3 をサポートしており、スタイリングとレンダリングの精度を確保します。

HTML から PDF への変換において、IronPDF を使用する主な利点は何ですか?

IronPDF は最新の HTML5 と CSS3 に対する強力なサポートを提供しており、複雑なウェブコンテンツを扱うのに理想的です。使いやすい API により C# とのシームレスな統合が可能で、JavaScript が多用されたコンテンツにも対応しており、動的なウェブページを正確に変換できます。

なぜ開発者は iTextSharp を IronPDF より選ぶのでしょう?

開発者は、カスタマイズのための柔軟なオプションを持つオープンソースソリューションを好む場合に iTextSharp を選ぶかもしれません。現在 iText7 として知られる iTextSharp は、特に高度な HTML と CSS が関与する場合、その複雑さを扱う専門知識を持つ人に適しています。

IronPDF は HTML コンテンツ内の JavaScript を処理できますか?

はい、IronPDF は高度なレンダリング機能により HTML コンテンツ内の JavaScript を処理でき、動的およびインタラクティブなウェブページを PDF に変換するのに適しています。

C# プロジェクトに IronPDF をインストールする方法は?

Visual Studio の NuGet Package Manager を介して IronPDF をインストールできます。Package Manager Console でコマンド Install-Package IronPdf を使用するか、NuGet Package Manager UI で IronPDF を検索します。

IronPDF と iTextSharp を選ぶ際に考慮すべきことは何ですか?

両者を選ぶ際には、使いやすさ、CSS と JavaScript のサポート、ライセンスの要件、HTML コンテンツの複雑さを考慮します。IronPDF は最新のウェブ技術とシームレスな統合に優れ、iTextSharp はオープンソースソリューションを好む人に適しています。

IronPDF のライセンス要件はありますか?

はい、開発環境を超えた使用には IronPDF の商用ライセンスが必要です。サポートレベルや必要な機能に応じてライセンスを購入できます。

iTextSharp で HTML を PDF に変換する際の一般的な問題は何ですか?

iTextSharp に関する一般的な問題は、複雑な HTML および CSS の処理で、追加の構成やカスタマイズが必要な場合があります。開発者は、高度なウェブコンテンツの適切なスタイリングとレンダリングを保証するためにコードを調整する必要があることがよくあります。

IronPDF は Aspose.PDF のような他の PDF ライブラリと比較してどうですか?

IronPDF は、Aspose.PDF と同様に最新のウェブ標準を包括的にサポートし、使いやすさを提供します。その競争力のある価格設定と機能セットは、質とパフォーマンスを維持しながら信頼性のある HTML から PDF への変換を必要とする開発者にとって強力な選択肢です。

Curtis Chau
テクニカルライター

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

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