透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
この記事では、IronPDFを使用してWindows Formsアプリケーションや任意の.NETアプリケーションからPDFファイルを保存する方法について探ります。
IronPDFライブラリは、C#アプリケーションでPDFファイルを生成および操作するための使いやすいクラスとメソッドを提供する.NETライブラリです。 数行のコードでPDFファイルを作成、修正、および保存することができるため、Windows Formsアプリケーションに最適な選択肢となります。
まず、新しいVisual Studioプロジェクトを作成します。 Visual Studio 2022で新しいC# Windows Formsアプリケーションを作成する手順は以下の通りです。
以下のようにVisual Studio 2022を開いてください。
Visual Studio 2022
スタートページで「新しいプロジェクトを作成」をクリックするか、「ファイル」 > 「新規」 > 「プロジェクト」に進んでください。
「新しいプロジェクトの作成」ダイアログボックスで、「新しいプロジェクトの作成」から「Windows フォーム アプリ」または「Windows フォーム アプリ (.NET フレームワーク)」を選択します。
新しいフォームアプリ
プロジェクトの名前を入力し、保存する場所を選択してください。
プロジェクトの場所
.NETフレームワークを選択してください。 ドロップダウンメニューから.NET 7.0を選択してください。
作成ボタンをクリックします。
追加情報
Visual Studio は、新しい C# Windows Forms アプリケーション プロジェクトを作成します。プロジェクトには、デフォルトのフォーム「Form1」が追加され、以下のように表示されます。
Form1 プロジェクト
以上です! では、デザイナーを使用して、Windowsフォームアプリケーションの構築を開始し、PDFドキュメントファイルを作成および保存するためのコントロールと機能を追加します。
お好みに合わせてフォームを設計できます。 このチュートリアルでは、2つのラベル、1つのリッチテキストボックス、2つのボタンを追加して、ミニマルなデザインを作ります。
フォームにボタンを追加する
次のステップとして、このプロジェクトにIronPDFをインストールして、豊富な機能を活用します。
IronPDFは、Visual StudioのNuGetパッケージマネージャーを使用してインストールできます。 NuGet パッケージ マネージャー コンソールに移動するには、ツール > NuGet パッケージ マネージャー > パッケージ マネージャー コンソール の順に進みます。
次のコマンドを入力し、Enterキーを押してください:
Install-Package IronPdf
このコマンドは、あなたのプロジェクトにIronPDFパッケージをダウンロードしてインストールします。 インストールが完了したら、IronPDFの使用を開始できます。
フローを開始するには、Form1.cs
クラスに Save_Click
と getFilePath
の2つのメソッドを書きます。 これらのメソッドは、ChromePdfRenderer Classライブラリを使用してテキストボックスの内容をPDFファイルとして保存するために一緒に使用されます。 各メソッドの動作を理解するために、順番に見ていきましょう。
次のメソッドはボタンクリックイベントのイベントハンドラです。 このメソッドの目的は、テキストボックスの内容をPDFファイルとして保存することです。
private void Save_Click(object sender, EventArgs e)
{
// Get the file path to save the PDF file.
string filename = getFilePath();
// If the file path is not empty or null, proceed with saving the PDF file.
if (!String.IsNullOrEmpty(filePath))
{
// Create a new instance of the ChromePdfRenderer class.
var renderer = new ChromePdfRenderer();
// Render the file contents of the text box as a PDF document using the ChromePdfRenderer.
var pdfDocument = renderer.RenderHtmlAsPdf(pdfContent.Text);
// Save the PDF document to the specified file path using the SaveAs method.
pdfDocument.SaveAs(filename);
// Show a message box to indicate that the PDF file has been saved successfully.
MessageBox.Show("PDF has been saved Successfully!");
}
}
private void Save_Click(object sender, EventArgs e)
{
// Get the file path to save the PDF file.
string filename = getFilePath();
// If the file path is not empty or null, proceed with saving the PDF file.
if (!String.IsNullOrEmpty(filePath))
{
// Create a new instance of the ChromePdfRenderer class.
var renderer = new ChromePdfRenderer();
// Render the file contents of the text box as a PDF document using the ChromePdfRenderer.
var pdfDocument = renderer.RenderHtmlAsPdf(pdfContent.Text);
// Save the PDF document to the specified file path using the SaveAs method.
pdfDocument.SaveAs(filename);
// Show a message box to indicate that the PDF file has been saved successfully.
MessageBox.Show("PDF has been saved Successfully!");
}
}
Private Sub Save_Click(ByVal sender As Object, ByVal e As EventArgs)
' Get the file path to save the PDF file.
Dim filename As String = getFilePath()
' If the file path is not empty or null, proceed with saving the PDF file.
If Not String.IsNullOrEmpty(filePath) Then
' Create a new instance of the ChromePdfRenderer class.
Dim renderer = New ChromePdfRenderer()
' Render the file contents of the text box as a PDF document using the ChromePdfRenderer.
Dim pdfDocument = renderer.RenderHtmlAsPdf(pdfContent.Text)
' Save the PDF document to the specified file path using the SaveAs method.
pdfDocument.SaveAs(filename)
' Show a message box to indicate that the PDF file has been saved successfully.
MessageBox.Show("PDF has been saved Successfully!")
End If
End Sub
このメソッドが行う処理の詳細な手順は以下の通りです:
このメソッドは、PDFファイルが保存されるファイルパスを取得するためにgetFilePath
メソッドを呼び出します。
ファイルパスが空でないか null でない場合、メソッドはPDFファイルの保存を続行します。
このメソッドは、ChromePdfRenderer
クラスの新しいインスタンスを作成します。 これは、Google Chrome ブラウザエンジンを使用してHTMLコンテンツをPDFドキュメントに変換するためのライブラリです。
その後、このメソッドはChromePdfRenderer
クラスのRenderHtmlAsPdf メソッドを使用して、テキストボックスpdfContent
のHTMLコンテンツをPDFドキュメントに変換します。 このPDFドキュメントはPdfDocument変数に割り当てられます。
このメソッドは、PdfDocument
クラスのSaveAs メソッドを使用して、PDFドキュメントを指定されたファイルパスに保存します。
このメソッドは、ユーザーにSaveFileDialog
を表示して、PDFファイルを保存するファイルパスを選択させるために使用されます。
public string getFilePath()
{
// Create a new instance of the SaveFileDialog class.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
// Set the initial directory where the SaveFileDialog will open.
saveFileDialog1.InitialDirectory = @"D:\";
// Set the title of the SaveFileDialog.
saveFileDialog1.Title = "Save the PDF Files";
// Set the SaveFileDialog to check if the specified path exists.
saveFileDialog1.CheckPathExists = true;
// Set the default extension for the file type.
saveFileDialog1.DefaultExt = ".pdf";
// Set the filter to display only PDF files or all files.
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
// Set the filter index to display the PDF filter as the default.
saveFileDialog1.FilterIndex = 2;
// Set the RestoreDirectory property to true so that the SaveFileDialog
// restores the current directory before closing.
saveFileDialog1.RestoreDirectory = true;
// Show the SaveFileDialog and get the result.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// If the user clicked the OK button in the SaveFileDialog, return the selected file path.
return saveFileDialog1.FileName;
}
// If the user did not click the OK button, return an empty string.
return "";
}
public string getFilePath()
{
// Create a new instance of the SaveFileDialog class.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
// Set the initial directory where the SaveFileDialog will open.
saveFileDialog1.InitialDirectory = @"D:\";
// Set the title of the SaveFileDialog.
saveFileDialog1.Title = "Save the PDF Files";
// Set the SaveFileDialog to check if the specified path exists.
saveFileDialog1.CheckPathExists = true;
// Set the default extension for the file type.
saveFileDialog1.DefaultExt = ".pdf";
// Set the filter to display only PDF files or all files.
saveFileDialog1.Filter = "PDF files (*.pdf)
*.pdf
All files (*.*)
*.*";
// Set the filter index to display the PDF filter as the default.
saveFileDialog1.FilterIndex = 2;
// Set the RestoreDirectory property to true so that the SaveFileDialog
// restores the current directory before closing.
saveFileDialog1.RestoreDirectory = true;
// Show the SaveFileDialog and get the result.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// If the user clicked the OK button in the SaveFileDialog, return the selected file path.
return saveFileDialog1.FileName;
}
// If the user did not click the OK button, return an empty string.
return "";
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
このメソッドが行う処理の詳細な手順は以下の通りです:
このメソッドはSaveFileDialog
クラスの新しいインスタンスを作成します。 このクラスはWindows Formsライブラリの一部であり、ユーザーがPDFファイルを保存するファイルパスを選択できるダイアログボックスを提供します。
このメソッドは、SaveFileDialog
オブジェクトの動作をカスタマイズするために、いくつかのプロパティを設定します。 InitialDirectory
プロパティは、ダイアログボックスが最初に開くディレクトリを設定します。 Title
プロパティはダイアログボックスのタイトルを設定します。 CheckPathExists
プロパティは、ダイアログボックスが指定されたパスの存在を確認するかどうかを指定します。 DefaultExt
プロパティは、ファイルタイプのデフォルトのファイル拡張子を設定します。 Filter
プロパティは、ダイアログボックスに表示されるファイルタイプフィルターを設定します。 FilterIndex
プロパティは、表示するデフォルトフィルターを設定します。 最後に、RestoreDirectory
プロパティは、ダイアログボックスが閉じる前に現在のディレクトリを復元するかどうかを指定します。
このメソッドは、ShowDialog
メソッドを呼び出すことによって SaveFileDialog
を表示します。 このメソッドはダイアログボックスを表示し、ユーザーが「OK」ボタンまたはキャンセルボタンをクリックしたかを示すDialogResult
値を返します。
ユーザーが「OK」ボタンをクリックすると、メソッドはSaveFileDialog
のFileName
プロパティにアクセスして、ユーザーが選択したファイルパスを返します。
ユーザーが「キャンセル」ボタンをクリックするか、ダイアログボックスを閉じた場合、メソッドは空の文字列を返します。
プロジェクトを実行して、出力を確認しましょう。 プロジェクトを実行すると、次のフォームが開きます。
Windows Forms プロジェクトの実行
PDFコンテンツを入力し、以下に示す「保存」ボタンをクリックしてください。
保存ダイアログボックス
次のPDFが作成されました。
PDFファイルを作成しました
IronPDFは、ChromePdfRenderer
クラスとSaveFileDialog
ダイアログボックスを使用して、HTMLコンテンツをPDFドキュメントに変換し、ユーザーが選択したファイルパスに保存する簡単な方法を提供します。
Windows FormsアプリケーションからPDFファイルを保存することは一般的な要件であり、IronPDFはこれを達成するための使いやすく柔軟な方法を提供します。 この記事では、C# Windows Forms アプリケーションで IronPDF を使用してファイルを作成し、コンテンツを追加し、保存する方法を示しました。 IronPDFを使用すると、開発者はほんの数行のコードでアプリケーションから高品質のPDFファイルを生成できます。
IronPDFは、HTMLからPDFへの変換チュートリアル、PDFのマージ例のコード、PDFページの分割ガイド、およびテキストと画像の抽出方法など、さまざまな機能を提供しています。 IronPDFは開発用に無料で提供されており、商用プロジェクトでの使用が可能な商用ライセンスおよび無料体験が含まれており、専用のサポートとアップデートが付属しています。 さらに、IronPDFはIron Suiteの一部であり、これはバーコード生成(IronBarcode)、Excelドキュメントの作成、読み取り、操作(IronXL)、テキスト抽出の処理(IronOCR)などを含む.NETソフトウェアコンポーネントのバンドルです。 完全な Iron Suite を購入することは、2 つの料金で 5 つの製品すべてを手に入れることができるため、コストパフォーマンスの高いソリューションです。