Datasoure
I've used the csv datasource (holidays.csv):
name,date
New Year's Day,January 1st
Christmas Day,December 25th
Valentine's Day,February 14th
The report's design
I've placed two textFields for showing holiday's name and the holiday's date. And I've placed the image element for showing image under the textField with holidays name.
With help of printWhenExpression I'm showing the holiday's name or image.
The report's design in iReport:
The report's template
The jrxml file:
<?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="holidays" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e0310045-780b-4af3-aa7b-ed7f5da1985f">
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="date" class="java.lang.String"/>
<detail>
<band height="66" splitType="Stretch">
<image scaleImage="RealHeight">
<reportElement uuid="e7738ed8-673e-49e9-8c14-060378cb6a79" x="0" y="0" width="263" height="66">
<printWhenExpression><![CDATA[$F{name}.equals("Christmas Day")]]></printWhenExpression>
</reportElement>
<imageExpression><![CDATA["mc.jpg"]]></imageExpression>
</image>
<textField>
<reportElement uuid="167148f8-ad51-4c87-9faa-27bf18f2160e" x="0" y="0" width="263" height="66" forecolor="#FF6666">
<printWhenExpression><![CDATA[!$F{name}.equals("Christmas Day")]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Forte" size="24" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="ad7c75b4-975b-478e-8a44-090a63c52896" x="263" y="0" width="263" height="66"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial" size="24" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The generated output
And the generated report will be (via preview in iReport):
what can i do in the report to get a result like this(the datasource is also changed to this is an christmas day):
The first solution: the image behind the textField element
Datasoure
I've used the csv datasource (log.csv):
The report's design
I've placed two textFields for showing message level and the message. And I've placed the image element for showing image under the textField with message level's icon.
With help of printWhenExpression I'm showing the message level's icon or text. I'm showing the icon if it is defined in datasource (field image) or the text (the field level) if not defined.
The imageExpression is:
We can set the full path to the image (the path plus file name from the datasource).
The report's design in iReport:
The report's template
The jrxml file:
The generated output
And the generated report will be (via preview in iReport):
The second solution: using Html element
Datasoure
I've used the csv datasource (text2replace.csv):
The report's design
I've used the Html element.
With help of hc:htmlContentExpression attribute I've set the html code: I replaced the occurrences of string "IMAGE" with html tag
<img>
for drawing images.The hc:htmlContentExpression is:
The report's design in iReport:
The report's template
The jrxml file:
The generated output
And the generated report will be (via preview in iReport):
Notes
I've used the method com.google.common.base.Strings.isNullOrEmpty(String) from Google's Guava library for checking if the image set or not. It is import string
<import value="com.google.common.base.Strings"/>
in my jrxml file.The another sample is here