Class ExtractTextInRectangle


  • public class ExtractTextInRectangle
    extends Object

    Extract text from PDF files


    This class provides a simple Java API to extract text from a PDF file and also a static convenience method if you just want to dump all the text from a PDF file or directory containing PDF files

    Example 1 - access API methods

     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();

    Example 2 - convenience static method

    Extract text from a file or set of files and write out results as a txt file Last value is maximum number of pages to process or -1 for all pages
    ExtractTextInRectangle.writeAllOutlinesToDir("pdfs", "output", -1);

    Example 3 - Access directly from the Jar

    ExtractTextInRectangle can run from jar directly using the command line and will extract any outlines from a PDF file or directory to a defined output directory:
    java -cp libraries_needed org/jpedal/examples/text/ExtractTextInRectangle inputValues
    Where inputValues is 2 values:
    • First value: The PDF filename (including the path if needed) or a directory containing PDF files. If it contains spaces it must be enclosed by double quotes (ie "C:/Path with spaces/").
    • Second value: The location to write out data extracted from the PDF file or files. If it contains spaces it must be enclosed by double quotes (ie "C:/Path with spaces/").

    See our Support Pages for more information on Text Extraction.
    • Field Detail

      • isTest

        public static boolean isTest
    • Constructor Detail

      • ExtractTextInRectangle

        public ExtractTextInRectangle​(String fileName)
        Sets up an ExtractTextInRectangle instance to open a PDF File
        Parameters:
        fileName - full path to a single PDF file
      • ExtractTextInRectangle

        public ExtractTextInRectangle​(String fileName,
                                      boolean extractPlainText)
        Sets up an ExtractTextInRectangle instance to open a PDF File
        Parameters:
        fileName - full path to a single PDF file
        extractPlainText - flag to extract plain text rather than XML
      • ExtractTextInRectangle

        public ExtractTextInRectangle​(byte[] byteArray)
        Sets up an ExtractTextInRectangle instance to open a PDF file contained as a BLOB within a byte[] stream
        Parameters:
        byteArray -
    • Method Detail

      • main

        public static void main​(String[] args)
        This class will allow you to extract any Words from page as a list via command line from a single PDF file or a directory of PDF files.
        The example expects two:
        • Value 1 is the file name or directory of PDF files to process
        • Value 2 is directory to write out the outline data
        Parameters:
        args - The expected arguments are described above.
      • writeAllTextToDir

        public static void writeAllTextToDir​(String inputDir,
                                             String outputDir,
                                             int maxPages)
                                      throws PdfException
        Convenience method to write all the text in a directory of PDF files
        Parameters:
        inputDir - directory containing PDF files
        outputDir - directory for writing out wordlists
        Throws:
        PdfException
      • setPassword

        public void setPassword​(String password)
        Parameters:
        password - the USER or OWNER password for the PDF file
      • getPageCount

        public int getPageCount()
        number of pages in PDF file (starting at 1)
        Returns:
        page count
      • openPDFFile

        public boolean openPDFFile()
                            throws PdfException
        routine to open the PDF File so we can access
        Returns:
        true if successful
        Throws:
        PdfException
      • closePDFfile

        public void closePDFfile()
        ensure PDF file is closed once no longer needed and all resources released