views:

332

answers:

2

My java application is generating an image(basically an org chart) for a jasper report. As org charts go, there's no way one can determine the final size of the image output. Also the image output can grow too large, that cropping the image is useless.

Is it even possible to have the jasper report to dynamically resize it's image element without splitting the images?

A: 

Try this:

<image scaleImage="ReatinShape" hAlign="Center"> 
    <reportElement x="100" y="150" width="600" height="150"/> 
    <graphicElement/> 
    <imageExpression class="net.sf.jasperreports.engine.JRRenderable">
     <![CDATA[new com.onbarcode.barcode.jasper.JasperRenderer(yourImage)]]></imageExpression> 
</image>
Boris Pavlović
a stupid question...what kind of class is the yourImage parameter? Is it the path? or a bufferedimageI'm used to developing Jasper reports in iReport, by the way.
xilver
It's the image object that your reports receives as an input.
Boris Pavlović
A: 

I've not tested this myself, but it it supposed to work. Get the image element from the design, change it's size, then compile the altered design.

BufferedImage img = ImageIO.read(new File(wo_image_path));
height = img.getHeight();
width = img.getWidth();         
jasperSubDesign = JasperManager.loadXmlDesign(context.getRealPath("/WEB-INF/reports/decoration_sheet_header.jrxml"));

JRDesignImage image = (JRDesignImage)jasperSubDesign.getPageHeader().getElementByKey("wo_image");

image.setX(3);
image.setY(69);
image.setHeight(new Long(height - Math.round(height*.35)).intValue());
image.setWidth(new Long(width - Math.round(width*.35)).intValue());

JasperCompileManager.compileReportToFile(jasperSubDesign, context.getRealPath("/WEB-INF/reports/decoration_sheet_header.jasper"));

From Jasperforge

Guillaume
Very nice! I understand that this one *could* be a subreport. If it is so, would there be a possibility for error if the image to actually outgrow the report itself?
xilver
My guess is that if the image goes is larger than the report, the compilation will fail. I don't don't know the Jasper design API but you can probably resize the report as well.
Guillaume