问题
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.
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/2288028/are-pdf-box-coordinates-relative-or-absolute