In this tutorials I am going to show you how to work with java pdf reader. To read the pdf document from Java application, here I am going to use PdfBox.

PDFBox is an open source Java tool to work with pdf documents, provided by Apache.

Java pdf reader :

Lets see how to work with PDFBox in java application. To work with PDFBox we need to have the pdfbox dependency.

pdfbxox maven :

We should include the below maven dependency in pom.xml

[xml]

<dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.6</version>
</dependency>

[/xml]

Java pdf reader Example :

I am going to read the below pdf from Java application.

Java pdf reader Example

[java]

package com.onlinetutorialspoint.pdfbox;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.text.PDFTextStripper;

public class PdfReader {

    public static void main(String[] args) {
        try (PDDocument document = PDDocument.load(new File("C:\\Desktop\\sample.pdf"))) {
            document.getClass();
            if (!document.isEncrypted()) {

                PDFTextStripper tStripper = new PDFTextStripper();
                String pdfFileInText = tStripper.getText(document);
                String lines[] = pdfFileInText.split("\\r?\\n");
                
                for (String line : lines) {
                    System.out.println(line);
                }
            }

        } catch (InvalidPasswordException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
[/java]

Output :

If a country is to be corruption free and become a nation of beautiful minds, I strongly feel there
are three key societal members who can make a difference. They are the father, the mother and
the teacher.

A. P. J. Abdul Kalam

 

Reference :

PDFBox by Apache

Happy Learning 🙂