Search code examples
jasper-reports

How to use a parameter to display a default image?


I have a report that I generate on my webserver. The report contains and image which I pass as a parameter which all works fine on report generation.

But I want also to be able to use this image in my report design in Jaspersoft Studio and wonder how I can do this. Can it be loaded as the defaultValueExpression for example something like below?

<parameter name="MyLogo" class="java.lang.Object">
    <parameterDescription><![CDATA[]]></parameterDescription>
    <defaultValueExpression>path/to/file here?</defaultValueExpression>
</parameter>

Solution

  • To display an image from location you will need to indicated absolute path in imageExpression

    For this reason I often use 2 parameter, 1 for the base path (location of images) and 1 for the image to display (name of image) on both you can indicated defaultValueExpression and if you need to change them pass your value through the parameter map. Naturally you can use only one parameter containing the complete absolute path to image.

    Example

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="image" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="61ee899d-277b-4efa-9306-c325b54ac022">
        <parameter name="IMAGE_PATH" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["C:\\Users\\pette\\Desktop\\tmp\\"]]></defaultValueExpression>
        </parameter>
        <parameter name="THE_IMAGE" class="java.lang.String">
            <defaultValueExpression><![CDATA["theboss.jpeg"]]></defaultValueExpression>
        </parameter>
        <title>
            <band height="118" splitType="Stretch">
                <image scaleImage="RealSize">
                    <reportElement x="0" y="0" width="139" height="95" uuid="f5f1c323-b120-48ed-98a5-478a5b907429"/>
                    <imageExpression><![CDATA[$P{IMAGE_PATH} + $P{THE_IMAGE}]]></imageExpression>
                </image>
            </band>
        </title>
    </jasperReport>
    

    Output

    Result