Managing page breaks in reports is crucial for readability and professionalism. In JasperReports, you often need a page break when a certain group of data ends—for instance, when the value of a key field changes. This blog post will show you how to implement a conditional page break in the Detail band of your JasperReport, triggered when the current product value is different from the previous (last) one. This technique uses a clever combination of variables and the printWhenExpression
.

The Problem: Grouping and Page Breaks in the Detail Band
Normally, you’d use a Group to define a break based on a field change, and the page break setting on the Group Footer would handle the separation. However, sometimes you want the logic directly in the Detail band, perhaps for finer control or when traditional grouping doesn’t fit your report layout.
The challenge is: how do you access the previous record’s value while processing the current record in the Detail band? The solution lies in using a Variable to store the value of the field from the last processed record.
Step 1: Define the Variable to Store the Previous Value
First, you need a variable that will hold the value of your key field (e.g., `$F{PRODUCT_VALUE}$) after the current record has been processed.
- In the Report Inspector, right-click on Variables and select Create Variable.
- Set the properties as follows:
- Name:
PreviousProductValue
(or similar) - Variable Class: Match the data type of your field (e.g.,
java.lang.String
orjava.lang.Integer
). - Calculation: No Caluclation Function or System (It’s not an aggregate calculation).
- Reset Type:
Column
(It should only update per record, not reset on group/report). - Variable Expression:
$F{PRODUCT_VALUE}
(This is the crucial step: it tells the variable what value to take). - Initial Value Expression:
$F{PRODUCT_VALUE}
(Optional, often left blank or set to a default/null value).
- Name:

💡 How it works: Because of JasperReports’ processing flow, when you use
$F{PRODUCT_VALUE}
in the Variable Expression, the variable’s value will update to the current field value after the detail band has been evaluated for the current record. Thus, in the next record’s Detail band evaluation, the variablePreviousProductValue
holds the value from the last record.
Step 2: Implement the Conditional Page Break
Now, you will place a Page Break element in the Detail band and define its printWhenExpression
using your new variable.
- Drag a Page Break element from the Palette onto the Detail band. It should be placed at the very top of the band, before any other elements. (Important: Image-3)
- Select the Page Break element and go to its properties.
- Find the Print When Expression property and set it to the following logical condition (assuming your field is
$V{REPORT_COUNT} > 1 && !$F{PRODUCT_VALUE
}.equals($V{PreviousProductValue})- Explanation:
$F{PRODUCT_VALUE}
: The value of the current product.$V{PreviousProductValue}
: The value of the product from the last processed record.- The expression evaluates to
true
(and thus prints the page break) only if the current product value is not equal to the previous product value.
- Explanation:


Conclusion: Smart, Data-Driven Paging
By using a simple variable to ‘remember’ the previous record’s state, you can effectively create a custom, data-driven page break directly within the Detail band, giving you precise control over your report’s output structure based on data changes. This method is a powerful tool in your JasperReports arsenal for creating clear, segmented, and professional documents.