JasperReports
JasperReports tips: localise reports with resource bundles, print page X of Y, control band splitting and page breaks, and float elements.
3 min readUpdated
JasperReports is a Java library that fills report templates (.jrxml files) with data and exports them to PDF, HTML, Excel and other formats. This note collects template techniques that come up often: translating reports with resource bundles, page numbering, controlling where bands split across pages, and letting elements move down below content of variable height.
Localise a report
Translated texts live in a java.util.ResourceBundle. There are two ways to attach a bundle to a report:
- At design time, set the
resourceBundleattribute of the report template to the base name of the bundle. - At fill time, pass a
ResourceBundleobject as the value of the built-inREPORT_RESOURCE_BUNDLEparameter.
Attach a bundle at design time
Put the texts in a properties file on the classpath, for example Resources.properties for the default locale or Resources_en_GB.properties for British English:
localization.home=HomeName the bundle in the <jasperReport> element:
<jasperReport ... resourceBundle="Resources" ...>Reference a key with the $R{} syntax in any expression:
<textField> <reportElement x="300" y="20" width="50" height="15"/> <textElement textAlignment="Center"/> <textFieldExpression class="java.lang.String"><![CDATA[$R{localization.home}]]></textFieldExpression></textField>Translate values from parameters and fields
$R{} takes a fixed key. When the key comes from a parameter ($P{}) or a field ($F{}), use the built-in str() method, which looks up its argument in the report’s resource bundle:
<textField> <reportElement x="300" y="20" width="50" height="15"/> <textElement textAlignment="Center"/> <textFieldExpression><![CDATA[str($F{fieldName})]]></textFieldExpression></textField>For texts with placeholders, the built-in msg() method formats a message like java.text.MessageFormat, using the report locale.
Fill a report in another locale
To generate a report in a locale other than the JVM’s current one, pass it in the built-in REPORT_LOCALE parameter when filling the report:
Map<String, Object> parameters = new HashMap<>();parameters.put(JRParameter.REPORT_LOCALE, new Locale("en", "GB"));Print “Page X of Y”
The classic approach uses two text fields with the PAGE_NUMBER variable. The first is evaluated immediately and shows the current page; the second has evaluationTime="Report", so it is filled in once the whole report is done and shows the total number of pages:
<textField> <reportElement x="130" y="130" width="120" height="20"/> <textFieldExpression><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression></textField><textField evaluationTime="Report"> <reportElement x="250" y="130" width="115" height="20"/> <textFieldExpression><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression></textField>Since JasperReports 6, a single text field can do it with the MASTER_CURRENT_PAGE and MASTER_TOTAL_PAGES system variables. Set the field’s evaluationTime to Master:
<textField evaluationTime="Master"> <reportElement x="0" y="0" width="200" height="20"/> <textElement textAlignment="Right"/> <textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression></textField>With msg() the pattern can also come from the resource bundle, so the text is translated too.
Control page breaks
Choose how a band splits
The splitType attribute of a band decides what happens when the band does not fit on the rest of the page:
Stretch(default): the band can split, but not within its declared height.Prevent: the band is moved to the next page instead of being split, if it fits there.Immediate: the band can split anywhere, as early as needed.
<band height="600" splitType="Immediate">Force a new page
To always start a new page at a given point, put a break element in the band:
<break type="Page"> <reportElement x="0" y="0" width="100" height="1"/></break>Float elements below variable-height content
When an element sits below another element that can stretch (such as a text field that grows with its content), set positionType="Float" on the lower element so it is pushed down as the upper one grows:
<staticText> <reportElement positionType="Float" x="0" y="190" width="495" height="15"/> <textElement verticalAlignment="Middle"/> <text><![CDATA[Text is here]]></text></staticText>A floating element keeps its distance to the element above it. It moves down when that element stretches, but it never moves up when the element above is shorter than designed.