Add TextElement to PageFooter programatically

0

Hi,

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;
            }
            //ToDo
            /*if (jasperPrint.getOrientationValue() == OrientationEnum.LANDSCAPE) {
                posY = 558;
                posX = 717;
            }*/
 
            for (JRPrintPage jrPrintPage : jasperPrint.getPages()) {
 
                pageCount++;
                //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.setX(posX);
                textPageNumber.setY(posY);
                textPageNumber.setWidth(54);
                textPageNumber.setHeight(12);
                textPageNumber.setText(pageCount + " / " + totalPages);
                jrPrintElement.addElement(textPageNumber);
            }
        }

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

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

2 Answers:

0

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
68742
Joined: Apr 24 2010 - 4:31pm
Last seen: 1 year 7 months ago
0

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
Feedback
randomness