Search code examples
javajasper-reports

How to get total Page count of a combined report


I've a jrxml and through java I'm setting a List<List<?>> in the bean collection. Now, my final list has 5 lists(can be more than 5 also), so the jrxml is treating all as different report in a single report and I can't get the combined page count of the report. The report shows page 1-5 for all the 5 reports.

Note: I saw some answers where its says to get count of pafe from backend, but in my case, its not feasible since the jasper printing is out of our coding feasibility. Is there a way this can be achieved from JRXML side? TIA.

<textField evaluationTime="Master">
    <reportElement x="660" y="14" width="58" height="14" forecolor="#1A75B4" uuid="24876562-c6ab-424d-9ac6-769ef9b54079">
        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
    </reportElement>
    <textElement textAlignment="Right">
        <font fontName="Albany WT" size="10"/>
    </textElement>
    <textFieldExpression><![CDATA["Page " + $V{MASTER_CURRENT_PAGE}]]></textFieldExpression>
</textField>
<textField evaluationTime="Master">
        <reportElement x="725" y="14" width="50" height="14" forecolor="#1A75B4" uuid="5c06c90b-79f2-450b-9f43-7eb00676871b">
            <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
        </reportElement>
        <textElement textAlignment="Left">
            <font fontName="Albany WT" size="10"/>
        </textElement>
        <textFieldExpression><![CDATA[" of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>

Edit 2 (Added Jasper Print code) Here helperReturnObject is a List of List:

List<JasperPrint> prints = new ArrayList<JasperPrint>();
        helperReturnObject.getTemPlatepaths().forEach(t -> {
            try
            {
                int index = helperReturnObject.getTemPlatepaths().indexOf(t);
                JasperReport jasperReport = null;

                if (!developMentFlag)
                {
                    jasperReport = (JasperReport) JRLoader.loadObject(JasperGatewayClass.class.getResourceAsStream(t));
                }
                else
                {
                    try
                    {
                        jasperReport = (JasperReport) JRLoader.loadObject(new FileInputStream(new File("path")));
                    }
                    catch (FileNotFoundException e)
                    {

                        e.printStackTrace();
                    }
                }
                JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(
                        helperReturnObject.getBeanCollections().get(index));
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                        helperReturnObject.getParameters().get(index), dataSource);
                prints.add(jasperPrint);
            }
            catch (/*JRException | NullPointerException*/ Exception e)
            {
                System.out.println(e.getMessage() + "----------------------ERROR----------------");
                e.printStackTrace();
            }
        });

Final Edit Edit 3(Working now. Had to update the code in order to get it worked for page header band):

for (JasperPrint jp : prints)
        {
            List<JRPrintPage> pages = jp.getPages();
            // Loop all pages of report
            for (JRPrintPage jpp : pages)
            {
                List<JRPrintElement> elements = jpp.getElements();
                // Loop all elements on page
                for (JRPrintElement jpe : elements)
                {
                    System.out.println(jpe.getClass().getTypeName());
                    // Check if text element
                    if (jpe instanceof JRTemplatePrintFrame)
                    {
                        JRTemplatePrintFrame jpf = (JRTemplatePrintFrame) jpe;
                        List<JRPrintElement> jpeElements = jpf.getElements();
                        for (JRPrintElement jpeElement : jpeElements)
                        {
                            if (jpeElement instanceof JRTemplatePrintText)
                            {
                                JRTemplatePrintText jpt = (JRTemplatePrintText) jpeElement;
                                // Check if current page marker
                                if (CURRENT_PAGE_NUMBER.equals(jpt.getValue()))
                                {
                                    jpt.setText("Page " + currentPage + " of"); // Replace marker
                                    continue;
                                }
                                // Check if total page marker
                                if (TOTAL_PAGE_NUMBER.equals(jpt.getValue()))
                                {
                                    jpt.setText(" " + totPageNumber); // Replace marker
                                }
                            }
                        }
                    }
                }
                currentPage++;
            }
        }

Solution

  • If the page number is inside detail band or footer, the code here will work. But since my requirement was to add the page number in page header, I had to update the code.

    for (JasperPrint jp : prints)
        {
            List<JRPrintPage> pages = jp.getPages();
            // Loop all pages of report
            for (JRPrintPage jpp : pages)
            {
                List<JRPrintElement> elements = jpp.getElements();
                // Loop all elements on page
                for (JRPrintElement jpe : elements)
                {
                    System.out.println(jpe.getClass().getTypeName());
                    // Check if text element
                    if (jpe instanceof JRTemplatePrintFrame)
                    {
                        JRTemplatePrintFrame jpf = (JRTemplatePrintFrame) jpe;
                        List<JRPrintElement> jpeElements = jpf.getElements();
                        for (JRPrintElement jpeElement : jpeElements)
                        {
                            if (jpeElement instanceof JRTemplatePrintText)
                            {
                                JRTemplatePrintText jpt = (JRTemplatePrintText) jpeElement;
                                // Check if current page marker
                                if (CURRENT_PAGE_NUMBER.equals(jpt.getValue()))
                                {
                                    jpt.setText("Page " + currentPage + " of"); // Replace marker
                                    continue;
                                }
                                // Check if total page marker
                                if (TOTAL_PAGE_NUMBER.equals(jpt.getValue()))
                                {
                                    jpt.setText(" " + totPageNumber); // Replace marker
                                }
                            }
                        }
                    }
                }
                currentPage++;
            }
        }