Skip to footer content
USING IRONPDF FOR JAVA

How to Read PDF File in Java

This article will explore how to create a PDF reader, to open a PDF file in your software application programmatically. To perform this task effectively, IronPDF for Java is one such system library which helps open and read PDF files using the filename in Java programs.

IronPDF

The IronPDF - Java Library is built on top of the already successful .NET Framework. This makes IronPDF a versatile tool for working with PDF documents compared to other class libraries such as Apache PDFBox. It provides the facility to extract and parse content, load text, and load images. It also provides options to customize the PDF pages such as page layout, margins, header and footer, page orientation, and much more.

In addition to this, IronPDF also supports conversion from other file formats, protecting PDFs with a password, digital signing, merging, and splitting PDF documents.

How to Read PDF Files in Java

Prerequisites

To use IronPDF to make a Java PDF reader, it is necessary to ensure that the following components are installed on the computer:

  1. JDK - Java Development Kit is required for building and running Java programs. If it is not installed, download it from the Oracle Website.
  2. IDE - Integrated Development Environment is software that helps write, edit, and debug a program. Download any IDE for Java, e.g., Eclipse, NetBeans, IntelliJ.
  3. Maven - Maven is an automation tool that helps download libraries from the Central Repository. Download it from the Apache Maven Website.
  4. IronPDF - Finally, IronPDF is required to read the PDF file in Java. This needs to be added as a dependency in your Java Maven Project. Include the IronPDF artifact along with the slf4j dependency in the pom.xml file as shown in the example below:
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
XML

Adding Necessary Imports

Firstly, add the following code on top of the Java source file to reference all the required methods from IronPDF:

import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
JAVA

Next, configure IronPDF with a valid license key to use its methods. Invoke the setLicenseKey method in the main method.

License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
JAVA

Note: You can get a free trial license key to create, read, and print PDFs.

Read Existing PDF File in Java

To read PDF files, there must be PDF files, or one can be created. This article will use an already created PDF file. The code is simple and a two-step process to extract text from the document:

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
JAVA

In the above code, fromFile opens a PDF document. The Paths.get method gets the directory of the file and is ready to extract content from the file. Then, [extractAllText](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText()) reads all the text in the document.

The output is below:

How to Read PDF File in Java, Figure 1: Reading PDF Text Output Reading PDF Text Output

Read Text from a Specific Page

IronPDF can also read content from a specific page in a PDF. The extractTextFromPage method uses a PageSelection object to accept a range of page(s) from which text will be read.

In the following example, the text is extracted from the second page of the PDF document. PageSelection.singlePage takes the index of the page which needs to be extracted (index starting from 0).

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
JAVA

How to Read PDF File in Java, Figure 2: Reading PDF Text Output Reading PDF Text Output

Other methods available in the PageSelection class which can be used to extract text from various pages include: [firstPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#lastPage()), [lastPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#firstPage()), pageRange, and [allPages](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#allPages()).

Read Text from a Newly-Generated PDF File

Search text can also be performed from a newly generated PDF file from either an HTML file or URL. The following sample code generates PDFs from URL and extracts all text from the website.

// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
JAVA

How to Read PDF File in Java, Figure 3: Read from a New File Read from a New File

IronPDF can also be used to extract images from PDF files.

The complete code is as follows:

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
JAVA

Summary

This article explained how to open and read PDFs in Java using IronPDF.

IronPDF helps easily create PDFs from HTML or URL and convert from different file formats. It also helps in getting PDF tasks done quickly and easily.

Try IronPDF for 30 days with a free trial and find out how well it works for you in production. Explore commercial licensing options for IronPDF which start only from $749.

Frequently Asked Questions

What is this Java library for working with PDF documents?

IronPDF for Java is a library built on top of the .NET Framework that allows developers to efficiently read, parse, and manipulate PDF documents in Java applications.

How can I read PDF files in Java using a library?

To read PDF files using IronPDF in Java, you need to download the IronPDF Java Library, use the 'fromFile' method to load PDF documents, and call the 'extractAllText' or 'extractTextFromPage' methods to extract text from the PDFs.

What are the prerequisites for using a PDF library in Java?

To use IronPDF in Java, you need to have JDK, an IDE like Eclipse or IntelliJ, Maven for dependency management, and include the IronPDF artifact in your project.

How can I extract text from a specific page in a PDF using a Java library?

IronPDF allows you to extract text from a specific page using the 'extractTextFromPage' method with a 'PageSelection' object, which specifies the page range or index to be extracted.

Can this library generate PDFs from URLs?

Yes, IronPDF can generate PDFs from URLs. You can use the 'renderUrlAsPdf' method to create a PDF from a given URL and then extract text from it.

Does this Java PDF library support password protection for PDFs?

Yes, IronPDF supports various features including password protection, digital signing, and merging or splitting PDF documents.

What other file formats can this library convert to PDF?

IronPDF can convert different file formats to PDF, including HTML and other document formats. It provides versatile options for PDF generation and manipulation.

Is there a trial version available for this Java PDF library?

Yes, IronPDF offers a 30-day free trial to explore its capabilities and evaluate its performance in production environments.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.

Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?