Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine several JasperPrint objects to have one report with mixed page orientation

The jasperPrint object has portrait orientation, but jasperPrint2 object has landscape orientation. I want to combine the two jasperprints to produce ONE pdf file but keeping their original orientation. When i add the jasperPrint2's pages to the jasperPrint then the final jasperPrint has portrait orientation... I tested the jasperPrint.setOrientation(JasperReport.ORIENTATION_LANDSCAPE) but nothing changed.

How can i produce ONE pdf file from the two jasperprints keeping their original orientation?

I have the following code :

JasperReport report = (JasperReport) JRLoader.loadObject(reportFile2.getPath());
jasperPrint = JasperFillManager.fillReport(report, parameters, conn);

JasperReport report2 = (JasperReport) JRLoader.loadObject(reportFile.getPath());
jasperPrint2 = JasperFillManager.fillReport(report2, parameters, conn);

List pages = jasperPrint2.getPages();
for (int j = 0; j < pages.size(); j++) {
    JRPrintPage object = (JRPrintPage) pages.get(j);
    jasperPrint.addPage(object);
}
like image 230
g_stam Avatar asked Dec 08 '11 09:12

g_stam


1 Answers

You can accomplish this by doing a batch export.

//put all the jasperPrints you want to be combined into a pdf in this list
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();

JasperReport report = (JasperReport) JRLoader.loadObject(reportFile2.getPath());
jasperPrintList.add(JasperFillManager.fillReport(report, parameters, conn));

JasperReport report2 = (JasperReport) JRLoader.loadObject(reportFile.getPath());
jasperPrintList.add(JasperFillManager.fillReport(report2, parameters, conn));

ByteArrayOutputStream baos = new ByteArrayOutputStream();       
JRPdfExporter exporter = new JRPdfExporter();       
//this sets the list of jasperPrint objects to be exported and merged
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
//the bookmarks is a neat extra that creates a bookmark for each jasper print
exporter.setParameter(JRPdfExporterParameter.IS_CREATING_BATCH_MODE_BOOKMARKS, Boolean.TRUE);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);     
exporter.exportReport();        
return baos.toByteArray();
like image 53
Jacob Schoen Avatar answered Nov 09 '22 11:11

Jacob Schoen