Jasper Reports Show "Page X of Y" using a single text field
Jaspersoft Studio, 6+
For Jaspersoft Studio v6, or if the first page number is duplicated, try this solution, which uses $V{MASTER_CURRENT_PAGE}
and $V{MASTER_TOTAL_PAGE}
with an evaluation time of Master
.
Jaspersoft Studio
For other versions of Jaspersoft Studio, try the steps outlined in the subsequent subsections.
Create Variable
Create a variable as follows:
- Create a variable called
V_CURRENT_PAGE_NUMBER
- Select the variable to open its properties (illustrated below)
- Set Expression to:
1
- Set Initial Value Expression to:
$V{PAGE_NUMBER}
- If the page number shows
0
, use$V{PAGE_NUMBER} + 1
. - If the page number always shows
1 of Y
, set Expression to$V{PAGE_NUMBER}
instead of the initial value expression, and leave the initial value expression empty.
- Set Reset type to:
Page
These settings are illustrated in the following figure:
Setting the Expression to 1
prevents its value from being null
. That is, if the footer shows Page null of 4 it probably means that the Expression hasn't been set.
The variable is created.
Add Page Footer
Add a Page Footer band as follows:
- Select the report in the outline panel
- Check Summary With Page Header And Footer to ensure the page footer appears on the summary page.
- Add a Page Footer band.
The footer is added.
Create Text Field
Create a text field as follows:
- Drag and drop a single text field onto the Page Footer band.
- Select the text field.
- Set Expression to:
msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
- Set Evalutation Time to:
Auto
These settings are illustrated in the following figure:
The single text field is created.
Preview Report
For a report with three pages plus a summary page, previewing the report shows:
The summary page shows:
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
I tried this approach, but ended up with incorrect page numbers: {1/7, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7}.
For JasperReports 6+, use MASTER_CURRENT_PAGE
and MASTER_TOTAL_PAGES
system variables and remember to set the text field evaluation time to Master
:
<textField evaluationTime="Master">
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression>
</textField>
See: http://jasperreports.sourceforge.net/sample.reference/book/index.html