Chris Wade

Chris developed much of the Forms handling code and also the hooks for the XFA.

Using Transforms to Convert PDF Co-ordinates to JAVA co-ordinates

1 min read

I was fixing a display issue in our PDF JAVA renderer the other day and thought you might like to understand more about the differences between PDF co-ordinates and JAVA co-ordinate system’s.

Well JAVA is a top, left system, which means 0,0 is in the top, left corner of the screen.
But the PDF specification defines its system as lower, left, this means co-ordinate 0,0 is in the bottom, left corner of the screen.

We want to work in the PDF coordinates as we are dealling entirly with PDF’s and not other co-ordinate systems, so you can see that we have to turn the java drawing surface so that we can draw our PDF objects in the correct places.

JAVA has these Graphics properties called AffineTransforms, which are really useful for us in this case.

To convert the JAVA space to PDF space we have to invert the display, thus moving the 0,0 to the bottom left. You could be forgiven for thinking that turning it by -90 degrees was the answer but if you plot 20 across by 40 up, (20,40) you will see what i mean.

plots of the examples in each system
This shows what i mean, as the 90 degree retated space will have x co-ordinates that go up the display and y co-ordinates that go across the screen.

That shows you the basic transform that needs applying to all pdf’s, but then we have to deal with the PDF mediabox, cropbox and even trim(rare) boxes, these are points which define a box on the page.┬áThe media box is the initial page bounds box, so everthing on that page should be within these points.

The crop or trim box generally tells you what on the page is going to be visible.
These boxes can be any values, so 0,0,580,720 is a good standard box, but also -700,-300,10,10 is a perfectly valid box.

Coupled with all this, the PDF offers the ability to rotate every PDFObject differently and then even to say that each object is positions in a different rotation again.

Graphics2D is a very powerful library and the AffineTransform is very useful for making things simple. Do you have any clever tricks with using Graphics2D in JAVA?

Chris Wade

Chris developed much of the Forms handling code and also the hooks for the XFA.

Leave a Reply

Your email address will not be published. Required fields are marked *