General Question: Changing origin co-ordinates of a PDF
Hey,
If a PDF has its origin (0, 0) at the top right corner, is it possible to just move the origin from the top right to top left, like it generally is?
I don't want to rotate the text or page, just want to move the co-ordinates of the pages, so that I can query/search text from a generic template which defines specific bounding boxes.
In PyMuPDF, coordinates of page objects are always relative to the unrotated page.
So in order to extract text from inside some rectangle, you must specify it in unrotated coordinates.
There are matrices which help with coordinate transformation: Page.rotationMatrix, Page.derotationMatrix.
So you can adjust a given rectangle's coordinates by using e.g. rect * page.rotationMatrix.
You can also simply reset the page's rotation and work as usual: page.setRotation(0).
Won't harm anything.
Yeah, I tried to log them first, and they came out to be:
- rotationMatrix : Matrix(0.0, 1.0, -1.0, 0.0, 842.0, 0.0)
- derotationMatrix : Matrix(0.0, -1.0, 1.0, 0.0, -0.0, 842.0)
I am not able to get much how to just fix the coordinates, could you please help me, and explain what these values actually signify? I already tried going through the docs.
First let's make sure we have a common basis of understanding:
- You have a rotated PDF page
- You have a rectangle given in rotated coordinates and need to extract text of the page contained in that rectangle
Is this right? Otherwise please specify ...
btw I have seen you work for Accenture ... me too until 2009 (Germany)
Okay, let's make it more clear so that we are on the same page. Do not think about what generic template I was talking about before, I just want you to understand the problem on the PDF orientation only. For better understanding, please refer to the image below.

Let me know if you get what I actually want.
And for the record, I tried setRotation(0), but that simply rotates my text too. JFYI, I am trying to extract ARABIC words and numbers from the PDF.
btw I have seen you work for Accenture ... me too until 2009 (Germany)
Hey, nice to virtually meet you, haha! Cheers! 💯
There are different things controlling a page's behaviour:
- rotation
- inside the source code that controls the page appearance (happens in the
/Contentsobject(s)), geometry / orientation may be changed by so-called matrices.
Point 2. is significant only when insrting your own on the page. If geometry changes have not been properly wrapped, your stuff will land in nirwana.
Rotation itself is not important or text extraction, because I will always treat the page as non-rotated. Same is true for returned image and annotation coordinates: they all are given unrotated.
Likewise, if you want to insert text, annotations, images, ..., you must provide coordinates as if the page were not rotated. If you e.g. have a rectangle in rotated coordinates, then use the unrotated version of it in stead, calculated like so: rect_unrotated = rect_rotated * page.derotationMatrix.
If non-encapsulated geometry changes exist, they must be neutralized before your first insertion.
I assume your inquiry has been answered ... otherwise, please re-open or open a new issue.