透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
PDFドキュメントの生成は、C#開発者にとって一般的でしばしば不可欠な要件です。 請求書の作成、詳細な業務報告書の作成、ウェブコンテンツの変換、その他のさまざまな業務文書の管理を任されている場合でも、信頼できるC# PDFジェネレーターは不可欠です。 多くの開発者は、これらのタスクを簡素化するだけでなく、HTMLを高忠実度でPDFに変換したり、既存のPDFを編集したり、プログラムから新しいPDFをゼロから作成したりするような強力な機能を提供する.NETライブラリを探しています。
そのような強力で使いやすいソリューションをお探しの場合は、ここが最適です。 このガイドでは、C#でのPDF生成と操作を効率化するために綿密に設計された主要な.NETライブラリ、IronPDFに焦点を当てます。 IronPDFが一般的なPDF生成のニーズにどのように対処するかを説明し、迅速に開始するための実用的なチュートリアルを提供し、開発ツールキットとしてIronPDFが強力な候補になる理由について説明します。
ご紹介する内容:
C# PDFライブラリを評価する際、開発者はしばしば使いやすさ、レンダリングの正確さ(特にHTMLからPDFへの変換)、包括的な機能セット、全体的なパフォーマンスを優先します。 IronPDFは、これらの分野で優れた性能を発揮するように設計されています:
総合的なPDF機能: IronPDFは単なるPDF作成ツール以上のものです。 これは、膨大な数の操作をサポートする完全なC# PDFツールです。
既存のPDFドキュメントの編集
PDFの結合と分割
ヘッダー、フッター、透かし、およびページ番号の追加
PDFフォームの記入と読み取り
クロスプラットフォーム互換性: Windows、Linux、macOS、Docker、AzureでのIronPDFを用いたアプリケーションの開発およびデプロイは、.NET(Core、Standard、Framework)を対象としています。
では、C#のWindows FormsアプリケーションでIronPDFを使用してPDFを生成する方法について詳しく見ていきましょう。
最初のステップは、Visual Studioプロジェクトを作成することです。 このチュートリアルでは、Windows Forms アプリテンプレートを使用しますが、IronPDF は Web アプリケーション (ASP.NET)、コンソールアプリ、WPF などとシームレスに動作します。
Visual Studioを開きます。
「新しいプロジェクトを作成」をクリックします。
テンプレートから「Windows Forms App (.NET Framework または .NET Core)」を選択し、「次へ」をクリックします。 次のウィンドウが表示されます。 プロジェクトに名前を付けます(例:MyCSharpPdfGenerator
)。
プロジェクトの命名
その後、「次へ」をクリックします。 ドロップダウンメニューから、希望する.NET Frameworkを選択してください(IronPDFは幅広い範囲をサポートしています)。
.NET Frameworkの選択
「作成」ボタンをクリックしてください。 プロジェクトは作成され、次のステップの準備が整います。
IronPDFは、NuGetを使用してプロジェクトに簡単に追加できます。 これは、最新バージョンとすべての必要な依存関係を確保するための推奨方法です。
Visual Studio のツール > NuGet パッケージ マネージャー > パッケージ マネージャー コンソールに移動します。 次に、次のコマンドを入力してEnterキーを押します:
Install-Package IronPdf
Install-Package IronPdf
ソリューション エクスプローラーでプロジェクトを右クリックし、「NuGet パッケージの管理...」を選択します。
「ブラウズ」タブをクリックして「IronPdf」を検索します。
検索結果からIronPdf
パッケージを選択し、「インストール」をクリックします。
または、IronPDF DLLをIronPDFのウェブサイトから直接ダウンロードできます。
DLLをダウンロードして、適切な場所(例:ソリューションディレクトリ内の「Libs」フォルダー)に解凍してください。
Visual Studio ソリューションエクスプローラーで、".NET Framework プロジェクト"の場合は「参照」、".NET Core/5+ プロジェクト"の場合は「依存関係」を右クリックして、「参照の追加...」または「プロジェクト参照の追加...」を選択し、「参照」をクリックします。
IronPdf.dll
をナビゲートして選択します。このチュートリアルでは、PDF生成をトリガーするための基本的なUIを作成します。 ウェブやコンソールアプリケーションを構築する場合、IronPDFのロジックを直接コントローラー、サービス、またはクラスに統合します。
Visual Studioのツールボックスに移動します(表示 > ツールボックス)。 次のコントロールをForm1のデザインサーフェスにドラッグ&ドロップします:
Label
(例:アプリケーションのタイトルとして「C# PDF Generator Demo」)RichTextBox
(名前はPdfText
)TextBox
(名前はURL
)。2つのButton
コントロール。
最初のボタンのテキストを「Generate PDF From Text」に設定します(GeneratePDFFromTextButton
と名付けます)。
GeneratePDFFromURLButton
と名付けます)。次に、C# のロジックを追加しましょう。 フォームデザイナーで「Generate PDF From Text」ボタン(GeneratePDFFromTextButton
)をダブルクリックします。 これは、Form1.cs
ファイルにイベントハンドラメソッドを作成します。
まず、Form1.cs
ファイルの先頭に IronPDF 名前空間を追加します。
using IronPdf;
using IronPdf;
Imports IronPdf
次に、ボタンのclick
イベントハンドラーを実装します。 このコードは、RichTextBox
からテキスト(プレーンテキストまたはHTML)を取り出し、それをPDFドキュメントに変換します。
private void GeneratePDFFromTextButton_Click(object sender, EventArgs e)
{
// It's recommended to set your license key once at application startup.
// IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
// If no key is set, IronPDF will watermark PDFs after a trial period.
// Use SaveFileDialog to let the user choose where to save the PDF
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Default to My Documents
saveFileDialog1.Title = "Save PDF File As";
saveFileDialog1.DefaultExt = "pdf";
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
saveFileDialog1.FilterIndex = 1; // Start with PDF files selected
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
// The core of PDF generation from HTML/Text using IronPDF
// IronPDF's ChromePdfRenderer accurately renders HTML, CSS, and JavaScript.
var renderer = new ChromePdfRenderer();
// The RenderHtmlAsPdf method converts an HTML string to a PDF document.
// This is incredibly powerful for generating dynamic reports, invoices, tickets, etc.
// from HTML templates.
using (var pdfDocument = renderer.RenderHtmlAsPdf(PdfText.Text))
{
pdfDocument.SaveAs(filename);
}
MessageBox.Show("PDF Generated Successfully at: " + filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void GeneratePDFFromTextButton_Click(object sender, EventArgs e)
{
// It's recommended to set your license key once at application startup.
// IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
// If no key is set, IronPDF will watermark PDFs after a trial period.
// Use SaveFileDialog to let the user choose where to save the PDF
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Default to My Documents
saveFileDialog1.Title = "Save PDF File As";
saveFileDialog1.DefaultExt = "pdf";
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
saveFileDialog1.FilterIndex = 1; // Start with PDF files selected
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
// The core of PDF generation from HTML/Text using IronPDF
// IronPDF's ChromePdfRenderer accurately renders HTML, CSS, and JavaScript.
var renderer = new ChromePdfRenderer();
// The RenderHtmlAsPdf method converts an HTML string to a PDF document.
// This is incredibly powerful for generating dynamic reports, invoices, tickets, etc.
// from HTML templates.
using (var pdfDocument = renderer.RenderHtmlAsPdf(PdfText.Text))
{
pdfDocument.SaveAs(filename);
}
MessageBox.Show("PDF Generated Successfully at: " + filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Private Sub GeneratePDFFromTextButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' It's recommended to set your license key once at application startup.
' IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
' If no key is set, IronPDF will watermark PDFs after a trial period.
' Use SaveFileDialog to let the user choose where to save the PDF
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) ' Default to My Documents
saveFileDialog1.Title = "Save PDF File As"
saveFileDialog1.DefaultExt = "pdf"
saveFileDialog1.Filter = "PDF files (*.pdf) *.pdf All files (*.*) *.*"
saveFileDialog1.FilterIndex = 1 ' Start with PDF files selected
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim filename As String = saveFileDialog1.FileName
' The core of PDF generation from HTML/Text using IronPDF
' IronPDF's ChromePdfRenderer accurately renders HTML, CSS, and JavaScript.
Dim renderer = New ChromePdfRenderer()
' The RenderHtmlAsPdf method converts an HTML string to a PDF document.
' This is incredibly powerful for generating dynamic reports, invoices, tickets, etc.
' from HTML templates.
Using pdfDocument = renderer.RenderHtmlAsPdf(PdfText.Text)
pdfDocument.SaveAs(filename)
End Using
MessageBox.Show("PDF Generated Successfully at: " & filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
C# PDF生成コードの説明:
IronPdf.License.LicenseKey
: IronPDFのライセンスキーを設定することは良いプラクティスです。 もしライセンスキーをお持ちであれば、行のコメントを解除し、"YourLicenseKey..."
を実際のキーに置き換えてください。 IronPDFはライセンスキーなしで動作しますが、試用期間後はドキュメントに透かしが追加されます。SaveFileDialog
: これは、ユーザーがPDFの保存場所とファイル名を選択するための標準的なWindowsダイアログを提供します。ChromePdfRenderer
: これはIronPDFのHTMLからPDFへの変換機能の中心です。 それは最大の忠実度を保証するために埋め込み型のChromiumエンジンを使用します。RenderHtmlAsPdf(PdfText.Text)
: この単一のメソッド呼び出しは、RichTextBox
からの文字列コンテンツ(リッチHTMLも含む)を取り、PDFドキュメントオブジェクトに変換します。SaveAs(filename)
: このメソッドは、生成されたPDFドキュメントをユーザーが指定したパスに保存します。pdfDocument
に対して using
ステートメントを使用することで、リソースが正しく管理されることが保証されます。
IronPDFが、HTMLからPDFへの変換のような複雑になりがちな作業を、ほんの数行のコードに簡略化する様子をご覧ください。 これは、迅速かつ信頼性の高い方法でPDF C#を生成する必要がある開発者にとって重要な利点です。
プロジェクトを実行するには、Ctrl + F5
を押す(またはスタートボタンをクリックする) Windowsフォームアプリケーションが表示されます。
リッチテキストボックスにHTMLコンテンツを入力してください。 例えば:
<h1>My First C# PDF Document</h1>
<p>This PDF was generated using <strong>IronPDF</strong> in a C# application.</p>
<p>IronPDF makes it very easy to convert HTML content, including styles and images, into professional PDF files.</p>
<ul>
<li>Easy to use</li>
<li>Accurate rendering</li>
<li>Feature-rich</li>
</ul>
<h1>My First C# PDF Document</h1>
<p>This PDF was generated using <strong>IronPDF</strong> in a C# application.</p>
<p>IronPDF makes it very easy to convert HTML content, including styles and images, into professional PDF files.</p>
<ul>
<li>Easy to use</li>
<li>Accurate rendering</li>
<li>Feature-rich</li>
</ul>
「Generate PDF From Text」ボタンをクリックしてください。 「名前を付けて保存」ダイアログが表示されます。 場所とファイル名を選択し、「保存」をクリックします。
PDFを保存した場所に移動して開いてください。 PDFドキュメント内でHTMLコンテンツが正確にレンダリングされているのが確認できるはずです。
ライブウェブページからPDFを生成することは、もう一つの一般的な要件です。 IronPDFはこれを非常に簡単にします。 フォームデザイナーで「Generate PDF FROM URL」ボタン (GeneratePDFFromURLButton
) をダブルクリックして、クリックイベントハンドラーを作成します。
次のC#コードを追加してください:
private void GeneratePDFFromURLButton_Click(object sender, EventArgs e)
{
// IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
if (string.IsNullOrWhiteSpace(URL.Text))
{
MessageBox.Show("Please enter a valid URL.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Title = "Save PDF From URL As";
saveFileDialog1.DefaultExt = "pdf";
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
try
{
var renderer = new ChromePdfRenderer();
// RenderUrlAsPdf fetches the content from the URL and converts it to PDF.
// This is excellent for archiving web pages or creating PDFs from online reports.
using (var pdfDocument = renderer.RenderUrlAsPdf(URL.Text))
{
pdfDocument.SaveAs(filename);
}
MessageBox.Show("PDF from URL Generated Successfully at: " + filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error generating PDF from URL: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void GeneratePDFFromURLButton_Click(object sender, EventArgs e)
{
// IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
if (string.IsNullOrWhiteSpace(URL.Text))
{
MessageBox.Show("Please enter a valid URL.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Title = "Save PDF From URL As";
saveFileDialog1.DefaultExt = "pdf";
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
try
{
var renderer = new ChromePdfRenderer();
// RenderUrlAsPdf fetches the content from the URL and converts it to PDF.
// This is excellent for archiving web pages or creating PDFs from online reports.
using (var pdfDocument = renderer.RenderUrlAsPdf(URL.Text))
{
pdfDocument.SaveAs(filename);
}
MessageBox.Show("PDF from URL Generated Successfully at: " + filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error generating PDF from URL: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Private Sub GeneratePDFFromURLButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' IronPdf.License.LicenseKey = "YourLicenseKey-GetYourKeyFromIronPdf.com";
If String.IsNullOrWhiteSpace(URL.Text) Then
MessageBox.Show("Please enter a valid URL.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
saveFileDialog1.Title = "Save PDF From URL As"
saveFileDialog1.DefaultExt = "pdf"
saveFileDialog1.Filter = "PDF files (*.pdf) *.pdf All files (*.*) *.*"
saveFileDialog1.FilterIndex = 1
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim filename As String = saveFileDialog1.FileName
Try
Dim renderer = New ChromePdfRenderer()
' RenderUrlAsPdf fetches the content from the URL and converts it to PDF.
' This is excellent for archiving web pages or creating PDFs from online reports.
Using pdfDocument = renderer.RenderUrlAsPdf(URL.Text)
pdfDocument.SaveAs(filename)
End Using
MessageBox.Show("PDF from URL Generated Successfully at: " & filename, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error generating PDF from URL: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
説明:
URL.Text
: これはフォーム上のTextBox
コントロールからURL文字列を取得します。RenderUrlAsPdf(URL.Text)
: この強力なIronPDFメソッドは、指定されたURLに移動し、そのコンテンツ(HTML、CSS、JavaScript、画像を含む)をレンダリングしてPDFドキュメントに変換します。try-catch
)が含まれており、ネットワークの問題や無効なURLが発生する可能性があります。プロジェクトを再実行してください (Ctrl + F5
)。 今回は、URL テキストボックスに完全なURL (例: https://ironpdf.com
) を入力してください。
「URLからPDFを生成」ボタンをクリックします。 保存場所とファイル名を選択します。
生成されたPDFを開く。 ウェブページがレイアウトとコンテンツを保持したまま、忠実にPDFドキュメントに変換されたことがわかります。
このチュートリアルが示すように、IronPDFはすべてのC# PDF生成のニーズに対して非常に強力でありながら簡単なソリューションを提供します。 複雑なCSSとJavaScriptを使用した複雑なHTMLページの変換、データからの動的なレポート生成、ライブURLからのPDFの作成、または.NETアプリケーション内での強力なPDF編集機能が必要な場合でも、IronPDFは効率的に仕事を遂行するためのツールとパフォーマンスを提供します。
PDF C# プロジェクトを生成する際、レンダリングの忠実度や機能セットに制限がある無料ライブラリと、膨大なボイラープレートコードを必要とするより複雑なソリューションの間で選択を迫られることがよくあります。 IronPDF は、開発を合理化し、高品質な出力を保証し、基本的な PDF 作成を超えた豊富な機能セットを提供する包括的で商業的にサポートされた.NET PDF ライブラリとして際立っています。
C#でPDFを生成および操作する最良の方法を体験する準備はできていますか?
Iron Suite を発見: より少ないコストでより多くの .NET ツールを手に入れましょう (Iron Suite には複数の .NET ライブラリが含まれており、他のドキュメント形式やタスクで作業する場合に素晴らしい価値を提供します。)
IronPDFを選択することで、C#プロジェクトにトップクラスのPDF生成および操作エンジンを装備し、貴重な開発時間を節約しつつ、毎回プロフェッショナル品質でピクセル完璧なPDFドキュメントを確保できます。C#でHTMLをPDFに変換する方法についての詳細なガイドを参照してください。