Wednesday , January 17 2018
Home / java / PDFBox – Java pdf reader Example

PDFBox – Java pdf reader Example

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


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

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"))) {
            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();
        }
    }

}

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 chandrashekhar

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+ ,

Recommended

Java MD5 Encryption Example

In this tutorial, I am going to show you how to use Java MD5 Encryption. …

Leave a Reply

Your email address will not be published. Required fields are marked *