USING IRONPDF

C# Read PDF File: Easy Tutorial

Updated December 2, 2023
Share:

If you are a developer you have probably encountered problems trying to read text from a PDF file. Perhaps one or more of the following scenarios apply to you.

  1. You are developing an application that takes two PDF documents as input and finds the similarity between the documents.
  2. You are developing an application that needs to read PDF documents with IronPDF and return the word count.
  3. You are developing an application that extracts data from a PDF file using IronPDF and puts it in a structured database.
  4. You are developing an application that needs to extract PDF text content and convert it into a string.
  5. Extracting data from PDF files using C# was a difficult and complex task until the development of IronPDF.

IronPDF Library Overview is a library that makes it so much easier for developers to read PDF files.

You can explore more about IronPDF and Iron Software Suite Offerings.

You can read PDF files and display the data in a C# Textbox by using just two lines of code. Yes, just two lines of code. You can also extract all the images from PDFs. Further, you can create another document with those images or display them in your application as per your requirements.

Let us show you how it's done.

We can proceed step by step with the application to select any PDF files and then display their content.

The following steps show you how to read PDF files in C#:

Prerequisite Knowledge:

  1. Basic Knowledge of C# Programming
  2. Basic Knowledge of C# GUI Controls

I have designed this tutorial in such a way that even a person with no programming background will be able to progress.

Who should read this

Any newcomer learning C# should know how to read PDF files using IronPDF because this is something you are definitely going to use in your career.

Professional developers should also read this to be able to understand the IronPDF Library, which helps us to read, generate, and manipulate PDF documents.

Now, how can we use this Library in our Project to read a PDF file?

I am using a Windows Forms App for demonstration. You can use a Console Application, a WPF Application, or an ASP.NET web application according to your preference.

Another major advantage of the IronPDF library is that it can be used with both C# and VB.NET.

Let's begin the demonstration without further delay.


Step #1: Create a Visual Studio Project

Open Visual Studio. I am using Visual Studio 2019.

Click on "Create New Project":

C# Read PDF File: Easy Tutorial, Figure 3: Create New Project Create New Project

Now, select the Windows Forms App from the template, press "Next", and the following window will appear. Enter a project name. I have written 'Read Pdf using IronPDF'.

C# Read PDF File: Easy Tutorial, Figure 16: Configure project via Visual Studio Configure project via Visual Studio

Now, click "Next", and the following window will appear. Select '.NET Core 3.1' from the drop-down menu.

C# Read PDF File: Easy Tutorial, Figure 9: .NET Core 3.1 version .NET Core 3.1 version

Click on the "Create" button, and the Project will be created as shown below.

C# Read PDF File: Easy Tutorial, Figure 6: Initial stage of a new Windows Forms application Initial stage of a new Windows Forms application


Step #2: Install the IronPDF NuGet Package

Click on the Project Menu from the Menu Bar, and a drop-down list will appear. Select Manage NuGet Packages, and click on it. The following window will appear:

C# Read PDF File: Easy Tutorial, Figure 1: NuGet Package Manager NuGet Package Manager

Now, click on "Browse". The following window will appear:

C# Read PDF File: Easy Tutorial, Figure 2: NuGet Package Manager UI NuGet Package Manager UI

Type IronPdf in the search box, and press "Enter". The following window will appear:

C# Read PDF File: Easy Tutorial, Figure 4: NuGet Solution NuGet Solution

Select and click on IronPdf. The following window will appear: C# Read PDF File: Easy Tutorial, Figure 7: Install Free IronPdf Install Free IronPdf

Press the "Install" button and wait for the installation to complete. The following window will appear after a successful installation:

C# Read PDF File: Easy Tutorial, Figure 5: IronPdf for .NET IronPdf for .NET

Press the "Ok" button, and you are good to go.

Note: There are other ways to download the NuGet Package. You can also install IronPdf by using the Package Manager Console; to do this, open the Package Manager Console and write the following code:

Install-Package IronPdf

You can also download it on the NuGet package page for IronPDF.

The following Readme.txt file will open:

