Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

How to extract text from PDF files in Java

1 min read

Text handling in a PDF file requires a lot of preparatory work with handling font encoding, and decoding the raw text data streams into usable content.  JPedal Java PDF library includes routines to handle all this and to make text extraction from PDF files very easy. Here we show you how.

Search a PDF file for text

JPedal makes it very easy to scan the pages of a PDF file for text. Here is a simple example (Javadoc).
JPedal includes some very powerful features for text search including regular expressions.

FindTextInRectangle extract=new FindTextInRectangle("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
      int pageCount=extract.getPageCount();
      for (int page=1; page<=pageCount; page++) {
 
          float[] co-ords=extract.findTextOnPage(page"textToFind", SearchType.MUTLI_LINE_RESULTS ) ;
      }
 }
 
 extract.closePDFfile();

Extract Unstructured Text from a PDF file

If a PDF contains unstructured, extractable text,  this API will allow it to be extracted from the page (Javadoc).

ExtractTextInRectangle extract=new ExtractTextInRectangle("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
 
        String text=extract.getTextOnPage(page);
     }
 }
 
 extract.closePDFfile();

Extract Strucutred Text from a PDF file

If a PDF was correctly created with structured, extractable text, then this API will allow the text content to be extracted from the page as Structured content in a Java Document (Javadoc).

ExtractTextInRectangle extract=new ExtractTextInRectangle("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     int pageCount=extract.getPageCount();
     for (int page=1; page<=pageCount; page++) {
 
        String text=extract.getTextOnPage(page);
     }
 }
 
 extract.closePDFfile();

Extract Wordlist from a PDF file

Many customers use JPedal to pre-index the text content of their PDF files in a database. This API makes it easy to extract all the words from a PDF file with their text positions onscreen  (Javadoc).

ExtractTextAsWordlist extract=new ExtractTextAsWordlist("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
      int pageCount=extract.getPageCount();
      for (int page=1; page<=pageCount; page++) {
 
        List wordList=extract.getWordsOnPage(page);
      } 
 }
 
 extract.closePDFfile();

Extract Document outline from  PDF files

PDF file often contain a Document outline to provide a Table of Contents (Javadoc).

ExtractOutline extract=new ExtractOutline("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     Document pdfOutline=extract.getPDFTextOutline();
 }
 
 extract.closePDFfile();

The JPedal API provides a great deal of easy to use functionality with PDF files and Text handling. Are there any other additional features you would like to see?

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.