3

I am unable to rotate image from center or any fixed point by using itext pdf library in java program.When i rotate image it's x and y cordinate gets changed. Kindly help me in this regard.

   Image pdfImage=Image.getInstance("assets/product.png");
    pdfImage.setAlignment(Element.ALIGN_CENTER);
    pdfImage.setRotationDegrees(30);
    document.add(pdfImage);
   pdfImage.setRotationDegrees(140);
    document.add(pdfImage);

In above code there is no same point by which I can judge the rotation point.

Thanks in advance

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97

1 Answers1

3

When you set a rotation using the setRotationDegrees() method, the image is rotated using the lower-left corner of the image as its rotation point. If you want another rotation point, you'll need to work with low-level functionality to change the CTM. See the different addImage() methods in the PdfContentByte class for more info:

  • addImage(Image image, AffineTransform transform) adds an Image with the given transformation defined using the com.itextpdf.awt.geom.AffineTransform class.
  • addImage(Image image, float a, float b, float c, float d, float e, float f) adds an Image using a CTM that is defined by the values a, b, c, d, e and f which are elements of a 3 by 3 matrix. E.g. e and f define the translation.

For more detailed info on the coordinate system and the transformation matrices, please read The ABC of PDF with iText. The book isn't finished yet, but it's free and the part you need is already there.

If you want to define the rotation yourself, you need to understand two very important concepts in PDF:

  • The origin of the coordinate system is defined by the MediaBox. If the mediabox is defined like this [0 0 595 842] (which is an A4 page) and there is no cropbox, then the origin of the coordinate system will be the lower-left corner of your page. The upper-right corner will have the coordinate (x = 595; y = 842).
  • In PDF, you don't rotate objects. Instead you rotate the coordinate system. When you add an object to a rotated coordinate system, it looks as if the objects are rotated.

All of this is explained in ISO-32000-1 and in the ABC book I started writing.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I have studied your reference carefully. Now I am able to rotate my Image from bottom left point but rotation must be in multiple of 90 degree to get proper image, If i change the rotation degree my image is also get changed. – roushan kumar Singh May 28 '15 at 09:37
  • Still I am unable to rotate image from center. – roushan kumar Singh May 28 '15 at 09:38
  • It's only a matter of Math! Surely you've had algebra and analytic geometry in high school? If not: unfortunately I don't have the time to be your Math teacher today. – Bruno Lowagie May 28 '15 at 09:48
  • have you got any anwer @roushankumarSingh.how to acheive the roattion from centre. – abh22ishek Nov 17 '15 at 11:02
  • @abh22ishek Why hijack an old question to ask a new question? Add an extra tag such as computational-geometry so that you get the attention of Math experts as this isn't a PDF questions: it's a Math question. – Bruno Lowagie Nov 17 '15 at 11:05
  • this book is not free, i couldn't find the related things on sample part – abh22ishek Nov 17 '15 at 11:07
  • @abh22ishek iText isn't free either if you're using it in a commercial context, but you get the book for free if you purchase a commercial license. Anyway: the Math is explained in this unfinished book: [The ABC of PDF with iText](https://leanpub.com/itext_pdfabc). You can get that book for free. The chapter that describes the coordinate system and transformations is finished, so you may want to read that. – Bruno Lowagie Nov 17 '15 at 11:09
  • i couldn't find that chapter in free sample,is it there in free sample or i have to buy the book – abh22ishek Nov 17 '15 at 11:18
  • All the Math is explained in section 4.2.5.1 Transforming the coordinate system. Are you saying you didn't find section 4.2.5.1 in chapter 4? Note that you have to "buy" the book, but you can choose to "pay" $0.00 (which means it's free). – Bruno Lowagie Nov 17 '15 at 11:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95336/discussion-between-abh22ishek-and-bruno-lowagie). – abh22ishek Nov 17 '15 at 11:49