Change PDF coordinate system

I want to change the PDF coordinate system to change the the origin point (0,0) -> from the left-bottom to the left-top. So, for example, when I will write text in the position x=5 y=10, it will be written in 10 points counting from left and 10 points counting from up (instead of from down).

I have been read the PDF specification and it talks about CropBox, Rotate, coordinate transformation ('cm' operator)... But I cannot be able to do it.

Note. In eps I do something similar using a translation and scaling:

% Rescaling and translate
0 95<-(height of the page) translate
1 -1 scale

Any help will be appreciate!

Upvotes: 4

Views: 1968

Answers (1)

David van Driessche
David van Driessche

Reputation: 7048

PDF is not a programming language such as PostScript but the exact same principles as what you have in your example apply here as well. Instead of through different PostScript calls ("translate" and "scale"), you'll set a transformation matrix

1 0 0 -1 0 95 cm

This has as effect that the X-axis remains the same, the Y-axis is inverted and the origin of the coordinate system is shifted, so exactly the same as what would happen in your EPS example.

You could change the transformation matrix in the beginning of your page, or you could change the transformation matrix for each individual element or for groups of elements.

Remember that the "cm" operator always concatenates with the existing transformation matrix, it doesn't set it. There is actually no way to set the matrix.

So instead in almost any PDF file you'll find constructs like

q
1 0 0 -1 0 95 cm
...
Q

This saves the current graphics state; changes the matrix and then does whatever operations you want and finally replaces the matrix to the original value.

Also, as stated in comments by Jongware and mkl; if you flip the coordinate system like this, it may very well require you to also flip other matrices such as the text transformation matrix in order to make sure the objects you draw are right side up.

Upvotes: 4

Related Questions