TextPosition.getXDirAdj()
and TextPosition.getYDirAdj()
do not return the coordinates in the default userspace coordinate system but instead in a coordinate system which PDFBox text stripping uses internally to easier determine lines of text.
To retrieve coordinates in the default userspace coordinate system, use the TranslateX
and TranslateY
properties of the text matrix and add the coordinates of the lower left corner of the crop box of the current page.
E.g. for a TextPosition textposition
and a PDRectangle cropBox
:
float x = textposition.getTextMatrix().getTranslateX() + cropBox.getLowerLeftX();
float y = textposition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY();
Often the lower left of the crop box is (0, 0), so the correction using the crop box often is not necessary. For an example, though, for which that correction is necessary, see this answer.