Rotating an image via itext pdf library

I am unable to rotate an image from the center or any fixed point using itext pdf library in java program. When I rotate the image, it changes the x and y cordinate. Please 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 the above code, there is no point at which I can judge the pivot point.

Thank you in advance

+3


source to share


1 answer


When you set the rotation using the method setRotationDegrees()

, the image is rotated using the lower left corner of the image as the pivot point. If you need a different pivot point, you will need to work with low-level functions to change the CTM. For more information, see the Various methods addImage()

in the PdfContentByte

class
:

  • addImage(Image image, AffineTransform transform)

    adds Image

    with the given transform defined by the class com.itextpdf.awt.geom.AffineTransform

    .
  • addImage(Image image, float a, float b, float c, float d, float e, float f)

    He adds Image

    , using the CTM, which is determined by the values a

    , b

    , c

    , d

    , e

    and f

    , which are elements 3 3. For example. e

    and f

    define the translation.

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



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

  • The origin of the coordinate system is determined by the MediaBox. If the mediabox is defined as this [0 0 595 842]

    (which is an A4 page) and there is no cropping, then the origin will be the bottom left corner of your page. The top right corner will be (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 that I started writing.

+2


source







All Articles