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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With