How to recalculate page number when combining multiple jasper reports in export?

后端 未结 4 2134
借酒劲吻你
借酒劲吻你 2020-12-06 15:06

I have decided to pass this Q/A after receiving both questions in chat and comment on answer on how to handle page number in \"combined\" jasper reports

Whe

4条回答
  •  星月不相逢
    2020-12-06 15:36

    I was inspired by @Adityaz7 answer, I think his method is the best one. But I've done this without the need to add a library to the project and to create a temporary PDF file.

    First of all modify your template and add one parameter to hold the number of previous pages:

    
    

    In the report element that contains the page number insert this expression:

    
    

    You have to use String.valueOf() method because if you use the simple sum of the elements $V{PAGE_NUMBER} + $P{PREVIOUS_PAGE_NUMBER}, Jasper will process this as a sum of strings and will print the two numbers as concatenated. (e.g. will print 11 instead of 2)

    In your java code create a variable to hold the number of previous pages and pass it to Jasper as parameter. Then fill the first report and simply add the size of the list that contains the report pages to prevPageNumber, and pass it as parameter to the second report and so on:

    int prevPageNumber = 0;
    HashMap paramMap = new HashMap();
    paramMap.put("PREVIOUS_PAGE_NUMBER", prevPageNumber);
    
    // fill the first report with prevPageNumber = 0
    JasperPrint jasperPrint1 = JasperFillManager.fillReport(report1, paramMap);
    
    if(jasperPrint1 != null && jasperPrint1.getPages() != null)
    {   
        // add previous report page number
        prevPageNumber += jasperPrint1.getPages().size(); 
    }
    
    paramMap.put("PREVIOUS_PAGE_NUMBER", prevPageNumber);
    JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, paramMap);
    

    I wrote this inline to respect the structure OP used in the question, but of course is better to do this in a loop if you have more than 2 report to print.

提交回复
热议问题