Wednesday, 18 March 2015

Jasper Reports : detecting last page

There always a time that we would want to display something different on the last page of the report , but there is no existing parameter could be used as expression, then how ?



Solution 1 : 

If we need to show something only on the last page then use "LastPageFoot" section.



Solution 2 :

If we have to do something when it is not last page and something else when it is last page , then follow below

Step 1 : have a text field on "Summary" section, and put $P{REPORT_PARAMETER.MAP}.put("isLastPage",Boolean.TRUE).

Please note that above statement would return null , as "isLastPage" does not exists in the map (REPORT_PARAMETER.MAP)

Step 2 : use $P{REPORT_PARAMETER.MAP}.get("isLastPage") as expression , if the value is null then it is not last page.

Please find sample code below and it is tested with jasperreports 6.0.3 , java 7: 


Note :

There is one important feature to understand in order for solution 2 to work which is "evaluation time" .

"Evaluation Time" determines in which phase of the report creation the Text field Expression has to be elaborated.
The possible values are:

• Report: The value of the expression is evaluated ad the end of the report.

• Page: The value of the expression is evaluated at the end of every page of the report.

• Column: The value of the expression is evaluated at the end of each column (if the report is composed
only of a column this is equivalent to Page).

• Group: The value of the expression is evaluated after the break of the specified group. This option is
visible only if at least one group is defined.

• Band: The value of the expression is evaluated after the end of the band where the element with this
evaluation time is placed. This is a very particular case, introduced to wait that the other elements in the
band are completely created. Typically the value of the variables are read at the start of the band, but for
example suppose to have a subreport with an output parameter to print in the main report. To print this
parameter it must be read when the subreport was already computed, so the value could be printed when the
band is completely created, in this cases the Band evaluation time is necessary.

• Auto: This is used when in the expression of the element there are more variables and fields, that need to
be evaluated at different times. The variables are evaluated at a time corresponding to their Reset Type (see
below for more information), instead the fields are always evaluated at time -now. This type is useful when
report elements have expressions that combine values evaluated at different times (for example, percentage
out of a total).

• Now: The value of the expression is evaluated after the read of every record, so at every iteration, this is the default  behaviour.
 

7 comments:

  1. hi, I tried your 2nd solution..But in my case I have created group header group footer...I want to access that parameter in goup footer..how can I do this..plz help

    ReplyDelete
  2. It is working in group band but not in cloumn footer in table...how Can I do this

    ReplyDelete
    Replies
    1. if it is only footer , can use solution 1 right ?

      Delete
  3. Thanks for the guide! I don't seem to get it to work, since I can't figure out what evaluation times to give those two text fields, since you only explained the concept of evaluation times, but didn't specify what you used.

    ReplyDelete
  4. Thanks, for me works !!!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. But I changed for:
    $P{REPORT_PARAMETERS_MAP}.put("isLastPage",Boolean.TRUE)

    ReplyDelete

Flag Counter