PyMuPDF icon indicating copy to clipboard operation
PyMuPDF copied to clipboard

General Question: Changing origin co-ordinates of a PDF

Open karanbalani opened this issue 6 years ago • 9 comments

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.

karanbalani avatar Aug 07 '20 08:08 karanbalani

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.

JorjMcKie avatar Aug 07 '20 09:08 JorjMcKie

You can also simply reset the page's rotation and work as usual: page.setRotation(0). Won't harm anything.

JorjMcKie avatar Aug 07 '20 09:08 JorjMcKie

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.

karanbalani avatar Aug 07 '20 15:08 karanbalani

First let's make sure we have a common basis of understanding:

  1. You have a rotated PDF page
  2. 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 ...

JorjMcKie avatar Aug 07 '20 15:08 JorjMcKie

btw I have seen you work for Accenture ... me too until 2009 (Germany)

JorjMcKie avatar Aug 07 '20 15:08 JorjMcKie

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.

image

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.

karanbalani avatar Aug 07 '20 15:08 karanbalani

btw I have seen you work for Accenture ... me too until 2009 (Germany)

Hey, nice to virtually meet you, haha! Cheers! 💯

karanbalani avatar Aug 07 '20 15:08 karanbalani

There are different things controlling a page's behaviour:

  1. rotation
  2. inside the source code that controls the page appearance (happens in the /Contents object(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.

JorjMcKie avatar Aug 07 '20 15:08 JorjMcKie

I assume your inquiry has been answered ... otherwise, please re-open or open a new issue.

JorjMcKie avatar Aug 12 '20 19:08 JorjMcKie