USING IRONPDF

How to Read PDF Files in C#

Updated February 25, 2024
Share:

Programmatic PDF processing is crucial in industries like finance, healthcare, legal, and education, where critical information needs to be processed, analyzed, and extracted from PDF documents for purposes such as data analysis, document management, and automation. Despite its importance, this task can be challenging.

IronPDF: A C# PDF Library

IronPDF enables you to handle incredibly difficult tasks easily. It allows for the easy editing of text in a PDF document, in a similar way to how you can work with text files in a text document, all the while allowing you to export files in any operating system. The IronPDF application covers the complete process of viewing, modifying, and extracting content from a PDF.

Take the Right Step with IronPDF

Text can be read and written in PDF file format quickly and easily using any computer with IronPDF software. Installation is a simple task. This is the best way to learn to read PDF files in C#. You may also download IronPDF free of cost for development. If you explore IronPDF you will notice that the library provides extensive functionality that makes it very easy to use PDFs. Explore classes in your free time! There are several C# examples available to learn how to create an optimal output from reading PDFs.

Read PDF Files using IronPDF

Step 1: Install the IronPDF Package

To begin, you will need to install the IronPDF NuGet package into your .NET project. You can do this by opening the Package Manager Console in Visual Studio and entering the following command:

Install-Package IronPdf

Step 2: Import the IronPDF Library

Next, you need to import the IronPDF library into your code by adding the following statement at the top of your file:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Step 3: Load the PDF Document

Once you have imported the IronPDF library, you can load a PDF document into your code by using the following code:

PdfDocument pdf = PdfDocument.FromFile(@"C:\dotnet.pdf");
var outputPath = "Example.pdf";
pdf.SaveAs(outputPath);
PdfDocument pdf = PdfDocument.FromFile(@"C:\dotnet.pdf");
var outputPath = "Example.pdf";
pdf.SaveAs(outputPath);
Dim pdf As PdfDocument = PdfDocument.FromFile("C:\dotnet.pdf")
Dim outputPath = "Example.pdf"
pdf.SaveAs(outputPath)
VB   C#

Step 4: Extract Text from the PDF

IronPDF provides a range of methods to extract text from an existing PDF file. For example, you can begin extracting text from a PDF and print it on the console by using the following code snippet:

string text = pdf.ExtractText();
Console.Writeline(text);
string text = pdf.ExtractText();
Console.Writeline(text);
Dim text As String = pdf.ExtractText()
Console.Writeline(text)
VB   C#

Using the above code, you can extract text from a PDF file.

How to Read PDF Files in C#, Figure 1: Extracting Text from a PDF Using IronPDF Extracting Text from a PDF Using IronPDF

Step 5: Rasterize a PDF to Images

Let's rasterize the PDF file to Images using IronPDF. First, import the required libraries:

using System.Linq;
using IronPdf;
using IronSoftware.Drawing;
using System.Linq;
using IronPdf;
using IronSoftware.Drawing;
Imports System.Linq
Imports IronPdf
Imports IronSoftware.Drawing
VB   C#

The code then uses the RasterizeToImageFiles method to extract all the pages of the PDF document to a folder as image files. The extracted images can be saved as either PNG or JPG files, and the dimensions and page ranges of the images can also be specified.

// Extract all pages to a folder as image files
pdf.RasterizeToImageFiles(@"C:\image\folder\*.png");

// Dimensions and page ranges may be specified
pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80);
// Extract all pages to a folder as image files
pdf.RasterizeToImageFiles(@"C:\image\folder\*.png");

// Dimensions and page ranges may be specified
pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80);
' Extract all pages to a folder as image files
pdf.RasterizeToImageFiles("C:\image\folder\*.png")

' Dimensions and page ranges may be specified
pdf.RasterizeToImageFiles("C:\image\folder\example_pdf_image_*.jpg", 100, 80)
VB   C#

Finally, the code uses the ToBitmap method to extract all pages of the PDF document as AnyBitmap objects, which can be processed and manipulated further within the code.

// Extract all pages as AnyBitmap objects
AnyBitmap [] pdfBitmaps = pdf.ToBitmap();
// Extract all pages as AnyBitmap objects
AnyBitmap [] pdfBitmaps = pdf.ToBitmap();
' Extract all pages as AnyBitmap objects
Dim pdfBitmaps() As AnyBitmap = pdf.ToBitmap()
VB   C#

The above code demonstrates how to extract the contents of a PDF file using IronPDF and save the extracted data as image files or AnyBitmap objects for further processing.

Step 7: Manipulate PDF Pages

Let's learn how to manipulate the pages of a PDF document by reading the PDF file using IronPDF.

The code first removes pages two and three from the PDF document using the RemovePages method:

pdf.RemovePages(1, 2);
pdf.RemovePages(1, 2);
pdf.RemovePages(1, 2)
VB   C#

The RemovePages method takes two arguments: the starting page to remove (in this case, page 2, represented as 1 since page numbering starts at 0) and the number of pages to remove (in this case, 2 pages).

Step 6: Save the PDF

Finally, you can save the PDF file to your local system using the SaveAs method. The code for saving the PDF file is as follows:

pdf.SaveAs(OutputPath);
pdf.SaveAs(OutputPath);
pdf.SaveAs(OutputPath)
VB   C#

IronPDF Compatibility

IronPDF is highly compatible with all the latest .NET Frameworks including the .NET 7. It also supports .NET Blazor and .NET MAUI, which are the latest offerings from Microsoft for web development. The library's compatibility with these frameworks makes it possible for developers to seamlessly integrate IronPDF into their applications and take advantage of its powerful features.

One of the main features of IronPDF is its ability to read PDF files in .NET Blazor and .NET MAUI. This feature enables developers to quickly and easily read and extract data from PDF files and use them in the .NET applications. This capability can be especially helpful when working with a large volume of data. Developers don't need any other library to use the IronPDF in their .NET project.

Get more information about IronPDF working with .NET Blazor and .NET MAUI on IronPDF's website.

Conclusion

In conclusion, reading PDF files programmatically is crucial in various industries. IronPDF provides a comprehensive solution to handle this task by offering extensive functionality to read, modify, and extract content from a PDF file. IronPDF is easy to install and use with just a few simple steps.

The library offers methods to extract text, rasterize a PDF to an image, manipulate pages, and save PDF files. Whether you are new to programmatic PDF processing or an experienced developer, IronPDF is the perfect tool to take your skills to the next level.

If you are looking for a reliable and efficient solution for reading PDF files in C#, IronPDF is worth exploring, especially with its license starting from $749, and a free trial available. You can see more plans provided by IronPDF in the image below. You can select the package that matches your needs.

How to Read PDF Files in C#, Figure 2: IronPDF Licensing Prices IronPDF Licensing Prices

< PREVIOUS
How to Convert Word to PDF in C# (Tutorial)
NEXT >
Should You Buy IronPDF?

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,173,334 View Licenses >