Search code examples
jasper-reports

How to hide/show and image or textField on last page?


In Jasper studio how can I disable/show background for a single page in the report.

I want to disable the watermark image which I set in the background for the last page.


Solution

  • The simplest method that I know when you need to compare current page towards totale number of pages is to use evaluation type "auto" on the element.

    From EvaluationTimeEnum

    Variables will be evaluated at a time corresponding to their reset type.

    Solution adapted from lucianc's answer at https://community.jaspersoft.com/questions/514622/print-when-last-page

    Create a variabile that contains current page having reset type Page (so it's evaluate when Auto as current page number)

     <variable name="currentPageInAutoEval" class="java.lang.Integer" resetType="Page">
        <variableExpression>$V{PAGE_NUMBER}</variableExpression>
     </variable
    

    set evaluation time to "Auto" on your element (textField, image etc) and in expression use ternary operator

    In your case, no image on last page it would be

    <image evaluationTime="Auto">
        <imageExpression>$V{currentPageInAutoEval}.equals($V{PAGE_NUMBER}) ? null : yourImage</imageExpression>
    </image>
    

    Hence with evalutationTime="Auto" $V{currentPageInAutoEval} will be evaluated as resetType (Page) to current page number and $V{PAGE_NUMBER} as the total number of pages.