How to merge two jrxml jasper report into 1 pdf sa

2019-08-03 17:31发布

I tried answer from this site to merge two jrxml jasper into 1 pdf file. Created file contain two page...

How to merge two jrxml into one page?

JasperPrint jasperPrint1 = JasperFillManager.fillReport(getClass().getResourceAsStream("/coba.jasper"), param, new JREmptyDataSource());
JasperPrint jasperPrint2 = JasperFillManager.fillReport(getClass().getResourceAsStream("/coba2.jasper"), param2, new JREmptyDataSource());

            List pages = jasperPrint2.getPages();
            for (int i = 0; i < pages.size(); i++) {
                JRPrintPage object = (JRPrintPage)pages.get(i);
                jasperPrint1.addPage(object);
            }                                   
JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jasper);
            OutputStream output = new FileOutputStream(new File("d:\\demo.pdf"));
            exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
            exporter.exportReport();

1条回答
smile是对你的礼貌
2楼-- · 2019-08-03 17:53

This is since you are adding a page addPage, you need to add the elements on a page to another page.

JRPrintPage page1 = jasperPrint1.getPages().get(0);
List<JRPrintPage> pages2 = jasperPrint2.getPages();
for (JRPrintPage page : pages2) {
   List<JRPrintElement> elements = page.getElements();
   for (JRPrintElement element : elements) {
     page1.addElement(element);
   }
}

This code will add all elements of jasperPrint2 (in all its pages) in page 1 on jasperPrint1

NOTE: Error handling (Nullpointer, ArrayIndexOutOfBounds) is beyond the scope of the example.

查看更多
登录 后发表回答