0

I want to programmatically edit a PDF using pyPDF. Currently, I'm struggling with interpreting the various PDF boxes' (TrimBox, MediaBox etc.) dimensions. Each box has four dimensions stored as a four-tuple, e.g.:

TrimBox:           56.69    56.69  1040.31   751.18

According to the PDF specification, these are supposed to describe a rectangle, and certainly (56.69, 56.69) determines the upper left corner of this rectangle. However, is (1040.31, 751.18) to be interpreted as the lower right corner of this rectangle, or as a vector relative to the upper left corner?

Apparently the answer is so well-known among typesetters that I couldn't find it explicitly spelt out anywhere I looked so far.

Daniel Werner
  • 1,350
  • 16
  • 26

3 Answers3

5

As Mark Storer and others commented correctly, the four box values are to be interpreted as (left start, bottom start, right end, top end), since the PDF format uses absolute coordinates. So (MediaBox[0], MediaBox[1]) is the bottom left corner and (MediaBox[2] and MediaBox[3]) the upper right corner of the box. MediaBox[2] and MediaBox[3] only represent width and height if MediaBox[0] and MediaBox[1] contain value 0, which should not be relied on.

Moreover, PDF rotation modifies the whole coordinate system rather than just the page, thus PDF boxes always refer to the non-rotated page. So if there is a rotation of 90 or 270 degrees, you need to swap width and height in order to obtain the visual dimensions of the box.

The coordinate values are called points, where by default 1 point is equivalent to 1/72 of an inch. However, this should not be relied on either, because each page can define a custom UserUnit (since PDF 1.6), as outlined in the PDF Reference Manual.

mara004
  • 1,435
  • 11
  • 24
  • Thanks for your answer. This finally clarifies everything that was going on. 11 years after I asked the question, no less Marking this answer as accepted. – Daniel Werner Sep 09 '21 at 20:38
  • 1
    Missing/wrong in my answer: It doesn't necessarily need to be bottom left and upper right corner. Usually this is the case, but in theory it could be any opposite corners. – mara004 Oct 06 '22 at 18:53
4

Daniel, since the lower left is the origin of the coordinate system, treating (x1,y1,x2,y2) as (x,y,w,h) works as long as the bottom left corner of the TrimBox is at the origin (that is, when (x1,y1) = (0,0)).

BTW, it took some hunting to find that the units used are Points -- which is not made clear, that I could find, in the PDF specifications document. Clearly, it was not written by a physicist. http://en.wikipedia.org/wiki/Point_(typography)

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Aman
  • 45,819
  • 7
  • 35
  • 37
  • Thanks for the information, Aman. It is still unclear to me, though, why treating the coordinates as (x, y, w, h) in my positioning algorithm works even when (x1, y1) = (56.69, 56.69). Perhaps the lower left corner coordinates have been near enough to the origin so far that the small difference made no noticeable difference. – Daniel Werner Oct 24 '10 at 09:38
  • 1
    The units are _not_ "points". The units are user defined and _default_ to 1/72 inch. See section 4.2.1 Coordinate Spaces of the PDF spec. – Bob Reynolds Feb 14 '16 at 23:25
  • 1
    The values used to define a rectangle are the coordinates of opposite corners of the rectangle being defined. The values may be (lower left, upper right) or (upper left, lower right); see section 3.8.4 Rectangles of the PDF spec. The spec can be found at http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf – Bob Reynolds Feb 14 '16 at 23:34
0

After some additional tinkering, I actually found two answers to my question. As far as the pyPDF sources are concerned, the four box coordinates should be read as (x1, y1, x2, y2), where the first two represent the lower left corner and the latter two represent the upper right corner.

However, drawing inside the PDF's TrimBox worked perfectly well when I interpreted the coordinates as (x, y, w, h), where (x, y) is the upper left corner and (w, h) the width and height of the rectangle that originates from there.

So, I might have gotten the first interpretation wrong, but at least the second one works for me.

Daniel Werner
  • 1,350
  • 16
  • 26
  • 4
    Your first interpretation is correct. So long as x1, y1 is (0, 0), then the two interpretations produce identical rectangles. This isn't always the case. Do **not** depend on it. PS: PDF units are in "points", which are 1/72 of an inch. 8.5" x 11" = 612pt x 792pt – Mark Storer Oct 26 '10 at 00:31