Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...