フッターコンテンツにスキップ
IRONPDFの使用

C#チュートリアル: IronPDFを使用してPDFテキストコンテンツビューアを構築します(Windowsフォーム)

In today's digital era, PDF files are integral to many workflows across education, business, and personal use. They are a standard format for sharing and presenting diverse data, including text, images, and tables.

While displaying full PDF documents with complete visual fidelity within a C# Windows Forms application can involve dedicated rendering components, developers often have other needs. Sometimes, the goal is to read PDF text in C#, extract data, or display the textual content of a PDF for quick review, indexing, or accessibility.

This article will guide you through creating an application that focuses on this specific task: building a simple C# PDF text content viewer using IronPDF, a powerful .NET library. You'll learn how to use IronPDF to load a PDF and effectively extract and display its text content in a Windows Forms application.

IronPDFとは何ですか?

IronPDF is a comprehensive C# library that empowers .NET developers to create, edit, and process PDF files within their applications. It allows users to convert HTML, images, and SVG to PDF documents, and importantly for this tutorial, to read and extract content from existing PDFs. IronPDF is designed for ease of use and provides a wide range of features to manipulate PDF files.

Requirements for Building a PDF Text Viewer

To create this C# PDF text display application, you will need:

  • Visual Studio: An Integrated Development Environment (IDE) for creating Windows Forms applications.
  • IronPDF: A NuGet package that provides the functionality to read, create, and manipulate PDF documents, including text extraction.

Illustration showing HTML to PDF conversion concept IronPDF can also create PDFs from HTML, a separate feature from the text extraction shown in this tutorial.

Steps to Create a PDF Text Content Viewer in C# with IronPDF

Step 1: Create a New Windows Forms Application in Visual Studio

To begin, launch Visual Studio and click on "Create a new project." Select "Windows Forms App (.NET Framework)" or a similar .NET template from the list.

Visual Studio new project dialog Visual Studio New Project Creation

Next, provide a name for your project (e.g., CSharpPdfTextReader) and click the Create button. This will set up a new Windows Forms Application project.

Step 2: Install the IronPDF Library

Using NuGet Package Manager GUI

  1. In Solution Explorer, right-click on your project and select "Manage NuGet Packages..."
  2. Go to the "Browse" tab and search for "IronPdf".
  3. Select the IronPdf package and click "Install".

NuGet Package Manager in Visual Studio searching for IronPDF Installing IronPDF via NuGet Package Manager

Using NuGet Package Manager Console

Alternatively, open the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and run the command:

Install-Package IronPdf

This will download and install IronPDF and its dependencies into your project.

Step 3: Add a RichTextBox to Your Form for Text Display

We will use a RichTextBox control to display the extracted text content from the PDF. A RichTextBox is suitable for showing formatted text, though for this tutorial, its primary role is to present the plain text extracted by IronPDF. It effectively shows the textual information without attempting to render the PDF's original visual layout.

To add a RichTextBox:

  1. Open your form in the Designer view.
  2. Go to the Toolbox (View > Toolbox).
  3. Find RichTextBox under "Common Controls," drag it onto your form.
  4. Adjust its size and position as needed. In the Properties window, you can set its Name (e.g., pdfDataRichTextBox) and set its Dock property to Fill if you want it to take up most of the form.

PDF Viewer C# Windows Application (Tutorial), Figure 4: Access the RickTextBox in Form1 Adding a RichTextBox to Form1 to display extracted PDF text

Step 4: Add a Button to Select the PDF File

Add a Button control to your form. Users will click this button to open a file dialog and select a PDF file for text extraction.

  1. Drag a Button from the Toolbox onto your form.
  2. In the Properties window, set its Name (e.g., openBtn) and Text (e.g., "Open PDF & Display Text").

PDF Viewer C# Windows Application (Tutorial), Figure 5: Add a new Button to Form1 Adding a Button to Form1 to trigger PDF selection

Step 5: Add C# Code to Load PDF and Extract Text

Double-click the button you just added ("Open PDF & Display Text") to create its Click event handler in Form1.cs.

First, ensure you have the IronPDF namespace imported at the top of your Form1.cs file:

using IronPdf;
using System; // For EventArgs, Exception
using System.Windows.Forms; // For OpenFileDialog, MessageBox, DialogResult, etc.
using IronPdf;
using System; // For EventArgs, Exception
using System.Windows.Forms; // For OpenFileDialog, MessageBox, DialogResult, etc.
Imports IronPdf
Imports System ' For EventArgs, Exception
Imports System.Windows.Forms ' For OpenFileDialog, MessageBox, DialogResult, etc.
$vbLabelText   $csharpLabel

