Jasper Reports
January 29, 2021

References

Internationalization

There are two ways to associate the java.util.ResourceBundle object with the report template.

  • At design time, by setting the resourceBundle attribute of the report template object to the base name of the target resource bundle.
  • A dynamic/runtime association can be made by supplying a java.util.ResourceBundle object as the value for the REPORT_RESOURCE_BUNDLE parameter at report-filling time.

Example (at design time):

Resources.properties or Resources_en_GB.properties

1
localization.home=Home
1
2
3
4
<jasperReport
    ...
    resourceBundle="Resources"
    ...
1
2
3
4
5
<textField>
    <reportElement x="300" y="20" width="50" height="15" />
    <textElement textAlignment="Center" />
    <textFieldExpression class="java.lang.String"><![CDATA[$R{localization.home}]]></textFieldExpression>
</textField>

In order to internationalization of parameter $P or dataset field $F, you can use the method str().

1
2
3
4
5
<textField>
    <reportElement x="300" y="20" width="50" height="15" />
    <textElement textAlignment="Center" />
    <textFieldExpression><![CDATA[str($F{fieldName})]]></textFieldExpression>
</textField>

If the report needs to be generated in a locale that is different from the current one, the built-in REPORT_LOCALE parameter can be used to specify the runtime locale when filling the report.

1
2
Map<String, Object> parameters = new HashMap();
parameters.put(JRParameter.REPORT_LOCALE, new Locale("en", "GB"));

Pagination

The common approach, as you mentioned, uses two separated text fields:

Current page number: $V{PAGE_NUMBER} with EvaluationTime: Now

Total page number: $V{PAGE_NUMBER} with EvaluationTime: Report

1
2
3
4
5
6
7
8
<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>

For JasperReports 6+, use MASTER_CURRENT_PAGE and MASTER_TOTAL_PAGES system variables and remember to set the text field evaluationTime to Master:

1
2
3
4
<textField evaluationTime="Master">
    <textElement textAlignment="Right"/>
    <textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression>
</textField>

New page, splitType

Create a new band with splitType="Immediate" property to add a new page.

1
<band height="600" splitType="Immediate">

Floating, positionType

If you need to have a staticText to float below another element with variable height, you can use positionType="Float".

Floating object will move down if necessary, but if the previous object is smaller than expected, it doesn’t move up.

According to the documentation, the floating element would have its y attribute ignored.

1
2
3
4
5
<staticText>
    <reportElement positionType="Float" x="0" y="190" width="495" height="15" />
    <textElement verticalAlignment="Middle" />
    <text><![CDATA[Text is here]]></text>
</staticText>