Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

java - Where is the Origin (x,y) of a PDF page?

I am using iText to create my PDF files.

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.

bottom left corner? top right corner? bottom right corner? top left corner?

Where is this origin?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • /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.

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

Explicitly:

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

Implicitly:

Document document = new Document();

This single line is equivalent to:

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

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.

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

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

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

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.

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

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.

Important:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...