23
votes

I would like to create one text field that contains Page X of Y, without splitting it in two parts, as per the common solution. My textfield contains "Page " + $V{currentPage} + " of " + $V{PAGE_NUMBER}" with evaluationTime=auto.

Let's say I have a report with 10 pages. Three are the Title Band, six are Detail Band and one is the Summary Band. My results show "Page 0 of 10" for the Title Bands, correct counts for the Detail Bands, then "Page 0 of 10" for the Summary Bands.

How do you ensure the variable is calculated everywhere, not only on Detail Band?

8
$V{PAGE_NUMBER} contains the current page number, what you want is $V{NumberOfPages} so you can use it in one text box with EvaluationTime: Now. Otherwise you need the two text box solution like hereKCD
@KCD does your JasperReport has a ${NumberOfPages} variable? I don't seem to be able to see it here.Paŭlo Ebermann

8 Answers

33
votes

Jaspersoft Studio, 6+

For Jaspersoft Studio v6, or if the first page number is duplicated, try this solution, which uses $V{MASTER_CURRENT_PAGE} and $V{MASTER_TOTAL_PAGE} with an evaluation time of Master.

Jaspersoft Studio

For other versions of Jaspersoft Studio, try the steps outlined in the subsequent subsections.

Create Variable

Create a variable as follows:

  1. Create a variable called V_CURRENT_PAGE_NUMBER
  2. Select the variable to open its properties (illustrated below)
  3. Set Expression to: 1
  4. Set Initial Value Expression to: $V{PAGE_NUMBER}
  • If the page number shows 0, use $V{PAGE_NUMBER} + 1.
  • If the page number always shows 1 of Y, set Expression to $V{PAGE_NUMBER} instead of the initial value expression, and leave the initial value expression empty.
  1. Set Reset type to: Page

These settings are illustrated in the following figure:

Current Page Number Variable

Setting the Expression to 1 prevents its value from being null. That is, if the footer shows Page null of 4 it probably means that the Expression hasn't been set.

The variable is created.

Add Page Footer

Add a Page Footer band as follows:

  1. Select the report in the outline panel
  2. Check Summary With Page Header And Footer to ensure the page footer appears on the summary page.
  3. Add a Page Footer band.

The footer is added.

Create Text Field

Create a text field as follows:

  1. Drag and drop a single text field onto the Page Footer band.
  2. Select the text field.
  3. Set Expression to: msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
  4. Set Evalutation Time to: Auto

These settings are illustrated in the following figure:

Single Text Field

The single text field is created.

Preview Report

For a report with three pages plus a summary page, previewing the report shows:

Page 1

The summary page shows:

Page 4

17
votes

I tried this approach, but ended up with incorrect page numbers: {1/7, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7}.

For JasperReports 6+, use MASTER_CURRENT_PAGE and MASTER_TOTAL_PAGES system variables and remember to set the text field evaluation time to Master:

<textField evaluationTime="Master">
    <textElement textAlignment="Right"/>
    <textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression>
</textField>

See: http://jasperreports.sourceforge.net/sample.reference/book/index.html

10
votes

The common approach, as you mentioned, uses two separated text fields:

Current page number

$V{PAGE_NUMBER} with EvaluationTime: Now

Total page number

$V{PAGE_NUMBER} with EvaluationTime: Report

3
votes

Regarding the current page number, evaluationTime=now and $V{PAGE_NUMBER} is your answer.

Unfortunately I don't think you can achieve what you want because there is a bug in PAGE_COUNT when Split Allowed is on for the Detail band. Otherwise evaluationTime=now and "Page " + $V{PAGE_NUMBER} + " of " + $V{PAGE_COUNT}" would probably work.

3
votes

For page history show like: Page: 1 of 5

Make the text field value to be like below and change the evaluation time to Master:

"Page: "+$V{MASTER_CURRENT_PAGE}+" of "+$V{MASTER_TOTAL_PAGES}

Source:

            <textField evaluationTime="Master">
                <reportElement x="224" y="0" width="186" height="15" uuid="6641bb8b-9f48-4832-942b-8b04220030e6">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                </reportElement>
                <textElement verticalAlignment="Top">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA["Page: "+$V{MASTER_CURRENT_PAGE}+" of "+$V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
            </textField>
2
votes

This work for my (little different of Dave's Answer)

*Using JasperSoft Studio

Capture: adding Variable

Then put a Text field with the expression:

"Pág. " + $V{PAGE_NUMBER} +"/" + $V{V_CURRENT_PAGE_NUMBER}

Hope this help!

0
votes

This should help, by using evaluationTime as Report

   <textField>
        <reportElement x="497" y="0" width="32" height="12" forecolor="#7E8083"
                           uuid="ef663cfd-4058-40bb-a6d9-de7f9a8164be"/> --update your elements here
        <textElement textAlignment="Right" verticalAlignment="Middle">
            <font fontName="SansSerif" size="7" pdfFontName="OpenSans-Regular.ttf"/>
        </textElement>
        <textFieldExpression>
            <![CDATA["Page " + $V{PAGE_NUMBER} + " of"]]>
        </textFieldExpression>
   </textField>
   <textField evaluationTime="Report">
        <reportElement x="529" y="0" width="7" height="12" forecolor="#7E8083"
                           uuid="ef663cfd-4058-40bb-a6d9-de7f9a8164be"/>  --update your elements here
        <textElement textAlignment="Right" verticalAlignment="Middle">
            <font fontName="SansSerif" size="7" pdfFontName="OpenSans-Regular.ttf"/>
            </textElement>
            <textFieldExpression>
                <![CDATA[$V{PAGE_NUMBER}]]>
            </textFieldExpression>
   </textField>
0
votes

If you want to see page no like Page 1 of 5 on right side of the report footer use ireport or jasper report Palette Tools(Page X of Y) is perfect.

If you want to see page no like Page 1 of 5 on left side of the report footer use ireport.

Please follow below steps

  1. Create function variable as like

enter image description here

  1. Please take a TextField

    "Page "+$V{V_CURRENT_PAGE_NUMBER}+" of "+ $V{PAGE_NUMBER}

enter image description here