Search code examples
javajasper-reportsdynamic-jasper

How to show image in Footer for pdf report using DynamicJasper API?


I am developing a pdf report using DynamicJasper API.

As per requirement I have to display one logo at header and other at footer. I am able to display logo at header by using DynamicReportBuilder.addImageBanner method. But i didn't find a way to add an image at footer.

I saw some threads regarding this issue in dynamic-jasper forum , the one work around which mentioned there is to create a jrxml template with pageFooter band.

I tried this, but didn't work out. I got following exception:

org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed

Following is the code which generate report.

  1. Creating a dynamiceReport object
  2. Creating a jasperPrint object
  3. Invoking a method which create report based on the format type. Here i have to pass jasperPrint object, which has jrxml representation, target file location and report format type.
// ...
    DynamicReport dynamicReport = buildDynamicReport(reportTemplate, grpFields);

    // build jrxml <br/>
    JasperPrint jasperPrint = buildJRXML(dynamicReport, dataSource, reportFormat);

    // export the report into specific target format <br/>
    exportReport(jasperPrint, targetReportFile, reportFormat);
// ...

private DynamicReport buildDynamicReport(Template reportTemplate,
                                         Vector<String[]> groupFields) throws ServiceException {
    /** code to generate DynamicReport object. */
    dynamicReportBuilder.setTemplateFile("jrxml/report-footer.jrxml");
    DynamicReport dynamicReport = dynamicReportBuilder.build();

    return dynamicReport;
}

Any help would be appreciate.


Solution

  • Since the dynamicjasper's ClassicLayoutManager class applies all image banners to only header band by default, there does not seem to be a straight forward way of achieving this.

    One workaround is to use a custom layout manager implementation - extending ClassicLayoutManager and overriding the applyBanners() method.

    public class CustomLayoutManager extends ClassicLayoutManager {
    
        protected void applyBanners() {
            super.applyBanners(); //let the ClassicLayoutManager apply header banners
    
            // explicitly add banner to footer band..
            JRDesignBand pageFooter = (JRDesignBand)getDesign().getPageFooter();
    
            // if there is no footer band we create one
            if(pageFooter == null) {
                pageFooter = new JRDesignBand();
                getDesign().setPageFooter(pageFooter);
            }
    
            Vector<ImageBanner> vImageBanner = new Vector<ImageBanner>();
            vImageBanner.add(new ImageBanner("yourlogoimage.ext", 120, 50, ImageBanner.ALIGN_LEFT));
    
            applyImageBannersToBand(pageFooter, vImageBanner, null);
        }
    }
    

    and then use the custom class in while generating the report..

    JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, new CustomLayoutManager(), parameters);