int imageHeight = awtImage.getHeight();
double pageHeight = pageSize.getHeight();
log.info("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);
Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
int pageRotation = page.findRotation();
AffineTransform ctmAT = ctm.createAffineTransform();
ctmAT.scale(1f/imageWidth, 1f/imageHeight);
Matrix rotationMatrix = new Matrix();
rotationMatrix.setFromAffineTransform( ctmAT );
if (pageRotation == 0 || pageRotation == 180)
{
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition()-ctm.getYScale());
}
else if (pageRotation == 90 || pageRotation == 270)
{
rotationMatrix.setValue(2,0,(float)ctm.getXPosition()-ctm.getYScale());
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition());
}
rotationMatrix.setValue(0, 1, (-1)*rotationMatrix.getValue(0, 1));
rotationMatrix.setValue(1, 0, (-1)*rotationMatrix.getValue(1, 0));
AffineTransform at = new AffineTransform(
rotationMatrix.getValue(0,0),rotationMatrix.getValue(0,1),
rotationMatrix.getValue(1,0), rotationMatrix.getValue( 1, 1),
rotationMatrix.getValue(2,0),rotationMatrix.getValue(2,1)
);
Shape clip = context.getGraphicsState().getCurrentClippingPath();
// If the pdf is printed, sometimes a NPE occurs, see PDFBOX-552 for details
// As a workaround we have to replace a possible null-value with a rectangle having
// the same dimension than the page to be printed