C# Read PDF File: Easy Tutorial, Figure 12: IronPdf's readme file with code samples IronPdf's readme file with code samples

I suggest you go through all the links and explore more IronPDF code samples about this Library.


Step #3: Design a Windows Forms App

Once a Project is created and the NuGet Package is installed, the next step is to design a Windows Forms App that will ask the user to browse for a file and display its content.

Open Form1 Design:

C# Read PDF File: Easy Tutorial, Figure 14: Form1 Design UI Form1 Design UI

Click on the toolbar that is on the left-hand side of the window:

C# Read PDF File: Easy Tutorial, Figure 15: Toolbox UI for Label and TextBox Toolbox UI for Label and TextBox

Search for Label, and drag and drop it into the Form Design

Name the label. Here, I have named it "C# Read Pdf using IronPDF".

C# Read PDF File: Easy Tutorial, Figure 20: Form1 UI with Label added Form1 UI with Label added

Next, drag and drop one text box (to show the file path), three buttons (one for browsing the files, one for reading PDF files using IronPDF, and the third button for "Clear the Text" fields), and one RichTextBox (for reading and displaying the file contents).

Set the "Read Only Property" for the TextBox and RichTextBox to "False". This is so that users can only read the contents and file path.

C# Read PDF File: Easy Tutorial, Figure 10: Form1 fully designed Form1 fully designed


Step #4: Add the Back-end Code for Browsing PDF Files

Double-click on the "Browse" button and the following window will appear:

private void Browse_Click(object sender, EventArgs e)
{
}
private void Browse_Click(object sender, EventArgs e)
{
}
Private Sub Browse_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
VB   C#

Next, write the following code inside the Browse_Click function:

private void Browse_Click(object sender, EventArgs e)
{
    OpenFileDialog browseFile = new OpenFileDialog
    {
        InitialDirectory = @"D:\",
        Title = "Browse Pdf Files",
        CheckFileExists = true,
        CheckPathExists = true,
        DefaultExt = "pdf",
        Filter = "pdf files (*.pdf)|*.pdf",
        FilterIndex = 2,
        RestoreDirectory = true,
        ReadOnlyChecked = true,
        ShowReadOnly = true
    };
    if (browseFile.ShowDialog() == DialogResult.OK)
    {
        FilePath.Text = browseFile.FileName;
    }
}
private void Browse_Click(object sender, EventArgs e)
{
    OpenFileDialog browseFile = new OpenFileDialog
    {
        InitialDirectory = @"D:\",
        Title = "Browse Pdf Files",
        CheckFileExists = true,
        CheckPathExists = true,
        DefaultExt = "pdf",
        Filter = "pdf files (*.pdf)|*.pdf",
        FilterIndex = 2,
        RestoreDirectory = true,
        ReadOnlyChecked = true,
        ShowReadOnly = true
    };
    if (browseFile.ShowDialog() == DialogResult.OK)
    {
        FilePath.Text = browseFile.FileName;
    }
}
Private Sub Browse_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim browseFile As New OpenFileDialog With {
		.InitialDirectory = "D:\",
		.Title = "Browse Pdf Files",
		.CheckFileExists = True,
		.CheckPathExists = True,
		.DefaultExt = "pdf",
		.Filter = "pdf files (*.pdf)|*.pdf",
		.FilterIndex = 2,
		.RestoreDirectory = True,
		.ReadOnlyChecked = True,
		.ShowReadOnly = True
	}
	If browseFile.ShowDialog() = DialogResult.OK Then
		FilePath.Text = browseFile.FileName
	End If
End Sub
VB   C#

OpenFileDialogue will create the instance of the File Dialogue control of the Windows Forms App.

I have set the Initial Path to D Drive; you can set it to any.

I have set DefaultExt = "pdf" as we only have to read the PDF file.

I have used a filter so that the browse file dialog will only show you the PDF file to select.

When the user clicks "Ok", it will show the file path in the File Path field.

Let us run the solution and test the "Browse" button.

C# Read PDF File: Easy Tutorial, Figure 10: Form1 UI Form1 UI