Now, implement the event handler for the button click. This code will:

  1. Prompt the user to select a PDF file.
  2. Use IronPDF to load the selected PDF.
  3. Use IronPDF's ExtractAllText() method to get all text from the PDF.
  4. Display this extracted text in the RichTextBox.
private void openBtn_Click(object sender, EventArgs e)
{
    // Create an OpenFileDialog to open PDF files
    var openFileDialog = new OpenFileDialog
    {
        Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*", // Filter to show only PDFs
        Title = "Select a PDF file to extract text from" // Dialog title
    };

    // Show dialog and check if the user selected a file
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            // It's recommended to set your license key once at application startup.
            // License.LicenseKey = "YourIronPdfLicenseKey"; 
            // If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
            // For text extraction, the trial is fully functional for development.

            // Load the selected PDF using IronPDF
            var pdf = PdfDocument.FromFile(openFileDialog.FileName);

            // Extract all text content from the PDF using IronPDF
            string extractedText = pdf.ExtractAllText();

            // Display the extracted text in the RichTextBox
            // (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
            pdfDataRichTextBox.Text = extractedText; 
        }
        catch (Exception ex)
        {
            // Show error message if an exception occurs
            MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
private void openBtn_Click(object sender, EventArgs e)
{
    // Create an OpenFileDialog to open PDF files
    var openFileDialog = new OpenFileDialog
    {
        Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*", // Filter to show only PDFs
        Title = "Select a PDF file to extract text from" // Dialog title
    };

    // Show dialog and check if the user selected a file
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            // It's recommended to set your license key once at application startup.
            // License.LicenseKey = "YourIronPdfLicenseKey"; 
            // If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
            // For text extraction, the trial is fully functional for development.

            // Load the selected PDF using IronPDF
            var pdf = PdfDocument.FromFile(openFileDialog.FileName);

            // Extract all text content from the PDF using IronPDF
            string extractedText = pdf.ExtractAllText();

            // Display the extracted text in the RichTextBox
            // (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
            pdfDataRichTextBox.Text = extractedText; 
        }
        catch (Exception ex)
        {
            // Show error message if an exception occurs
            MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
Private Sub openBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
	' Create an OpenFileDialog to open PDF files
	Dim openFileDialog As New OpenFileDialog With {
		.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*",
		.Title = "Select a PDF file to extract text from"
	}

	' Show dialog and check if the user selected a file
	If openFileDialog.ShowDialog() = DialogResult.OK Then
		Try
			' It's recommended to set your license key once at application startup.
			' License.LicenseKey = "YourIronPdfLicenseKey"; 
			' If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
			' For text extraction, the trial is fully functional for development.

			' Load the selected PDF using IronPDF
			Dim pdf = PdfDocument.FromFile(openFileDialog.FileName)

			' Extract all text content from the PDF using IronPDF
			Dim extractedText As String = pdf.ExtractAllText()

			' Display the extracted text in the RichTextBox
			' (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
			pdfDataRichTextBox.Text = extractedText
		Catch ex As Exception
			' Show error message if an exception occurs
			MessageBox.Show("An error occurred while processing the PDF file: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try
	End If
End Sub
$vbLabelText   $csharpLabel

Code Breakdown:

  • openFileDialog: A standard dialog for file selection, filtered for PDF files.
  • PdfDocument.FromFile(openFileDialog.FileName): This IronPDF method loads the chosen PDF into a PdfDocument object.
  • pdf.ExtractAllText(): This is the key IronPDF function for this tutorial. It reads through the entire PDF and extracts all discernible text content into a single string. This is incredibly useful for C# parse PDF text scenarios.
  • pdfDataRichTextBox.Text = extractedText;: The extracted text is then assigned to the Text property of your RichTextBox (ensure the name pdfDataRichTextBox matches the name you gave your RichTextBox control).

This demonstrates how IronPDF simplifies reading PDF text in C#, allowing developers to access PDF content programmatically with minimal effort.

Step 6: Build and Run Your C# PDF Text Viewer Application

  1. In Visual Studio, go to the "Build" menu and select "Build Solution."
  2. Once the build is successful, press "F5" or click the "Start" button to run the application.

Your application window will appear. Click the "Open PDF & Display Text" button, select a PDF file from your computer, and click "Open."

PDF Viewer C# Windows Application (Tutorial), Figure 6: Run the Application Running the C# PDF Text Viewer Application

The RichTextBox will then display the text content extracted from the selected PDF file.

PDF Viewer C# Windows Application (Tutorial), Figure 7: Display the text content after selecting a PDF file Text content extracted from the PDF and displayed in the RichTextBox

For information on visually rendering PDFs in MAUI applications (which is different from this tutorial's text extraction focus), you might explore "PDF Viewing in MAUI Tutorial".

Conclusion: Accessing PDF Text Content Made Easy with C# and IronPDF

By following these steps, you've created a C# Windows Forms application that effectively extracts and displays text content from PDF files using IronPDF. This approach is valuable when you need to programmatically access the textual information within PDFs for display, analysis, or further processing in your .NET applications.

IronPDF provides robust capabilities for C# PDF text extraction, and it's just one part of its comprehensive feature set. You can also use IronPDF for more advanced tasks like text searching within PDFs, adding annotations, printing PDF documents, PDF encryption and decryption, and editing PDF forms.

Remember, this tutorial focused on one specific use case: making PDF text accessible in a C# application. You can adapt and expand upon this foundation to meet more complex requirements.

If you're interested in exploring the full potential of IronPDF:

  • Dive into the IronPDF documentation for detailed guides and examples.
  • To use IronPDF in your production applications without trial limitations, a license key is required. You can purchase a license from the IronPDF website. Licenses start from $799.
  • You can also evaluate the full commercial version with a free trial.

よくある質問

C#アプリケーションでPDFからテキストを抽出するにはどうすればいいですか?

IronPDFのExtractAllText()メソッドを使用して、C#アプリケーション内でPDFドキュメントからすべての判読可能なテキストコンテンツを効率的に抽出できます。

C#でPDFテキストビューアを作成するにはどのようなツールが必要ですか?

C#でPDFテキストビューアを作成するには、開発環境としてVisual Studioと、NuGetパッケージマネージャーを通じてインストールできるIronPDFライブラリが必要です。

Windows Formsアプリケーションで抽出したPDFテキストを表示するにはどうすればいいですか?

Windows Formsアプリケーションでは、PDFから抽出したテキストコンテンツを表示するためにRichTextBoxコントロールを使用できます。これにより、フォーマットされたテキストの表示が可能です。

C#アプリケーションでPDFファイルを選択するプロセスは何ですか?

PDFファイルを選択するには、ファイルダイアログを開くボタンコントロールをフォームに追加します。これにより、ユーザーは処理したいPDFファイルをブラウズして選択できます。

C#でのPDF処理中にエラーを処理するにはどうすればいいですか?

PDF処理コードをtry-catchブロックでカプセル化し、例外が発生した場合はMessageBox.Showを使用してエラーメッセージを表示することによってエラーを処理できます。

IronPDFはどのような追加の機能を提供しますか?

IronPDFはテキスト抽出以外にも、HTMLからPDFへの変換、注釈の追加、テキストの検索、PDFの暗号化と復号、印刷、PDFフォームの編集などの機能を提供します。

PDF処理のための新しいWindows FormsプロジェクトをVisual Studioで設定する方法

Visual Studioで 「Create a new project」を選択し、「Windows Forms App(.NET Framework)」を選択します。プロジェクトに名前を付け、「Create」をクリックしてPDF処理のためのプロジェクトを設定します。

C#でPDFテキストビューアアプリケーションを実行するために必要な手順は何ですか?

Visual Studioのビルドメニューから「ビルドソリューション」を選択し、F5キーを押すか「開始」をクリックしてアプリケーションを実行します。ボタンを使用してPDFファイルを選択し、そのテキストを表示します。

IronPDFはHTMLをPDFに変換するために使用できますか?

はい、IronPDFはRenderHtmlAsPdfメソッドでHTML文字列を、RenderHtmlFileAsPdfメソッドでHTMLファイルをPDFに変換できます。

PDFテキスト抽出の一般的なトラブルシューティングシナリオは何ですか?

一般的な問題には、標準外のフォントや暗号化されたPDFの処理が含まれます。テキスト抽出が失敗する場合は、PDFファイルがパスワード保護されていないことを確認し、フォントの互換性を確認してください。

IronPDF は .NET 10 と互換性がありますか?

はい。IronPDF は、.NET 10 と以前のバージョン (.NET 9、8、7、6、.NET Core、.NET Standard、.NET Framework など) の両方をサポートしています。つまり、互換性の問題が発生することなく、.NET 10 プロジェクトで IronPDF を使用して Windows フォーム テキスト ビューアーを構築できます。

Curtis Chau
テクニカルライター

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

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