How to Read a PDF File in C# by IronPDF?
Tags: csharp, readpdf, ironpdf, dotnet
Published: 2021-07-14 08:32:03

#Reading a Pdf file in C# using IronPdf; Step by Step Guide:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7qbysi23v98tdszk7ky.png)
 
 
If you're a developer, you must have gone through the problems, where you need to read text from a pdf file. For example:

1. You are developing an application, which takes two pdf documents as input and finds the similarity between files. 
2. You are developing an application which needs to read pdf files and return its word count.
3. You are developing an application which extracts data from your pdf file and puts it in some structured database.
4. You are developing an application which needs to extract pdf text and convert them into string.

Extracting data from a pdf file using C# was a difficult and complex task until IronPdf launched.  

[IronPdf](https://ironpdf.com/) is a library which makes a developers’ life easy, when it comes to reading pdf files.  

You can explore more about IronPdf from this [link](https://ironpdf.com/). 

You can read pdf files and display it in C# Textbox by using just two lines of code. Yes! Just two lines of code. You can also extract all the images in your pdf files and you can create another document of that images or display them in your Application as per your requirement.

Are you wondering how..?

I will show you how. I will show you step by step with the application ,which will enable you to select any pdf files and then it will display it’s content.   
##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 can understand this.
##Who should read this.?
1. Any newbie who is learning C# should also know how to read pdf files, because this is something you are definitely going to use this in your career.
2. Professional developer should also read this to understand the Ironpdf Library which helps us to read, generate, manipulate pdf files.

We will cover following topics:

1. Download C# Read PDF Library and Open Visual Studio
2. Install the Nuget Package from the Menu Bar
3. Design the Windows Form and Write Code for Reading Pdf
4. Add the Back-End Code for the "Clear and Close" Button
5. Run the Project to See your Content of PDF Document
Now Let's see, how we can use this Library in our Project to read the pdf files.

I am making Window form APP for demonstration. you can develop Console Application, WPF Application or ASP.Net web Application according to your choice or need.

The interesting thing is this library can be used with both C# and VB.Net.

Now Let's begin the demonstration without any further delay. 

##Step # 1: Create Visual Studio Project:
 
Open Visual Studio. I am using Visual Studio 2019. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofucq6pfmy90s2ltx09d.png)
Click on Create New Project
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh7qz7d3ksd889bi5a79.png) 
Now, Select Windows Form App from Template, and Press Next, Following Window will appear. Write Project Name. I have written Read Pdf using IronPdf.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka5vbpqhr0ynndp51hy6.png) 
Now, Click Next, Following WIndow will appear. Select .Net Core 3.1 from Drop down Menu.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hub6imdz6wbkdkll70sm.png) 
Click on the "Create" Button, Project will be created as shown below.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orhlqwucioljptcjs4hf.png) 
##Step # 2: Install Nuget Package of IronPdf: 
Now, we have to download Nuget Package or you can also directly download the Library by clicking [here](https://ironpdf.com/downloads/csharp-read-pdf-devto.zip). to create solution. Click on Project Menu from Menu Bar, Drop Down List will appear. Select Manage Nuget Packages, and click on it. The following window will appear.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuccbbwymtn5v2c6h7fp.png) 
Now, Click on the "Browse" tab. The Following window will appear. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhm3a59habbsvpn472pk.png) 

Type IronPdf in Search Box, and Press Enter. The following window will appear. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9mvamfifkw66u5n9n7e.png) 
Select and Click on IronPdf. The following Window will appear. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4kev2auvtn1mq1zyerr.png) 
Press on Install Button, and wait for installation to complete. The following window will appear after successful Installation. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlvza5yemz6u4bpx0r72.png) 
Press Ok Button, and you are good to go.  
Following Readme.Txt file will open. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pbhvjzkodwj9rwjnba9.png) 
I suggest you go through all links and explore more about this Library. 
##Step # 3: Design Window Form:
Project is created and Nuget Package is installed. Next step is to Design Window form that will ask the user to browse for a file and display its content. 

Open Form1 Design
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aesdhwd9kjvpr0l6zh6c.png) 
Click on the Toolbar that is on the left side of the window. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ocboxkiq6pnjy3wnhh.png)
Search for Label, Drag and Drop it on Form Design
Name the Label. I have name it as C# Read Pdf using IronPdf
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iu0nj9t1v562cku4i82.png) 
Now Drag and Drop one Text Box (for showing file Path), Three Buttons (One for Browse the file and another for Read the Pdf file using IronPdf and Other Button for Clear the Text fields), one Rich Text Box (for read and Display the File Content).

Set Read Only Property for  Text Box and Rich Text Box to False. So that user can only read the content and file path.  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r0w99a58b827j5nnpzy.png) 
##Step # 4: Add the Back End Code for Browse the File:
Double Click on the Browse Button, The following code will appear.
```
private void Browse_Click(object sender, EventArgs e)
        {
           
        }
```
Now, Write following code inside 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;
            }
        }
```
OpenFileDialogue will create the instance of the File Dialogue control of Windows form. 

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

I have set DefaultExt = “pdf” as we have to just read the pdf file. 

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

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

Let's run the Project and Test Browse Button. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgscpldl2ivwnssit700.png)
Press on the Browse Button, The following window will appear. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gqkxwb45r02lijj8nv4.png) 
Select File, I am selecting IronPdfTest.pdf and Press Open. The following window will appear.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bhe2aqqygfsllz7tcxx.png)
Now let's write the Code behind the Read Button to read the file. 
##Step # 5: Add the Back End Code for Read Pdf file using Iron Pdf:
You might be thinking that code for reading pdf files will be complex and difficult to write / understand. 

Don’t worry. Ironpdf has made it easier and simpler. We can easily read pdf files using just two lines of code. 

Go to Form1 Design, double click on Read Button, Following code will appear. 
```
private void Read_Click(object sender, EventArgs e)
        {
       
        }
```
Add following code for importing [IronPdf](https://ironpdf.com/examples/reading-pdf-text/) library on the top of the .cs file.
```
using IronPdf;
using System;
using System.Windows.Forms;
```

Write the following code inside the Read_Click function. 
```
private void Read_Click(object sender, EventArgs e)
        {
            PdfDocument PDF = PdfDocument.FromFile(FilePath.Text);
            FileContent.Text = PDF.ExtractAllText(); 
        }
```
Now, Let’s write the code behind Clear Button.

Double Click on Clear Button, It will take you to the following code. 
```
private void Clear_Click(object sender, EventArgs e)
        {
        }
```
Write following Code inside Clear_Click Function
```
private void Clear_Click(object sender, EventArgs e)
        {
            FileContent.Text = "";
            FilePath.Text = "";
        }
```
##Run the Project. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgscpldl2ivwnssit700.png)
Click on the Browse Button and select the file that you want to read. In my case I am reading the IronPdf.pdf file.  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e58ibuglfzsdrr997gf.png)
Press Open Button, The following Window will appear. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdu4cci42l06242hwcrf.png) 
Press on Read Button. It will read the file and display the content as shown below.  
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alejaf57jsg6l0a2pcl7.png) 

This is the completion of the guide. I hope it was easy for you to follow and understand. If you have a query feel free to ask in the comments. 

You can download this solution from [here](https://drive.google.com/file/d/15M2zdTue4zCBU1qiWRXWPuGYppBx5O93/view?usp=sharing).