I have a requirment where I need to display current page number and the total page number accross reports. I have a master report and 2 subreports. I am not able to identify how
You can use $V{PAGE_NUMBER} variable, but in case of a sub-report it provides you either with just the current sub-report page number or with the total sub-report number of pages. The problem is that Jasper Reports doesn't straightly allow you to pass the current page number from the master report to the sub-report.
Simple solution would be if you just incorporate the functionality to print the current page number in the master report. This works but not in a flexible way since you loose the ability to manage the detail of printing the page number in each sub-report.
Eventually I've come up with the better solution utilizing scriptlets. The main idea is to pass to the sub-reports the scriptlet instance, containing the necessary information.
public class ExampleScriptlet extends JRDefaultScriptlet {
private Integer page_number;
public void afterPageInit() throws JRScriptletException {
this.page_number = (Integer) this.variablesMap.get("PAGE_NUMBER").getValue();
}
public String getPageNum() throws JRScriptletException {
return String.valueOf(page_number);
}
public ExampleScriptlet getScriptlet() throws JRScriptletException {
return this;
}}
In the master report put scriptletClass = "my.example.ExampleScriptlet"
in jasperReport tag.
In the sub-report section of the master report pass the instance of the scriptlet to the sub-report
$P{REPORT_SCRIPTLET}.getScriptlet()
Declare the scriptlet parameter in the sub-report
In the sub-report retrieve the page number from the scriptlet