Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

java - How to merge two jrxml jasper report into 1 pdf same page

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...