Search code examples
fontsrotationpdfboxtext-extraction

How to get the text rotation angle with PDFBOX


I have a job to remove a watermark in a PDF. I find some text watermark show in PDF with some angle. So I can judge whether the text is a possible watermark or not by the angle of text. Now I have a problem to get the rotation angle of text.

I have tried to call TextPositon.getRation(), TextPositon.getDir(), I also referenced Rotate text in pdfbox with java.

mkl said "Your getDir only returns multiples of 90° but text can be drawn at arbitrary angles" from PDFBox text extraction, rotation and font name, size, but I still feel confused.


Solution

  • From the ExtractText tool:

    static int getAngle(TextPosition text)
    {
        Matrix m = text.getTextMatrix().clone();
        m.concatenate(text.getFont().getFontMatrix());
        return (int) Math.round(Math.toDegrees(Math.atan2(m.getShearY(), m.getScaleY())));
    }