PDF Viewer C# Windows Application (Tutorial)

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

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

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

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 Rich Text Box to your form.

We'll add a rich text box in our form. This rich text box will be use 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 at 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

Step 4 Add a button to select PDF

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

PDF Viewer C# Windows Application (Tutorial) Figure 5

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 form 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 selected a file and clicked '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 a 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 rich text box 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

After selecting the PDF file, its content will shows up in the rich text box.

PDF Viewer C# Windows Application (Tutorial) Figure 7

Learn how to use PDF viewer in MAUI by visiting "Viewing 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.

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 own 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.