Image rotation via itext pdf library

↘锁芯ラ 提交于 2019-12-12 12:59:19

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30498201/image-rotation-via-itext-pdf-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!