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
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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