USING IRONPDF

PDF Viewer C# Windows Application (Tutorial)

Updated February 25, 2023
Share:

In today's digital era, PDF files have become an essential part of our daily routine. They are used in many fields like education, business, and personal use, etc. PDF files can contain a large amount of data, including text, images, and tables, which makes them an ideal format for sharing and presenting data.

However, sometimes it becomes challenging to display PDF documents within a Windows Forms application. This article will show you how to create a simple PDF viewer application using IronPDF, a C# PDF library for .NET developers.

What is IronPDF?

IronPDF is a C# library that enables developers to create, edit, and display PDF files in their .NET applications. It allows users to convert HTML, images, and SVG to PDF documents and vice versa. IronPDF is simple to use, and it provides developers with a wide range of features to manipulate PDF files.

Requirements

To create a PDF viewer application, you will need the following tools and packages:

Visual Studio: A software development IDE (Integrated Development Environment) used to create Windows Forms applications.

IronPDF: A NuGet package that provides functionality to read, create, and manipulate PDF documents.

PDF Viewer C# Windows Application (Tutorial), Figure 1: HTML to PDF HTML to PDF

Steps to Create a PDF Viewer Windows Application with IronPDF

Step 1 Create a new Windows Forms Application in Visual Studio

To create a new Windows Forms Application, launch Visual Studio, and click on "Create a new project." Then select "Windows Forms App (.NET Framework)" from the list of project templates.

PDF Viewer C# Windows Application (Tutorial), Figure 2: Visual Studio Code Visual Studio Code

Next, enter a name for your project and click on the Create button. This will create a new Windows Forms Application project in Visual Studio.

Step 2 Install IronPDF using NuGet Package Manager

To install IronPDF, right-click on your project in the solution explorer and select "Manage NuGet Packages". This will open the NuGet Package Manager, where you can search for IronPDF.

PDF Viewer C# Windows Application (Tutorial), Figure 3: NuGet Package Manager NuGet Package Manager

Once you have found IronPDF, click on the "Install" button to add it to your project. This will install IronPDF and all its dependencies.

Step 3 Add a RichTextBox to your form

We'll add a RichTextBox in our form. This RichTextBox will be used to display PDF content. A RichTextBox is a container that enables you to display or edit rich text content, including paragraphs, hyperlinks, and more. You can also use a RichTextBox to display PDF content, although it may not preserve all the formatting found in the original PDF document.

Here's how you can add a RichTextBox to your designer CS file:

  1. In the Toolbox (typically located at the side of the Visual Studio IDE), expand the "Common Controls" section.
  2. Look for the "RichTextBox" control and click on it.
  3. Go back to your form in the Designer, and you should see your cursor has changed to a crosshair. Click and drag to create a new RichTextBox on the form.
  4. You can adjust the size and position of the RichTextBox by selecting it and dragging the corners or edges.
  5. In the Properties window (usually located on the other side of the IDE), you can set properties for the RichTextBox, such as its Name (so you can refer to it in your code).

PDF Viewer C# Windows Application (Tutorial), Figure 4: Access the RickTextBox in Form1 Access the RickTextBox in Form1

Step 4 Add a button to select PDF

Now we need to add a button, which will help to select a PDF file using the Browse window.

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

Step 4 Add code behind the button

Now double-click on the button. It'll open the source code file. First, you will need to add the following using statement to the top of your Form1.cs file:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Step 5 Create an event to handle PDF file loading

The provided code is an event handler for a button click event in a Windows Forms application that uses the IronPDF library to load a PDF file and extract its text content.

private void openBtn_Click(object sender, EventArgs e)
{
    var openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        var pdf = PdfDocument.FromFile(openFileDialog.FileName);
        pdfData.Text = pdf.ExtractAllText();
    }
}
private void openBtn_Click(object sender, EventArgs e)
{
    var openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        var pdf = PdfDocument.FromFile(openFileDialog.FileName);
        pdfData.Text = pdf.ExtractAllText();
    }
}
Private Sub openBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim openFileDialog As New OpenFileDialog()
	openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
	If openFileDialog.ShowDialog() = DialogResult.OK Then
		Dim pdf = PdfDocument.FromFile(openFileDialog.FileName)
		pdfData.Text = pdf.ExtractAllText()
	End If
