PDF 框坐标是相对坐标还是绝对坐标?

发布于 2024-08-22 00:24:49 字数 333 浏览 3 评论 0原文

我想使用 pyPDF 以编程方式编辑 PDF。目前,我正在努力解释各种 PDF 框(TrimBox、MediaBox 等)的尺寸。每个盒子都有四个维度存储为四元组,例如:

TrimBox:           56.69    56.69  1040.31   751.18

根据 PDF 规范,这些维度应该描述一个矩形,并且当然 (56.69, 56.69) 确定了该矩形的左上角。然而,(1040.31, 751.18) 是被解释为这个矩形的右下角,还是相对于左上角的向量?

显然,这个答案在排字工中是如此众所周知,以至于到目前为止我在任何地方都找不到明确的拼写。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄兵必败 2024-08-29 00:24:49

正如 Mark Storer 和其他人正确评论的那样,四个框值将被解释为 (左开始、下开始、右结束、顶端),因为 PDF 格式使用绝对坐标。所以 (MediaBox[0], MediaBox[1]) 是框的左下角,(MediaBox[2] and MediaBox[3]) 是框的右上角。
MediaBox[2]MediaBox[3] 仅在 MediaBox[0]MediaBox[1] 时表示宽度和高度code> 包含值 0,不应依赖该值。

此外,PDF 旋转会修改整个坐标系而不仅仅是页面,因此 PDF 框始终引用未旋转的页面。因此,如果旋转 90 度或 270 度,则需要交换宽度和高度才能获得盒子的视觉尺寸。

坐标值称为点,默认情况下 1 点相当于 1/72 英寸。但是,也不应依赖于此,因为每个页面都可以定义自定义 UserUnit(自 PDF 1.6 起),如 PDF 参考手册

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.

潦草背影 2024-08-29 00:24:49

大牛,由于左下角是坐标系的原点,只要TrimBox的左下角位于原点(即当 (x1,y1) = (0,0)) 时。

顺便说一句,我花了一些时间才发现所使用的单位是点——这在 PDF 规范文档中我可以找到,但并没有明确说明。显然,它不是物理学家写的。 http://en.wikipedia.org/wiki/Point_(typography)

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)

暗恋未遂 2024-08-29 00:24:49

经过一些额外的修改后,我实际上找到了我的问题的两个答案。就pyPDF源而言,四个框坐标应读取为(x1,y1,x2,y2),其中前两个代表左下角,后两个代表右上角。

然而,当我将坐标解释为 (x, y, w, h) 时,在 PDF 的 TrimBox 内绘图效果非常好,其中 (x, y) 是左上角,(w, h) 是矩形的宽度和高度源于那里。

所以,我可能第一个解释是错误的,但至少第二个对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文