Quite often we might need to put at the beginning of our reports, values that are calculated only after the entire document is generated. The most common situation of this kind would be the display of the total number of pages on the page footer or header of each page. How to do that, since the report is filled page by page and the total number of pages is known only when we reach the end of our document?
Well, that's easy!
JasperReports allows us to specify the exact moment at which an expression for a text field is evaluated. To display the total number of pages, we only need to put on our page footer (or any other report section) a text field that will use the PAGE_NUMBER report variable in its expression. For this field we will specify that the evaluation time should be "Report", because only when the end of the report is reached, this system variable will contain the total number of pages on our report.
The text field declaration in the XML report design file for the total number of pages in a document will look like this:
If we remove the attribute evaluationTime from the <textField> element, the text field expression will be evaluated when the section is filled (default behavior) so we will obtain the current page number on the document.
To display the current page number and the total number of pages simultaneously, like in "Page 3 of 5" phrases, we need two text fields with the same expression (PAGE_NUMBER variable value), but with different evaluationTime attributes:
evaluationTime="Now" for the current page number (default) evaluationTime="Report" for the total number of pages
There are 5 possible values for the attribute evaluationTime: Now, Report, Page, Columnand Group, master
For more information about this attribute, see the <textField> element in the Schema Reference.
An example is also supplied in the sample application.