Dynamic Image size in Jasper Report

2019-09-03 08:06发布

问题:

I am trying to print a Jasper Report with a image. Image is displayed dynamically using a parameter. Image is printing fine. But I need to output the image size dynamically too. How can I pass the the image size (in inch) as a parameter and get my expected output?

<image>
<reportElement uuid="a4ed8a3a-827e-494c-ba8d-7276007941c7" x="0" y="0" width="555" height="608"/>
<imageExpression><![CDATA[$P{IMAGE_LOCATION}]]></imageExpression>
</image>

回答1:

public JasperDesign dynamicImage() throws JRException {

    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("NoXmlDesignReport");
    jasperDesign.setPageWidth(595);
    jasperDesign.setPageHeight(842);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(20);
    jasperDesign.setBottomMargin(20);

    JRDesignExpression expression = new JRDesignExpression();

    //Summary

        band = new JRDesignBand();
        band.setHeight(125);
        JRDesignImage jrDesignImage = new JRDesignImage(jasperDesign);
        expression = new JRDesignExpression();
        expression.setText("\"" "/path/to/image\"");
        jrDesignImage.setExpression(expression);
        jrDesignImage.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
        jrDesignImage.setX(400);
        jrDesignImage.setY(0);
        jrDesignImage.setWidth(93);
        jrDesignImage.setHeight(100);
        jrDesignImage.setScaleImage(ScaleImageEnum.RETAIN_SHAPE);
        band.addElement(jrDesignImage);
        jasperDesign.setSummary(band);

    return jasperDesign;
}

You can set width and height dynamically.