I had the same issue and this thread was what got me to the solution, thank you very much!
To close the loop on Patrick's very last open question here, I did have to find out the new X and Y coordinates based on the angle because the whole canvas was rotated. I did this by applying a negative theta rotation to the X and Y coordinates. My eventual solution looked like: public void drawImage(PDImageXObject image, double theta, float x, float y, float width, float height) { contentStream.saveGraphicsState(); Matrix rotate = new Matrix(); rotate.rotate(theta); contentStream.transform(rotate); Point bottomLeft = new Point(x, y); bottomLeft.applyRotation(-1 * theta); contentStream.drawImage(image, bottomLeft.getX(), bottomLeft.getY(), width, height); contentStream.restoreGraphicsState(); } Which can be found here (https://github.com/mressler/dominionCases/blob/master/src/main/java/com/ressq/pdfbox/helpers/ContentStream.java) And Point's applyRotation method looks like: public void applyRotation(double theta) { Double xPrime = x * Math.cos(theta) - y * Math.sin(theta); Double yPrime = y * Math.cos(theta) + x * Math.sin(theta); x = xPrime.floatValue(); y = yPrime.floatValue(); } The full source code can be found at https://github.com/mressler/dominionCases. I'm glad this thread was in the first page of search results for this issue! I hope this helps someone else down the road. Just out of curiosity, why DOES the AffineTransformation squish the image? Shouldn't it preserve the width and height and just rotate the image? On 2016-03-07 12:51, "Stahle, Patrick" <patrick.sta...@te.com> wrote: > Hi Tillman, > > Correct me if I am wrong, but it looks like the following instructions: > matrix.rotate(45); > canvas.transform(matrix); > Rotates the whole page canvas. So now the stream thinks as in the above > example the page goes up at a 45 degree angle. So cord. 0,0 is still in lower > left hand corner, but if I move my x cord only it will move from the lower > left corner at a 45 degree angle towards the upper right hand corner. So for > me to put my image in the lower right hand corner I am going to have to > transform my x, y coordinates (y, will go negative in example) to put it in > the right position. So simply I have to figure out the new x,y based off the > angle.... > > -----Original Message----- > From: Tilman Hausherr [mailto:thaush...@t-online.de] > Sent: Monday, March 07, 2016 12:24 PM > To: users@pdfbox.apache.org > Subject: Re: drawing images with rotation PDFBox 2.0 > > rotation is around the lower left axis. That is why I wrote a few days > ago: "90° rotated in clock direction would be you'd have to adjust the Y > value, i.e. add the width to it." > > Additionally, the parameter of rotate() is in radians, not in degrees. > Use Math.toRadians(). > > Tilman > > Am 07.03.2016 um 16:17 schrieb Stahle, Patrick: > > Hi, > > > > With the following code: > > Matrix matrix = new Matrix(); > > matrix.rotate(45); > > canvas.saveGraphicsState(); > > canvas.transform(matrix); > > canvas.drawImage(ximage, rect.getLowerLeftX(), rect.getLowerLeftY(), > > rect.getWidth(), rect.getHeight()); > > System.out.println("rect=" + rect + ", page mediabox=" + > > page.getMediaBox()); > > canvas.restoreGraphicsState(); > > > > System out: > > rect=[485.01,52.8,581.4,89.214], page mediabox=[0.0,0.0,612.0,792.0] > > > > I would expect my image to show up in the lower right hand corner, but I am > > seeing about mid way down on the left hand side. Is the x, y position not > > starting form the bottom left? > > > > Thanks, > > Patrick > > > > -----Original Message----- > > From: Stahle, Patrick [mailto:patrick.sta...@te.com] > > Sent: Monday, March 07, 2016 9:57 AM > > To: users@pdfbox.apache.org > > Subject: RE: drawing images with rotation PDFBox 2.0 > > > > Ok, I am lot closer. This seems to work. The only thing right now is I > > don't think the position is correct, but I have to look at that a bit > > more... > > Seems like I have to first rotate & transform and then use the drawImage > > and pass a width & height to scale it. > > > > Matrix matrix = new Matrix(); > > matrix.rotate(45); > > canvas.saveGraphicsState(); > > canvas.transform(matrix); > > canvas.drawImage(ximage, rect.getLowerLeftX(), rect.getLowerLeftY(), > > rect.getWidth(), rect.getHeight()); > > canvas.restoreGraphicsState(); > > > > -----Original Message----- > > From: Stahle, Patrick [mailto:patrick.sta...@te.com] > > Sent: Monday, March 07, 2016 8:05 AM > > To: users@pdfbox.apache.org > > Subject: RE: drawing images with rotation PDFBox 2.0 > > > > I don't know how to do the rotation without AffineTransform, do you have a > > suggestion for at.rotate method call? > > > > -----Original Message----- > > From: Tilman Hausherr [mailto:thaush...@t-online.de] > > Sent: Friday, March 04, 2016 3:51 PM > > To: users@pdfbox.apache.org > > Subject: Re: drawing images with rotation PDFBox 2.0 > > > > Am 04.03.2016 um 21:27 schrieb Stahle, Patrick: > >> From the code below both images draw, but as soon as I uncomment out " > >> canvas.transform(new Matrix(at));" the first image does not draw or draws > >> where I can't see it. I must still be missing something? > >> > >> AffineTransform at = new AffineTransform(rect.getWidth(), 0, 0, > >> rect.getHeight(), 0, 0); > > Don't use the line above! Just create a pure rotation matrix. > > > >> PDPageContentStream canvas = new PDPageContentStream(document, page, > >> PDPageContentStream.AppendMode.APPEND, true, true); > >> at.rotate(Math.toRadians(0)); > >> canvas.saveGraphicsState(); > >> //canvas.transform(new Matrix(at)); > >> canvas.drawImage(ximage, 100 /*rect.getLowerLeftX()*/, 100 > >> /*rect.getLowerLeftY()*/); > >> canvas.restoreGraphicsState(); > >> > >> canvas.saveGraphicsState(); > >> AffineTransform at2 = new AffineTransform(rect.getWidth(), 0, 0, > >> rect.getHeight(), rect.getLowerLeftX(), rect.getLowerLeftY()); > > That line above too, don't use it. > > > > Tilman > > > > > >> at.rotate(Math.toRadians(0)); > >> canvas.drawXObject(ximage, at2); > >> canvas.restoreGraphicsState(); > >> canvas.close(); > >> > >> -----Original Message----- > >> From: Tilman Hausherr [mailto:thaush...@t-online.de] > >> Sent: Friday, March 04, 2016 3:08 PM > >> To: users@pdfbox.apache.org > >> Subject: Re: drawing images with rotation PDFBox 2.0 > >> > >> Am 04.03.2016 um 21:04 schrieb Stahle, Patrick: > >>> I tried the following but the image now no longer draws... > >>> AffineTransform at = new AffineTransform(rect.getWidth(), 0, 0, > >>> rect.getHeight(), rect.getLowerLeftX(), rect.getLowerLeftY()); > >>> PDPageContentStream canvas = new PDPageContentStream(document, page, > >>> PDPageContentStream.AppendMode.APPEND, true, true); > >>> at.rotate(Math.toRadians(90)); > >>> canvas.saveGraphicsState(); > >>> canvas.transform(new Matrix(at)); > >>> canvas.drawImage(ximage, rect.getLowerLeftX(), rect.getLowerLeftY()); > >>> canvas.restoreGraphicsState(); > >>> canvas.close(); > >> No, what I meant is make a transform that has only the rotation. Then > >> draw the image at the position you're planning (however you may have > >> to adjust this, as the rotation is done around the (0,0) axis) > >> > >> The best would be to set a position like (300,300) which is about in the > >> middle and see what happens. > >> > >> 90° rotated in clock direction would be you'd have to adjust the Y value, > >> i.e. add the width to it. > >> > >> TIlman > >> > >>> Did I misunderstand something? > >>> > >>> As for the imaging squishing I am seeing. It looks to me like the > >>> rectangle size / position of the image non rotated stays exactly the same > >>> but the contents are rotated and squished. I can send you a couple pdfs > >>> showing what I mean (direct email?). And maybe that is how it is supposed > >>> to work, but I would of expected the image to look exactly the same just > >>> rotated. In case of 90 degrees, like the example above, I would of expect > >>> simply the width to become the height and the height to become the width. > >>> > >>> -----Original Message----- > >>> From: Tilman Hausherr [mailto:thaush...@t-online.de] > >>> Sent: Friday, March 04, 2016 2:44 PM > >>> To: users@pdfbox.apache.org > >>> Subject: Re: drawing images with rotation PDFBox 2.0 > >>> > >>> Am 04.03.2016 um 20:35 schrieb Stahle, Patrick: > >>>> Hi, > >>>> > >>>> I am struggling with rotating an image. For instance I have the > >>>> following code: > >>>> AffineTransform at = new AffineTransform(rect.getWidth(), 0, 0, > >>>> rect.getHeight(), rect.getLowerLeftX(), rect.getLowerLeftY()); > >>>> PDPageContentStream canvas = new PDPageContentStream(document, page, > >>>> PDPageContentStream.AppendMode.APPEND, true, true); > >>>> at.rotate(Math.toRadians(90)); > >>>> canvas.drawXObject(ximage, at); > >>>> canvas.close(); > >>>> > >>>> It seems to work, but not the way I would've expected it to. It rotates > >>>> the image but keeps the original boxed rectangle size which in this case > >>>> squishing the image. Is this expected behavior, and if so is there way > >>>> for an image to rotate and keep the sizing? I kind of hoped it work the > >>>> same way as rotating text... > >>> Sorry I don't understand you... why should it not keep the size? > >>> > >>>> Also on a PDFBox 2.0 note, the "PDPageContentStream -> drawXObject' is > >>>> deprecated and the source says to use drawImage instead. However I was > >>>> not able to find a drawImage method that takes AffineTransform. What is > >>>> the recommended way to do this in 2.0 going forward? > >>> saveGraphicsState(); > >>> transform(new Matrix(transform)); <== do the rotation > >>> only > >>> > >>> drawImage() <=== here just set the position > >>> > >>> restoreGraphicsState(); > >>> --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org For additional commands, e-mail: users-h...@pdfbox.apache.org