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:[email protected]]
Sent: Monday, March 07, 2016 8:05 AM
To: [email protected]
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:[email protected]]
Sent: Friday, March 04, 2016 3:51 PM
To: [email protected]
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:[email protected]]
> Sent: Friday, March 04, 2016 3:08 PM
> To: [email protected]
> 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:[email protected]]
>> Sent: Friday, March 04, 2016 2:44 PM
>> To: [email protected]
>> 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: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]