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

后端 未结 4 2130
借酒劲吻你
借酒劲吻你 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:21

    That's correct but if there are JRPrintFrame's and if your remark is in this code can not find it. Below is the one working for me: (You should find out in which container element the TOTAL_PAGE_NUMBER in!

    private void correctPageNumbers(List jasperPrintList) {
    // First loop on all reports to get totale page number
    int totPageNumber = 0;
    for (JasperPrint jp : jasperPrintList) {
        totPageNumber += jp.getPages().size();
    }
    
    // Second loop all reports to replace our markers with current and total
    // number
    int currentPage = 1;
    for (JasperPrint jp : jasperPrintList) {
        List pages = jp.getPages();
        // Loop all pages of report
        for (JRPrintPage jpp : pages) {
            List elements = jpp.getElements();
    
            // Loop all elements on page
            for (JRPrintElement jpe : elements) {
                if (jpe instanceof JRSubreport) {
                    JRSubreport jrr = (JRSubreport) jpe;
                    jrr.getBackcolor();
                }
                // Check if text element
                if (jpe instanceof JRPrintText) {
                    JRPrintText jpt = (JRPrintText) jpe;
                    System.out.println("jpt.value: " + jpt.getValue() + "  |  " + "jpt.text" + jpt.getText());
                    // Check if totale page marker
                    if (TOTAL_PAGE_NUMBER.equals(jpt.getValue())) {
                        jpt.setText(" " + totPageNumber); // Replace marker
                    }
                } else if (jpe instanceof JRTemplatePrintFrame) {
                    List frameElements = ((JRTemplatePrintFrame) jpe).getElements();
                    for (JRPrintElement jpef : frameElements) {
                        if (jpef instanceof JRPrintText) {
                            JRPrintText jpt = (JRPrintText) jpef;
                            System.out.println("jpt.value: " + jpt.getValue() + "  |  " + "jpt.text"
                                    + jpt.getText());
                            // Check if totale page marker
                            if (TOTAL_PAGE_NUMBER.equals(jpt.getValue())) {
                                jpt.setText(" " + totPageNumber); // Replace
                                                                    // marker
                            }
                        }
                    }
                }
            }
            currentPage++;
        }
    }
    

    }

提交回复
热议问题