How to get current page number and total pages as variables on each page?

jasper-reports

I need to generate OMR code on a report. I think about calling some service method on each page that get page number and total pages as parameters. Problem is: How to get that values (page number, total pages) as variables on each page ?

<background>
    <band height="797">
        <textField>
            <reportElement x="30" y="684" width="100" height="97"/>
            <textFieldExpression><![CDATA[OMRService.generateCode($V{totalPages},$V{pageNumber})]]></textFieldExpression>
        </textField>
    </band>
</background>

Best Answer

below you have sample code how to do it in 2 different textFields.

<textField evaluationTime="Page">
        <reportElement x="0" y="0" width="520" height="15"/>
        <textElement textAlignment="Right"/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}+"/"]]>  </textFieldExpression>
</textField>
<textField evaluationTime="Report">
        <reportElement x="521" y="0" width="14" height="15"/>
        <textElement textAlignment="Left"/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>

When evaluationTime="Page" you will get page number, when "Report" there will be displayed total number of pages. I dont know how to merge it together in one textfield (dont even know if its possible - I spent a lot of time on it and couldnt solve this problem).

Related Topic