Search code examples
jasper-reportssubreport

Show Page number range of subreports in Master Report


I have a master report with 4 subreports in its details section. I want to display the page number range (for example 1- 2) of all the subreports in the Master report title section. I tried using subreport return value but it works only when I have 1 subreport not if there are more than 1 subreport

enter image description here


Solution

  • Here is the solution

        Step1 :  Create variables of any type inside each of the SubReport and the set the calculation, Increment Type and Reset Type to None. No need to specify the Expression as well Initial Value Expression
    
    Step 2 :  Create variables of String type inside the Master Report to hold the value of page number (eg. 1-2)  and set the Calculation as System. 
    
    Step 3 : Add the variables created in Step2 on the Master Report Title section and set the evaluation time as Report.
    Step 4 : Create scriptlet and set the page number in that
    public class PageScriptlet extends JRDefaultScriptlet {
    
     private static int mgmtReportPages;
     private static int balanceSheetReportPages;
     private static int incomeStmtReportPages;
     private static int addInfoReportPages;
     private static final String MGMT_REPORT_PAGENUM = "mgmtReportPageNum";
     private static final String INCOME_STMT_PAGENUM = "incomeStmtPageNum";
     private static final String BALANCE_SHEET_PAGENUM = "balanceSheetPageNum";
     private static final String ADDINFO_PAGENUM = "addInfoPageNum";
     private static final String MGMT_REPORT_INDEX = "mgmtReportIndex";
     private static final String INCOMESTMT_INDEX = "incomeStmtIndex";
     private static final String BALANCESHEET_INDEX = "balanceSheetIndex";
     private static final String NOTES_INDEX = "notesIndex";
     private static final String SIGNATURE_INDEX = "signatureIndex";
     private static Integer firstPage = 2;
    
     public PageScriptlet() {
      super ();
     }
    
     @Override 
     public void afterPageInit() throws JRScriptletException{
      Map<String, JRFillVariable> variablesMap = this.variablesMap;
      if(variablesMap.containsKey(MGMT_REPORT_INDEX)){
       Integer lastPage = mgmtReportPages+1;
       String index = null;
       if(firstPage == lastPage){
        index = String.valueOf(firstPage);
       }else{
        index = String.valueOf(firstPage)+"-"+String.valueOf(lastPage);
       }
       this.setVariableValue(MGMT_REPORT_INDEX, index);
      }
    
      if(variablesMap.containsKey(INCOMESTMT_INDEX)){
       Integer firstPage = mgmtReportPages + 2;
       Integer lastPage = incomeStmtReportPages;
       String index = null;
       if(firstPage == lastPage){
        index = String.valueOf(firstPage);
       }else{
        index = String.valueOf(firstPage)+"-"+String.valueOf(lastPage);
       }
       this.setVariableValue(INCOMESTMT_INDEX, index);
      }
    
      if(variablesMap.containsKey(BALANCESHEET_INDEX)){
       Integer firstPage = incomeStmtReportPages+1;
       Integer lastPage = balanceSheetReportPages;
       String index = null;
       if(firstPage == lastPage){
        index = String.valueOf(firstPage);
       }else{
        index = String.valueOf(firstPage)+"-"+String.valueOf(lastPage);
       }
       this.setVariableValue(BALANCESHEET_INDEX, index);
      }
    
      if(variablesMap.containsKey(NOTES_INDEX)){
       Integer firstPage = balanceSheetReportPages + 1;
       Integer lastPage = addInfoReportPages;
       String index = null;
       if(firstPage == lastPage){
        index = String.valueOf(firstPage);
       }else{
        index = String.valueOf(firstPage)+"-"+String.valueOf(lastPage);
       }
       this.setVariableValue(NOTES_INDEX, index);
      }
    
      if(variablesMap.containsKey(SIGNATURE_INDEX)){
       Integer lastPage = addInfoReportPages;
       String index = String.valueOf(lastPage);
       this.setVariableValue(SIGNATURE_INDEX, index);
      }
     }
    
     @Override 
     public void beforePageInit() throws JRScriptletException{
      Map<String, JRFillVariable> variablesMap = this.variablesMap;
      Integer pageNumber = (Integer)this.getVariableValue("PAGE_NUMBER");
      if(variablesMap.containsKey(MGMT_REPORT_PAGENUM)){
       mgmtReportPages = pageNumber == null ? 1 : pageNumber + 1;
      }
      if(variablesMap.containsKey(INCOME_STMT_PAGENUM)){
       incomeStmtReportPages = pageNumber == null ? mgmtReportPages + 2 : incomeStmtReportPages + 1;
      }
    
      if(variablesMap.containsKey(BALANCE_SHEET_PAGENUM)){
       balanceSheetReportPages = pageNumber == null ? incomeStmtReportPages + 1 : balanceSheetReportPages + 1;
      }
    
      if(variablesMap.containsKey(ADDINFO_PAGENUM)){
       addInfoReportPages = pageNumber == null ? balanceSheetReportPages+1 : addInfoReportPages + 1;
      }
     }
    
    }