Read PDF in Java Using OpenPDF

In this post we’ll see a Java program to read PDF document using OpenPDF library.

To know more about OpenPDF library and PDF examples check this post- Generating PDF in Java Using OpenPDF Tutorial

Reading PDF using OpenPDF

For reading PDF using OpenPDF you need to perform the following steps.

  1. Create a PDFReader instance with the path to PDF that has to be read.
  2. Wrap PDFReader instance in an instance of PdfTextExtractor.
  3. Get PDF content using pdfTextExtractor.getTextFromPage() method.
import java.io.IOException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class ReadPDF {
  // PDF to be read
  public static final String READ_PDF = "F://knpcode//result//OpenPDF//Content.pdf";
  public static void main(String[] args) {
    PdfReader pdfreader = null;
    try {
      pdfreader = new PdfReader(READ_PDF);
      // get pages in PDF
      int pages = pdfreader.getNumberOfPages();
      PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfreader);
      // Iterate through pages to read content
      for(int i = 1; i <= pages; i++) {
        // Extract content of each page
        String contentOfPage = pdfTextExtractor.getTextFromPage(i, true);
        System.out.println(contentOfPage);
      }			
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      if(pdfreader != null) {
        pdfreader.close();
      }
    }	
  }
}

Related Posts

That’s all for the topic Read PDF in Java Using OpenPDF. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • ArrayBlockingQueue in Java
  • Comparable Vs Comparator in Java
  • Static Method Overloading And Overriding in Java
  • JShell in Java
  • How to Compress MapReduce Job Output
  • Spring Boot With JSP Example

    Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.