End Sub
VB   C#

Here's a detailed breakdown of the code:

This is the event handler for the 'Click' event of a button called 'openBtn'. Every time this button is clicked, this method gets triggered.

private void openBtn_Click(object sender, EventArgs e)
private void openBtn_Click(object sender, EventArgs e)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'private void openBtn_Click(object sender, EventArgs e)
VB   C#

This line initializes a new instance of the OpenFileDialog class, which is a standard dialog that allows the user to open a file.

var openFileDialog = new OpenFileDialog();
var openFileDialog = new OpenFileDialog();
Dim openFileDialog As New OpenFileDialog()
VB   C#

This line sets the Filter property of the OpenFileDialog to only display PDF files and all file types in the file dialog box.

openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
VB   C#

The ShowDialog() method shows the OpenFileDialog to the user. If the user selects a file and clicks the 'Open' button on the dialog, ShowDialog() returns DialogResult.OK. This line checks if the user did just that.

if (openFileDialog.ShowDialog() == DialogResult.OK)
if (openFileDialog.ShowDialog() == DialogResult.OK)
If openFileDialog.ShowDialog() = DialogResult.OK Then
VB   C#

If the user selects a file and clicks 'Open', this line gets the selected file's path from the FileName property of the OpenFileDialog. It then uses the FromFile method of the PdfDocument class in the IronPDF library to create a new PdfDocument object that represents the selected PDF file.

var pdf = PdfDocument.FromFile(openFileDialog.FileName);
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
Dim pdf = PdfDocument.FromFile(openFileDialog.FileName)
VB   C#

This line calls the ExtractAllText method on the PdfDocument object to extract all text from the PDF file. It then assigns the extracted text to the Text property of a control called 'pdfData', which is presumably a TextBox or similar control that can display text.

pdfData.Text = pdf.ExtractAllText();
pdfData.Text = pdf.ExtractAllText();
pdfData.Text = pdf.ExtractAllText()
VB   C#

In summary, this method opens a file dialog for the user to select a PDF file. If the user selects a file and clicks 'Open', the method loads the PDF file, extracts all text from it, and displays the extracted text in a control.

Step 6 Build and run the application

Once you have completed all the steps above, you can build and run your PDF viewer application. To do this, click on the "Build" menu in Visual Studio and select "Build Solution". After building the solution, you can run the application by pressing the "F5" key or by clicking on the "Start" button in the toolbar.

When the application starts, you should see a form with RichTextBox and a button. To open a PDF file, click on the "Open PDF" button, select a PDF file from your computer, and click the "Open" button.

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

After selecting the PDF file, its content will show up in the RichTextBox.

PDF Viewer C# Windows Application (Tutorial), Figure 7: Display the text content after selecting a PDF file Display the text content after selecting a PDF file

Learn how to use PDF viewer in MAUI by visiting "View PDFs in MAUI" tutorial.

Conclusion

By following these steps, you can create a simple PDF viewer application that allows users to view PDF content. With the IronPDF library, you can also add more advanced features such as text searching, annotation, and printing as well as encrypting PDFs and interacting with PDF forms.

Remember that this is just a simple example of what you can do with IronPDF. You can experiment with different controls, properties, and events to create your custom PDF viewer application that meets your specific needs.

If you're interested in learning more about IronPDF, be sure to check out the IronPDF documentation, which provides detailed information on how to use the library and includes many examples and tutorials.

To use IronPDF in your application, you need to have a valid license key. You can obtain a license key by purchasing a license from the IronPDF website. License starts from $749 and you can also avail free trial.

< PREVIOUS
How to Read PDF Table in C#
NEXT >
How to Convert Word to PDF in C# (Tutorial)

Ready to get started? Version: 2024.6 just released

Start Free Trial Total downloads: 9,580,872
View Licenses >