* @throws IOException If there is an error invoking the sub object.
*/
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException
{
PageDrawer drawer = (PageDrawer)context;
PDPage page = drawer.getPage();
Dimension pageSize = drawer.getPageSize();
Graphics2D graphics = drawer.getGraphics();
COSName objectName = (COSName)arguments.get( 0 );
Map xobjects = drawer.getResources().getXObjects();
PDXObject xobject = (PDXObject)xobjects.get( objectName.getName() );
if( xobject instanceof PDXObjectImage )
{
PDXObjectImage image = (PDXObjectImage)xobject;
try
{
image.setGraphicsState(drawer.getGraphicsState());
BufferedImage awtImage = image.getRGBImage();
if (awtImage == null)
{
log.warn("getRGBImage returned NULL");
return;//TODO PKOCH
}
int imageWidth = awtImage.getWidth();
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
if (clip == null)
{
clip = new Rectangle(pageSize);
}
graphics.setClip(clip);
graphics.drawImage( awtImage, at, null );
}
catch( Exception e )
{
e.printStackTrace();
log.error(e, e);
}
}
else if(xobject instanceof PDXObjectForm)
{
PDXObjectForm form = (PDXObjectForm)xobject;
COSStream invoke = (COSStream)form.getCOSObject();
PDResources pdResources = form.getResources();
if(pdResources == null)
{
pdResources = page.findResources();
}
getContext().processSubStream( page, pdResources, invoke );
}
else