Robert Ensinger

How To: Single Textbox "Page {X} of {Y}" Page Counts in Jaspersoft Reports

Blog Post created by Robert Ensinger on Dec 28, 2015

When migrating our reports from Business Objects to Jaspersoft it's important for us to abide by our documented reporting standards, retaining the same look & feel that our customers are familiar with.

 

In our reporting standards the page count information is found in the page footers as shown below. "Page X of Y" in the report footer, bottom right, aligned right with room to grow left.

report_footer.jpg

Jaspersoft's PAGE_NUMBER variable & functionality is designed as a two textbox solution for "Page X of Y" results.

page_x_of_y.jpg

This doesn't lend itself easily to clean align left or align right visualizations. To do this we need to Display "Page 1 of N" In a Single Text Field. Here's how.

 

1) Create a new Integer Variable called CURRENT_PAGE_NUMBER that inherits $V{PAGE_NUMBER} via its expression, with no calculation or increment type as shown below. Be sure to set the Reset type to Page.

variable_composit.jpg

2) Add a Text Field to your page, set the Evaluation Time to Auto as shown below, text alignment as you wish and add the expression:

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

 

text_box.jpg

 

3) Save & Preview your report. You should now see the Page X of Y message in one text box, aligned as expected.

report_footer2.jpg

Source:

<variable name="CURRENT_PAGE_NUMBER" class="java.lang.Integer" resetType="Page">
  <variableExpression><![CDATA[$V{PAGE_NUMBER}]]></variableExpression>
</variable>

 

<textField evaluationTime="Auto">
 <reportElement x="460" y="10" width="100" height="20" uuid="60626a70-961a-40c3-ba86-6e72046e405a">
   <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
   <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
   <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
  </reportElement>
  <textElement textAlignment="Right" verticalAlignment="Middle"/>
  <textFieldExpression><![CDATA["Page "+ $V{CURRENT_PAGE_NUMBER} + " of " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>

Outcomes