How to add page number across master and subreports

前端 未结 3 1397
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 21:41

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

3条回答
  •  爱一瞬间的悲伤
    2021-01-24 22:29

    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

     
    

提交回复
热议问题