Search code examples
javajasper-reports

Overriding page number in JapserReport not reflecting on final PDF


I'm trying to export the List<JapserPrint> into a single PDF file. Because I'm dealing with multiple JapserPrint objects, I need to change the page number dynamically in java code.

I tried the following solution

I've made one parent JapserPrint object out of List

private void overridePageNumbers(final JasperPrint parentJapserPrint) {

    final int totalPages = jasperPrints.getPages().size();

    final AtomicInteger currentPageNumber = new AtomicInteger(1);

    final List<JRPrintPage> listPages = parentJapserPrint.getPages();

    for (final JRPrintPage currentPage : listPages) {
        final List listElements = currentPage.getElements();

        for (final Object element : listElements) {
            if (element instanceof JRTemplatePrintText) {
                final JRTemplatePrintText templatePrintText = (JRTemplatePrintText) element;

                // set currrent page
                if (templatePrintText.getKey() != null &&
                        templatePrintText.getKey().equalsIgnoreCase("textFieldCurrentPage")) {
                    LOGGER.info(templatePrintText.getFullText());
                    templatePrintText.setText("Page " + currentPageNumber.getAndIncrement() + " of ");
                    LOGGER.info(templatePrintText.getFullText());
                }

                // set total number of pages
                if (templatePrintText.getKey() != null &&
                        templatePrintText.getKey().equalsIgnoreCase("textFieldNumberOfPages")) {
                    LOGGER.info(templatePrintText.getFullText());
                    templatePrintText.setText(String.valueOf(totalPages));
                    LOGGER.info(templatePrintText.getFullText());
                }
            }
        }
    }
}

    <pageFooter>
    <band height="17">
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
        <textField pattern="EEEEE dd MMMMM yyyy">
            <reportElement mode="Transparent" x="0" y="4" width="100" height="13" uuid="4e52ad3d-806e-45fa-ba2c-0d026ce60568"/>
            <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement key="textFieldCurrentPage" mode="Opaque" x="573" y="4" width="192" height="13" uuid="d4f96443-350c-49fe-a0af-60ad86fb0f30"/>
            <textElement textAlignment="Right"/>
            <textFieldExpression><![CDATA["${CURRENT_PAGE_NUMBER}"]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement key="textFieldNumberOfPages" mode="Opaque" x="768" y="4" width="31" height="13" uuid="81bfe333-c59e-415c-b570-9136c4ddf6d7"/>
            <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
        </textField>
    </band>
</pageFooter>

With the help of log, It seems like the text value is being set. But the final output PDF file doesn't show overridden value.

18:42:05.967 INFO  com.company.service.ReportService - [n/a] Page 1 of
18:42:05.968 INFO  com.company.service.ReportService - [n/a] Page 1 of 
18:42:05.970 INFO  com.company.service.ReportService - [n/a] Page 2 of
18:42:05.971 INFO  com.company.service.ReportService - [n/a] Page 2 of 
18:42:05.975 INFO  com.company.service.ReportService - [n/a] Page 1 of
18:42:05.975 INFO  com.company.service.ReportService - [n/a] Page 3 of 
18:42:05.977 INFO  com.company.service.ReportService - [n/a] Page 2 of
18:42:05.978 INFO  com.company.service.ReportService - [n/a] Page 4 of 

Solution

  • I found what the problem is, on my own

    In my code, I'm using virtualizer to minimize the memory utilization and to avoid the system to run into out of memory exception. You can read about it more here

    final JRGzipVirtualizer virtualizer = new JRGzipVirtualizer(10);
    .
    . // other code
    .
    virtualizer.setReadOnly(true);
    

    new JRGzipVirtualizer(10) means it only keeps 10 pages at a time in the memory and remaining it stores in a compressed form. I'm also overriding virtualizer's read-only mode to true. This is what leads to my problem. It doesn't allow us to override the JasperPrint object once it has been generated

    Solution: remove the code virtualizer.setReadOnly(true);