PDF 页面的原点 (x,y) 在哪里? [英] Where is the Origin (x,y) of a PDF page?

查看:36
本文介绍了PDF 页面的原点 (x,y) 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 iText 创建我的 PDF 文件.

I am using iText to create my PDF files.

我想使用我在此处找到的方法将文本定位在文档中的某个特定位置:itext绝对定位文本.

I want to position text at some specific place in the document using the method I found here: itext positioning text absolutely.

但是,我不知道在哪里可以找到页面坐标系的原点.

However, I cannot figure out where to find the origin of the coordinate system of the page.

左下角?右上角?右下角?左上角?

这个起源在哪里?

推荐答案

页面的尺寸(也就是页面边界)在页面字典中定义:

The dimensions of a page (aka the page boundaries) are defined in a page dictionary:

  • /MediaBox:物理媒体(页面)的边界.此值是强制性的,因此您可以在每个 PDF 中找到它.
  • /CropBox:显示或打印时可见的区域./CropBox 等于或小于 /MediaBox.该值是可选的;如果缺少,/CropBox 等于 /MediaBox.
  • 其他可能的值是 /BleedBox/TrimBox/ArtBox.这些是为特定目的而定义的,但它们不再经常使用.如果它们丢失,它们默认为 /CropBox.这些值都不能超过 /CropBox.
  • /MediaBox: the boundaries of the physical medium (the page). This value is mandatory, so you'll find it in every PDF.
  • /CropBox: the region that is visible when displayed or printed. The /CropBox is equal to or smaller than the /MediaBox. This value is optional; if it's missing, the /CropBox is equal to the /MediaBox.
  • Other possible values are the /BleedBox, /TrimBox and /ArtBox. These have been defined for specific purposes, but they aren't used that often anymore. If they are missing, they default to the /CropBox. None of these values can outsize the /CropBox.

当您使用 iText 创建文档时,您可以显式或隐式地定义 /MediaBox.

When you create a document with iText, you define the /MediaBox either explicitly or implicitly.

明确:

Rectangle rect = new Rectangle(20, 20, 300, 600);
Document document = new Document(rect);

隐式:

Document document = new Document();

这一行相当于:

Rectangle rect = new Rectangle(0, 0, 595, 842);
Document document = new Document(rect);

传递给Rectangle构造函数的四个参数(llxllyurxury) 使用左下角和右上角的 x 和 y 坐标定义一个矩形.

The four parameters passed to the Rectangle constructor (llx, lly, urx, ury) define a rectangle using the x and y coordinates of the lower-left and the upper-right corner.

对于new Rectangle(0, 0, 595, 842),页面左下角与坐标系原点重合(0, 0).页面右上角与坐标(595, 842)重合.

In case of new Rectangle(0, 0, 595, 842), the lower-left corner of the page coincides with the origin of the coordinate system (0, 0). The upper-right corner of the page coincides with the coordinate (595, 842).

所有度量均以用户单位定义,默认情况下,用户单位大致对应于印刷点:1 个用户单位 = 1 个点.

All measurements are defined in user units, and by default, the user units roughly corresponds with the typographic point: 1 user unit = 1 point.

注意粗略这个词:我们使用点来进行计算,但是在ISO标准中,我们非常谨慎地不要将点用作用户单位的同义词.例如:A4 页面的尺寸为 595 x 842 个用户单位,但如果您以点为单位计算精确值,则会有细微差别(点后的一些数字).

Note the word roughly: we use points to do calculations, but in the ISO standard, we are very cautious not to use point as a synonym for user unit. For instance: an A4 page measures 595 by 842 user units, but if you'd calculate the exact value in points, there would be a slight difference (some numbers after the point).

页面的左下角并不总是坐标系的原点.如果我们使用 Rectangle(20, 20, 300, 600) 定义一个页面,原点是下方 20 个用户单位和左下角左侧 20 个用户单位.也可以使用负值来定义页面大小.

The lower-left corner of the page isn't always the origin of the coordinate system. If we define a page using Rectangle(20, 20, 300, 600), the origin is 20 user units below and 20 user units to the left of the lower-left corner. It's also possible to use negative values to define a page size.

例如:假设您要创建一个由 4 个 A4 页面组成的 A2 文档,那么您可以像这样定义页面大小:

For instance: suppose that you want to create an A2 document consisting of 4 A4 pages, than you could define the page sizes like this:

Rectangle(-595, 0, 0, 842)   Rectangle(0, 0, 595, 842)
Rectangle(-595, -842, 0, 0)  Rectangle(0, -842, 595, 0);

通过像这样定义媒体框,您还可以传递有关不同页面的相对位置的信息.如果将 4 张 A4 页面视为一个单元,则坐标系的原点是 A2 页面的确切中心.

By defining the media box like this, you also pass information with respect to the relative position of the different pages. If you look at the 4 A4 pages as a unit, the origin of the coordinate system is the exact center of the A2 page.

重要提示:

以上所有内容都假设您没有引入任何坐标变换,例如使用 concatCTM()transform() 方法.这些方法允许您更改坐标系,例如将 x 和 y 轴之间的角度从 90 度(默认)更改为另一个角度.您还可以缩放轴以获得不同的纵横比.虽然这样做确实很有趣,但它需要相当多的数学.

All of the above assumes that you didn't introduce any coordinate transformations, e.g. using the concatCTM() or transform() method. These methods allow you to change the coordinate system, for instance change the angle between the x and y axis from 90 degrees (the default) to another angle. You can also scale an axis to get a different aspect ratio. While it's certainly fun to do this, it requires quite some Math.

这篇关于PDF 页面的原点 (x,y) 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