Press the "Browse" button, and the following window will appear:

C# Read PDF File: Easy Tutorial, Figure 11: Browse File dialog to select a PDF file Browse File dialog to select a PDF file

Select the file (I am selecting IronPDFTest.pdf) and press "Open". The following window will appear.

C# Read PDF File: Easy Tutorial, Figure 13: PDF in C# PDF in C#

Now let's write the code behind the "Read" button to read the file.


Step #5: Add the Back-end Code for Read PDF Documents using IronPDF

You might be thinking that code for reading a PDF file would be complex and difficult to write and understand.

Don't worry. IronPDF has simplified things and made it all so much easier. We can easily read the PDF file using just two lines of code.

Go to Form1 Design and "double-click" on the "Read" button. The following window will appear:

private void Read_Click(object sender, EventArgs e)
{
}
private void Read_Click(object sender, EventArgs e)
{
}
Private Sub Read_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
VB   C#

Add a namespace using IronPdf to import the IronPDF library:

using System;
using IronPdf;
using System;
using IronPdf;
Imports System
Imports IronPdf
VB   C#

Write the following code inside the Read_Click function:

private void Read_Click(object sender, EventArgs e)
{
    using PdfDocument pdf = PdfDocument.FromFile(FilePath.Text);
    FileContent.Text = pdf.ExtractAllText(); 
}
private void Read_Click(object sender, EventArgs e)
{
    using PdfDocument pdf = PdfDocument.FromFile(FilePath.Text);
    FileContent.Text = pdf.ExtractAllText(); 
}
Private Sub Read_Click(ByVal sender As Object, ByVal e As EventArgs)
	Using pdf As PdfDocument = PdfDocument.FromFile(FilePath.Text)
		FileContent.Text = pdf.ExtractAllText()
	End Using
End Sub
VB   C#

FilePath is the name of the text field that displays the location of the PDF document we want to read. We will get the location of the file dynamically.

ExtractAllText with IronPDF is the IronPDF function that will extract all the data from PDF pages. This data will then be displayed in the Rich Text box and named as "File Content".

Next, let's write the code behind the "Clear Button". This is just an additional item if you wish to clear the screen once you have read the PDF document.

Double-click on the "Clear Button", and it will take you to the following code:

void Clear_Click(object sender, EventArgs e)
{
}
void Clear_Click(object sender, EventArgs e)
{
}
Private Sub Clear_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
VB   C#

Write the following code inside the Clear_Click function:

void Clear_Click(object sender, EventArgs e)
{
    FileContent.Text = "";
    FilePath.Text = "";
}
void Clear_Click(object sender, EventArgs e)
{
    FileContent.Text = "";
    FilePath.Text = "";
}
Private Sub Clear_Click(ByVal sender As Object, ByVal e As EventArgs)
	FileContent.Text = ""
	FilePath.Text = ""
End Sub
VB   C#

Run the Solution

Click on the "Browse" button and select the document you want to read. In my case, I am reading the IronPDF.pdf file as an example:

C# Read PDF File: Easy Tutorial, Figure 8: PDF documents PDF documents

Press the "Open" button and the following window will appear:

C# Read PDF File: Easy Tutorial, Figure 13: Application with a selected PDF file Application with a selected PDF file

Press the "Read" button. It will read the file and display the content as shown below.

C# Read PDF File: Easy Tutorial, Figure 19: Display PDF text content Display PDF text content


Summary

This is an example solution. No matter how many pages, images or texts are in your PDF files, IronPDF will extract all the texts and images for you to use for any purpose. You simply need to get the license for the library and begin using it.

This completes the tutorial. I hope you have understood everything, and if you have any queries, feel free to post them in the comments section.

You can download the project zip file. If you wish to buy the complete package of Iron software products, our special offer means that you can now buy all of them for the price of just two. If you need more details about the license and support, please click Iron Software's pricing information. You can also get the free trial license offerings.

< PREVIOUS
.NET PDF Generator in 1 Click
NEXT >
How to Password Protect a PDF Document

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >