Search code examples
javajasper-reports

Jasper reports custom exporter reading properties


I have written a few custom exporters for older versions of Jasper, but the changes in version 6 are throwing me for a loop. I have gotten to the point where I've got a JasperPrint object and can walk through the report elements, but none of the properties I've set in the report design are available. Since Jasper has changed vastly over the years, searching for examples is not helping.

Specifically what I want to accomplish is to write a custom exporter that reads properties set on individual elements in the JRXML and takes actions based on those properties. I'm not sure of what steps I need to take to make the properties in the JRXML available to the custom exporter.

A snip from my jrxml:

<staticText>
    <reportElement uuid="35d904d5-317f-4ea6-9d93-e50e5fc613b9" x="2" y="0" width="100" height="20">
        <property name="svg.t.data.ch" value="v"/>
    </reportElement>
    <textElement/>
    <text><![CDATA[Customer Name]]></text>
</staticText>

And a simplified but functional piece of code:

public class CustomExporter extends JRAbstractExporter<ReportExportConfiguration, ExporterConfiguration, ExporterOutput, JRExporterContext>{
...
@Override
public void exportReport() throws JRException   {
    JRPropertiesMap props = null;
    List<ExporterInputItem> items = exporterInput.getItems();

    for(ExporterInputItem item : items){
        jasperPrint = item.getJasperPrint();
        for(JRPrintPage page : jasperPrint.getPages()){
            for(JRPrintElement ele : page.getElements()){

                switch(ele.getOrigin().getBandTypeValue()){
                case COLUMN_HEADER:
                case GROUP_HEADER:
                case DETAIL:
                    props = ele.getPropertiesMap();
                    break;
                default:
                    break;
                }
            }
        }
    }
}

The JRPropertiesMap props is always null.


Solution

  • Design (as in JRXML) element properties are not automatically transferred to print elements, you need to configure a global property to instruct the engine to copy properties with a given prefix.

    If you want for instance to copy all svg.t.* element properties, you'll need the following property in jasperreports.properties (or set programatically)

    net.sf.jasperreports.print.transfer.foo=svg.t.
    

    You can have several net.sf.jasperreports.print.transfer.* properties, each registering a prefix for properties that are to be copied to print elements.