Jasper Reports: page x of y within one record

2019-05-18 12:36发布

问题:

I have report with multiple records, where one record is consisting of 1-5 pages. How to display "page x of y", where x is a number of page for actual record and y is total pages for actual record ? I have something like below for x variable (reset on new record, incremet by page), but it doesn't work (on each page x have 1 value):

<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Count">
    <variableExpression><![CDATA[1]]></variableExpression>
    <initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>

<!-- group by record -->
<group name="report_count" isStartNewPage="true">
    <groupExpression><![CDATA[$V{REPORT_COUNT}]]></groupExpression>
</group>

<textField evaluationTime="Now" evaluationGroup="report_count">
    <reportElement x="141" y="5" width="156" height="20"/>
    <textElement textAlignment="Right"/>
    <textFieldExpression><![CDATA["Page "+$V{x}+" of"]]></textFieldExpression>
</textField>

回答1:

The problem is that calculation="count" does not do what you are expecting it to. It returns the count of non-null values that your variableExpression returns. As your variableExpression only returns a single value, the variable is always set to 1.

An easy solution is to set the calculation type to "Nothing", and the variableExpression to $V{x}+1

i.e:

<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Nothing">
    <variableExpression><![CDATA[$V{x} + 1]]></variableExpression>
    <initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>


Edit: Alternative solution

The group tag can have the attribute isResetPageNumber. When set to true it will reset the built-in variable PAGE_NUMBER at the start of every group. As you are already grouping by each record, I think this should give you the effect you are looking for.