Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Image element (com.itextpdf.layout.element.Image) supports anticlockwise rotation. Is it possible to make a clockwise rotation of the same image?

PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
img.scaleAbsolute(525.58203, 737.0079)
img.setFixedPosition(30.785828, 34.66619)
// The following block of code does not affect the center point of rotation.
// I tried a lot of different values for rotation point. No change!
  img.setProperty(Property.ROTATION_POINT_X, 30.785828);
  img.setProperty(Property.ROTATION_POINT_Y, 34.66619);
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(90)); //img.setRotationAngle(Math.toRadians(90));
canvas.add(img);

Update:

This is what happens with the image using 90 degrees counter clockwise.

This is what happens with the image using -90 or 270 degrees counter clockwise.

Why would you complicate things by creating two functions for something that can be done with only one function?

(The latter remark was influenced by the keynote by Venkat Subramaniam at the Great Indian Developer Summit this morning. The title of the keynote was: "Do Not Walk Away from Complexity, Run!")

Update:

After your initial comment (I tried with 270 as well. For a reason I do not understand, the image has been rotated clockwise but it is positioned under the bottom of the pdf Page.), I made this image:

In your second comment, you wrote: You are right! That means that I have to set the position again so that the image to be displayed into the pdf page. How can I move the image, so that to be moved above the bottom of the page?

That probably depends on how you are positioning the image in the first place. What are you currently using? Are you using the setFixedPosition() or the setRelativePosition() method? Or are you just adding the image to the document without defining a position?

I tried with 270 as well. For a reason I do not understand, the image has been rotated clockwise but it is positioned under the bottom of the pdf Page. – Ap Tsi Apr 25, 2018 at 16:35 If the image is at the bottom of the page, then it makes sense that rotating the image 90 percent clockwise positions the image under the bottom of the page. Why do you think that's wrong? – Bruno Lowagie Apr 25, 2018 at 16:37 You are right! That means that I have to set the position again so that the image to be displayed into the pdf page. How can I move the image, so that to be moved above the bottom of the page? – Ap Tsi Apr 25, 2018 at 16:40 I am using setFixedPosition(). It should be something like setFixedPosition(x,y). x is the same like before. y should be the width of the image object. – Ap Tsi Apr 25, 2018 at 18:02 Yes, if you create a PDF from scratch, the default y coordinate for the bottom of the page is 0, and the y axis points upwards. – Bruno Lowagie Apr 26, 2018 at 1:02

Set an image in page with different rotation and display the same result.

It is an example, you can accordingly set x, y .

itextpdf version 7.0.2

    public Image imageWithPosition(int pageNum, PdfPage pdfPage, ImageData imageData) {
        val pageHeight = pdfPage.getPageSizeWithRotation().getHeight();
        val pageWidth = pdfPage.getPageSizeWithRotation().getWidth();
        Image image = new Image(imageData).scaleAbsolute(pageWidth, pageHeight);
        float x = 0f;
        float y = 0f;
        val rotation = pdfPage.getRotation();
        // rotation 180, 270 need to handle specially
        if (rotation == 180) {
            y = pageHeight;
        } else if (rotation == 270) {
            y = pageWidth;
        // set fixed position
        image.setFixedPosition(pageNum, x, y);
        // toRadians 
        image.setRotationAngle(Math.toRadians(pdfPage.getRotation()));
        return image;

That line scales the image to be fitted into the container with

width = 525.58203 and height = 737.0079.

The following block of code makes what I need!

PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), 
page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
float width = img.getXObject().getWidth();
float widthContainer = 525.58203;
float heightContainer = 737.0079;
float horizontalScaling = widthContainer / width;
img.scaleAbsolute(widthContainer, heightContainer);
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(270));
img.setFixedPosition(imageLlx, imageLly + width * horizontalScaling);
canvas.add(img);

The result looks like:

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.