Search code examples
javajasper-reportsdynamic-jasper

Generating both landscape and portrait in a single report using DynamicJasper API


I have different modules in my application. Each module can be exported in excel, word and pdf. The format of report are in either landscape or portrait. Now I need to compile all those reports and export as a single. But I am facing a problem that while exporting, DynamicJasper shows in a single format only i.e either landscape or portrait.

How can I show my report using both format in a single report using DynamicJasper API?


Solution

  • You can create a Master Report and add subreports to it, this is an example of the master report and how to add Subreports:

    public DynamicReport buildDynamicReport() throws ReportDocumentGenerationException {
      DynamicReportBuilder drb = new DynamicReportBuilder();
      drb.setDetailHeight(detailHeight)     
      .setMargins(properties.getReportMargins().getTopMargin(), properties.getReportMargins().getBottomMargin(), properties.getReportMargins().getLeftMargin(), properties.getReportMargins().getRightMargin())     
      .setDefaultStyles(null, null, null, getSpoolStyle(SpoolRow.PLAIN_ATTRIBUTE))    
      .setPageSizeAndOrientation(new Page(**/*Your document dimensions and orientation*/**)
      .setColumnsPerPage(1);
       for(ReportDocumentInformationPage page: reportInformation.getPaginas()){
                drb.addConcatenatedReport(getPageSubReport(page.getPageNumber()),new ClassicLayoutManager(),"DS"+page.getPageNumber().toString(),DJConstants.DATA_SOURCE_ORIGIN_PARAMETER, DJConstants.DATA_SOURCE_TYPE_JRDATASOURCE,!page.getPageNumber().equals(1));
                params.put("DS"+page.getPageNumber().toString(), getReportJRDataSource(page.getPageNumber())  );
            }
            drb.setUseFullPageWidth(true);
            DynamicReport dr = drb.build();
            return dr;
        }
    

    This is an example on how to create those subreports:

    *private DynamicReport getPageSubReport(int i) throws ReportDocumentGenerationException {
            try{
                DynamicReportBuilder drb = new DynamicReportBuilder();
                drb.setDetailHeight(detailHeight)
                .setReportName("Reporte"+i)
                .setMargins(properties.getReportMargins().getTopMargin(), properties.getReportMargins().getBottomMargin(), properties.getReportMargins().getLeftMargin(), properties.getReportMargins().getRightMargin())
                .setDefaultStyles(null, null, null, getSpoolStyle(SpoolRow.PLAIN_ATTRIBUTE))    
                .setPageSizeAndOrientation(new Page(**/*Your specific Page dimensions and orientation***/)
                .setColumnsPerPage(1);
    
                AbstractColumn spoolColumn = ColumnBuilder.getNew()     
                .setColumnProperty("value", String.class.getName())     
                .setTitle(null)     
                .setWidth(150)
                .build();       
                spoolColumn.setConditionalStyles(getSpoolConditionalStyle());
    
                drb.addColumn(spoolColumn);
                drb.setUseFullPageWidth(true);
                drb.addField("attributes", String.class.getName());
                DynamicReport dr = drb.build();
                return dr;
            }catch(ColumnBuilderException cbe){
                cbe.printStackTrace();
                throw new ReportDocumentGenerationException("No se pudo definir correctamente la columna del reporte para la pagina "+i);
            }catch(Exception e){
                e.printStackTrace();
                throw new ReportDocumentGenerationException("No se pudo generar la pagina "+i+" del reporte");
            }
        }*
    

    Hope it helps.