Question

Is it possible to concatenate subReports in a frame placed in the detail band? The goal is to display these subReports and the previous defined elements in the Detail section from a Template.

If I use dynamicReportBuilder.setTemplateFile(TEMPLATE); and dynamicReportBuilder.addConcatenatedReport(subreport); to insert the subreports in the detail Report, which is loaded from the Template, the previous defined elements in the Detail setion of the report template dissapear. So I am looking for the way to conserve these elements.

Was it helpful?

Solution

Based on your comment it seems like one of two things is happening.

1) You have the sub-report defined in the Detail band of the Template file.

In this case it will not work. The Detail band in the template file should be empty. If this is the case it is more than likely just ignoring what ever you have there and doing everything else. Check out the HOW-TO Use custum jrxml templates for more information.

2) dynamicReportBuilder.addConcatenatedReport(subreport); does not do what you think it does.

This method appends a second report to the end of the first report. Think of it more as a batch. It is the same thing as setting a value for JASPER_PRINT_LIST as an export Parameter when using the Jasper Reports API directly. Check out the HOW-TO Add Concatenated Reports for more information.


Dynamic Jasper is a great library, but is really only designed to work with standard tabular reports. It has support for some advanced features, including sub-reports, but it can be limiting.

From what I can find so far, it appears you can only add sub-reports to the Group Headers and Group Footers. So in your case you will probably need to add a Group to report first using GroupBuilder. Then you can add the sub-report to the Group Footer. The trick for the group is to make sure that each row will end up being its own group by picking the appropriate fields to group on.

You can look at the code examples in the HOW-TO Add labels in group header and footer to see how to build the groups.

To see how to add sub-reports to the report you can use DynamicReportBuilder.addSubreportInGroupFooter() method. For more details about this part and an example check out HOW-TO Add Subreports (fast way).

The other option you have is to not use Dynamic Jasper for this particular report, and just use jrxml files and the Jasper Report's API to do it yourself. It may or may not be easier depending on your setup.

OTHER TIPS

By Ricardo Mariaca. This code is the solution, Thank to Ricardo and Dynamic Report

    private void build() {
    try {
        JasperPdfExporterBuilder pdfExporterBuilder = export
                .pdfExporter(PDF_FILE);
        JasperReportBuilder jasperReportBuilderMain = report()
                .columns(
                        col.column("Item", "item", type.stringType()),
                        col.column("Quantity", "quantity",
                                type.integerType()),
                        col.column("Unit price", "unitprice",
                                type.bigDecimalType()))
                .setDataSource(createSubreportDataSource())
                // .detail(cmp.subreport(createSubreport()))
                .setWhenNoDataType(WhenNoDataType.ALL_SECTIONS_NO_DETAIL);

        JasperReportBuilder jasperReportBuilderDisclaimer = report()
                .setPageFormat(PageType.A4, PageOrientation.LANDSCAPE)
                .summary(cmp.subreport(jasperReportBuilderMain))
                .summaryWithPageHeaderAndFooter()
                .setWhenNoDataType(WhenNoDataType.ALL_SECTIONS_NO_DETAIL)
                .columnHeader(
                        cmp.text("first page header").setFixedHeight(50))
                .columnFooter(
                        cmp.text(DISCLAIMER).setStretchWithOverflow(true)
                                .setFixedHeight(250))
                .pageHeader(
                        Templates
                                .createTitleComponent("Ricardo Mariaca Approach"))
                .pageFooter(Templates.footerComponent).show()
                .toPdf(pdfExporterBuilder);

    } catch (DRException e) {
        e.printStackTrace();
    }
}

private JRDataSource createSubreportDataSource() {
    DRDataSource dataSource = new DRDataSource("item", "quantity",
            "unitprice");
    for (int i = 0; i < 180; i++) {
        dataSource.add("Book", (int) (Math.random() * 10) + 1,
                new BigDecimal(Math.random() * 100 + 1));
    }
    return dataSource;
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top