How to add page number across master and subreports

前端 未结 3 1385
没有蜡笔的小新
没有蜡笔的小新 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:12

    First of all you need to add a textfield to page footer in your main raport page, and the expression should be : $V{PAGE_NUMBER}+" /" Then add other text field right next to the first one and the expression should be : " " + $V{PAGE_NUMBER} This will look like this : 1 / 2 But the important one is that;

    1. set first text fields evaluation time: "now" from properties window.
    2. set second text fields evaluation time: "report" from properties window.

    Then, for viewing correctly these page numbers on subreport pages; Click and select your main report name at top of Report Inspector window on the left side. Make sure you checked and selected this option from properties window :

    Summary with Page Header and Footer

    This will make your subreport show page numbers correctly. Make sure your subreport has summary band to get results.

    There is no need to pass parameters to subreport or even no need to add these textfields to subreport pages to show page numbers.

    Subreports will recognize page numbers automatically.

    0 讨论(0)
  • 2021-01-24 22:16

    Open Ireport and Go to Window-> Palette option here you can see a Page number and total page element in tool category. Now just drag and drop into that location where you want to print page numbers.

    0 讨论(0)
  • 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

    <subreportParameter name="pg_num" ><subreportParameterExpression>
                    $P{REPORT_SCRIPTLET}.getScriptlet()
                </subreportParameterExpression> </subreportParameter>
    

    Declare the scriptlet parameter in the sub-report

    <parameter name="pg_num" class="my.example.ExampleScriptlet"/>
    

    In the sub-report retrieve the page number from the scriptlet

     <textFieldExpression><![CDATA["($P{pg_num}.getPageNum())"]]></textFieldExpression>
    
    0 讨论(0)
提交回复
热议问题