0

Using PyMuPDF, is there any way to get the page margins? I mean the distance from the edge of the page to the nearest horizontal/vertical element, depending on whether it is left/right or top/bottom margin.

Looking at the documentation I don't see any native way to do it, but maybe by iterating through all the page elements and checking which one has higher/lower coordinates?

Kikolo
  • 212
  • 1
  • 10
  • 2
    You have the right idea: These values are not stored anywhere in a PDF, so the only approach left is iterating over all pages. Often one will find that text borders are not necessarily the same as image distances kept from page borders, etc. PyMuPDF has a fast method that only delivers the boundary boxes per box type `page.get_bboxlog()`, without first reading text or extracting images or vector graphics. This allows you to compute borders per any of these types. – Jorj McKie Jan 29 '23 at 01:32
  • 1
    For the record: I have implemented @JorjMcKie 's suggestion and it works like a charm. The only precaution to take is that, when iterating trough `age.get_bboxlog()` you have to skip the element corresponding to the `rect` of `page` itself – Kikolo Jan 29 '23 at 04:25

0 Answers0