Add TextElement to PageFooter programatically



I'm currently trying to add a text element programatically in a JRPrintPage object.

I've a working full report which is build by concatenating multiple reports, I've had to do this in order to mix portrait and landscape report.

My problem is that I have to insert page number inside the pageFooter of those page after filling these reports. I have the total number of page and also the number of the current page.

JasperReports version : 6.2.0

I've tried the following without any success : 

private void drawPageNumbers(List<JasperPrint> listJasperPrint, int totalPages) throws JRException {
        int pageCount = 0;
        int posY = 0;
        int posX = 0;
        for (JasperPrint jasperPrint : listJasperPrint) {
            if (jasperPrint.getOrientationValue() == OrientationEnum.PORTRAIT) {
                posY = 9;
                posX = 273;
            /*if (jasperPrint.getOrientationValue() == OrientationEnum.LANDSCAPE) {
                posY = 558;
                posX = 717;
            for (JRPrintPage jrPrintPage : jasperPrint.getPages()) {
                //get Footer
                JRPrintElement jrPrintElement = jrPrintPage.getElements().get(jrPrintPage.getElements().size()-1);
                //JRPrintText textPageNumber = new JRTemplatePrintText(new JRTemplateText(originFooter, jasperPrint.getDefaultStyleProvider()));
                JRBasePrintText textPageNumber = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
                textPageNumber.setText(pageCount + " / " + totalPages);

The jrxml structure of the PageFooter is the following:

<pageFooter> <band>  <frame>   <!-- various text element -->  </frame> </band></pageFooter>

Hope someone can help me, if you need more informations, don't hesitate to ask them !

Thanks in advance,


jim.lemaire's picture
Joined: Sep 3 2015 - 2:46am
Last seen: 2 years 9 months ago

2 Answers:


I had a similar situation but instead of trying to modify JasperPrint object after it was created, I just passed the starting page number as a parameter for pages after the first.


hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 1 year 7 months ago

I did it that way at first but only to realize that some of my reports may contain more than 1 page, then you only know that once it's compiled so it "falsify" the counter 

jim.lemaire's picture
Joined: Sep 3 2015 - 2:46am
Last seen: 2 years 9 months ago