Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image rotation with PDFBox

I am quit new to using PDFBox. What I need is to add an image with rotation to an exiting PDF! I know how to add the image, but my problem is how to rotate the image! I've seen somethign about AffineTransform and Matrix but I have no idea what is that and how it works!

I'd really appreciate passing some sample code, and thank you in advance!

Best Regards

like image 496
user256872 Avatar asked May 26 '14 09:05

user256872


1 Answers

It helps to look at the source of the "simple" image display method:

 public void drawXObject(PDXObject xobject, float x, float y, float width, float height)
 {
     AffineTransform transform = new AffineTransform(width, 0, 0, height, x, y);
     drawXObject(xobject, transform);
 }

so this is what you do to display an image at (200,200) with a rotation of 45°:

 AffineTransform at = new AffineTransform(ximage.getWidth(), 0, 0, ximage.getHeight(), 200, 200);
 at.rotate(Math.toRadians(45));
 contentStream.drawXObject(ximage, at);

Re: AffineTransform: this is a subtopic of geometry. To get an introduction, read the java description here.

like image 77
Tilman Hausherr Avatar answered Nov 11 '22 01:11

Tilman Hausherr