Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox - getting words locations (and not only characters')

Is it possible to get the locations of words using PDFBox, similar to "processTextPosition"? It seems that processTextPosition is called on single characters only, and the code that merges them into words is part of PDFTextStripper (in the "normalize") method, which does return the location of the text. Is there a method / utility that extracts the location as well? (For those wondering what the motivation is - the information is actually a table, and we would like to detect empty cells) Thanks

like image 925
user964797 Avatar asked Sep 10 '12 14:09

user964797


1 Answers

to get words and their x and y positions in a text extracted from a pdf file you will have to extend the PdfTextStripper class and use the custom class to extract text from the pdf file eg

public class CustomPDFTextStripper extends PDFTextStripper{

    public CustomPDFTextStripper() throws IOException {

    }

    /**
    * Override the default functionality of PDFTextStripper.
    */

    @Override
    protected void writeString(String text, List<TextPosition> textPositions) throws IOException{
        TextPosition firstProsition = textPositions.get(0);
        writeString(String.format("[%s , %s , %s]", firstProsition.getTextPos().getXPosition(),
                firstProsition.getTextPos().getYPosition(), text));

    }
}

create an object of this custom class and extract text as thus

PDFTextStripper pdfStripper = new CustomPDFTextStripper();
String text = pdfStripper.getText(*pdf file wrapped as a PDDocument object*);

the resultant text string is in the form [xposition, yposition, word] separated by the default word separator

like image 124
Ovokerie Ogbeta Avatar answered Nov 10 '22 04:11

Ovokerie Ogbeta