views:

260

answers:

2

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.

A: 

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
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
+1  A: 

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 dimensions 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)

Aman
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