PDFBox – Java pdf reader Example

By |2018-03-03T06:43:45+05:30December 20th, 2017|java|

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


Java pdf reader Example :

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

Java pdf reader Example

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"))) {
            if (!document.isEncrypted()) {

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

        } catch (InvalidPasswordException e) {
        } catch (IOException e) {


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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment