/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.examples.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

import java.io.File;
import java.io.IOException;

/**
 * This is an example on how to get some x/y coordinates of text.
 *
 * Usage: java org.apache.pdfbox.examples.util.PrintTextLocations &lt;input-pdf&gt;
 *
 * @author Ben Litchfield
 */
public class PrintTextLocations extends PDFTextStripper
{
    /**
     * Default constructor.
     *
     * @throws IOException If there is an error loading text stripper properties.
     */
    public PrintTextLocations() throws IOException
    {
        super.setSortByPosition( true );
    }

    /**
     * This will print the documents data.
     *
     * @param args The command line arguments.
     *
     * @throws Exception If there is an error parsing the document.
     */
    public static void main( String[] args ) throws Exception
    {
        if( args.length != 1 )
        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( new File(args[0]) );
                PrintTextLocations printer = new PrintTextLocations();
                int pageNum = 0;
                for( PDPage page : document.getPages() )
                {
                    pageNum++;
                    System.out.println( "Processing page: " + pageNum );
                    PDStream contents = page.getStream();
                    if( contents != null )
                    {
                        printer.processPage(page);
                    }
                }
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }

    /**
     * A method provided as an event interface to allow a subclass to perform
     * some specific functionality when text needs to be processed.
     *
     * @param text The text to be processed
     */
    protected void processTextPosition( TextPosition text )
    {
        System.out.println( "String[" + text.getXDirAdj() + "," +
                text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale=" +
                text.getXScale() + " height=" + text.getHeightDir() + " space=" +
                text.getWidthOfSpace() + " width=" +
                text.getWidthDirAdj() + "]" + text.getUnicode() );
    }

    /**
     * This will print the usage for this document.
     */
    private static void usage()
    {
        System.err.println( "Usage: java org.apache.pdfbox.examples.pdmodel.PrintTextLocations <input-pdf>" );
    }

}

@@ -849,85 +849,167 @@
 ort 
-org.apache.pdfbox.pdmodel.PDDocument;%0Aimport org.apache.pdfbox.pdmodel.PDPage
+java.io.ByteArrayOutputStream;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.Writer;%0Aimport java.util.List
 ;%0Aim
@@ -1043,23 +1043,18 @@
 del.
-common.PDStream
+PDDocument
 ;%0Aim
@@ -1147,58 +1147,8 @@
 n;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0A%0A
 /**%0A
@@ -1410,27 +1410,48 @@
   * 
-Default constructor
+Instantiate a new PDFTextStripper object
 .%0A  
@@ -1516,20 +1516,10 @@
 ng t
+h
 e
-xt stripper
  pro
@@ -1596,49 +1596,8 @@
   %7B%0A
-        super.setSortByPosition( true );%0A
     
@@ -2081,24 +2081,25 @@
 args%5B0%5D) );%0A
+%0A
             
@@ -2107,31 +2107,29 @@
    P
-rintTextLocations print
+DFTextStripper stripp
 er =
@@ -2175,204 +2175,157 @@
     
-int pageNum = 0;%0A                for( PDPage page : document.getPages() )%0A                %7B%0A                    pageNum++;%0A                    System.out.println( %22Processing page: %22 + pa
+stripper.setSortByPosition( true );%0A                stripper.setStartPage( 0 );%0A                stripper.setEndPage( document.
 ge
+t
 Num
+berOfPages()
  );%0A
+%0A
     
@@ -2340,39 +2340,64 @@
     
-    PDStream contents = page.ge
+Writer dummy = new OutputStreamWriter(new ByteArrayOutpu
 tStr
@@ -2401,16 +2401,17 @@
 Stream()
+)
 ;%0A      
@@ -2424,147 +2424,44 @@
     
-    if( contents != null )%0A                    %7B%0A                        printer.processPage(page);%0A                    %7D%0A                %7D
+stripper.writeText(document, dummy);
 %0A   
@@ -2668,153 +2668,69 @@
   * 
-A method provided as an event interface to allow a subclass to perform%0A     * some specific functionality when text needs to be processed
+Override the default functionality of PDFTextStripper
 .%0A     *
 %0A   
@@ -2729,87 +2729,142 @@
    *
+/
 %0A    
- * @param text The text to be processed
+@Override%0A    protected void writeString(String string, List%3CTextPosition%3E textPositions) throws IOException
 %0A    
- */
+%7B
 %0A    
-protected void process
+    for (
 Text
@@ -2875,11 +2875,17 @@
 tion
-( T
+ text : t
 extP
@@ -2895,22 +2895,25 @@
 tion
- text )%0A
+s)%0A    
     %7B%0A
+    
     
@@ -2970,24 +2970,28 @@
 j() + %22,%22 +%0A
+    
             
@@ -3065,32 +3065,36 @@
                 
+    
 text.getXScale()
@@ -3151,32 +3151,36 @@
                 
+    
 text.getWidthOfS
@@ -3208,32 +3208,36 @@
                 
+   
+ 
 text.getWidthDir
@@ -3271,22 +3271,36 @@
 de() );%0A
+        %7D%0A
     %7D%0A
+    
 %0A    /**
@@ -3515,11 +3515,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Make PrintTextLocations work again