How to collate multiple jrxml jasper reports into a one single pdf output file

后端 未结 5 664
一个人的身影
一个人的身影 2020-11-30 08:06

I have to prepare reports using five different sql queries. Each query will give out one report table.

So I wrote 5 jrxml files each corresponding to one of the abov

相关标签:
5条回答
  • 2020-11-30 08:19

    You can take advantage of exporting the whole jasperprint list:

    List jpList = new ArrayList();
    jpList.add(JRLoader.loadObjectFromFile("build/reports/Report1.jrprint")); 
    ...
    JRExporter exporter = new JRPdfExporter(); 
    exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jpList); 
    exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, stream); 
    exporter.exportReport();
    
    0 讨论(0)
  • 2020-11-30 08:22

    Page numbers without itext...

    private void drawPageNumbers(List<JasperPrint> listJasperPrint, int totalPages) throws JRException {
    
        int pageCount = 0;
        int posY = 0;
        int posX = 0;
    
        for (JasperPrint jasperPrint : listJasperPrint) {
    
            if (jasperPrint.getOrientation() == JRReport.ORIENTATION_PORTRAIT) {
                posY = 805;
                posX = 472;
            }
    
            if (jasperPrint.getOrientation() == JRReport.ORIENTATION_LANDSCAPE) {
                posY = 558;
                posX = 717;
            }
    
            for (Object obj : jasperPrint.getPages()) {
    
                pageCount++;
                JRPrintPage page = (JRPrintPage) obj;
    
                JRPrintText textTotalPages = new JRTemplatePrintText(new JRTemplateText(
                        jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
                textTotalPages.setX(posX + 54);
                textTotalPages.setY(posY);
                textTotalPages.setWidth(40);
                textTotalPages.setHeight(16);
                textTotalPages.setText(" " + totalPages);
                page.addElement(textTotalPages);
    
                JRPrintText textPageNumber = new JRTemplatePrintText(new JRTemplateText(
                        jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
                textPageNumber.setX(posX);
                textPageNumber.setY(posY);
                textPageNumber.setWidth(80);
                textPageNumber.setHeight(16);
                textPageNumber.setText("Página " + pageCount + " de");
                page.addElement(textPageNumber);
            }
        }
    
        return;
    }
    
    0 讨论(0)
  • 2020-11-30 08:27

    This answer is to help users with JASPER REPORT VERSION >5.6 (latest versions), hence remove the deprecated code.

    Since jasper-report 5.6 JRPdfExporterParameter.JASPER_PRINT_LIST is deprecated the current code of Wojtek Owczarczyk answer is:

    List<JasperPrint> jpList = new ArrayList<>();
    //add your JasperPrint's from loading jrprint or more 
    //commonly filling report with JasperFillManager.fillReport 
    
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setExporterInput(SimpleExporterInput.getInstance(jpList)); //Set as export input
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(stream)); //Set output stream
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    //set your configuration
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
    0 讨论(0)
  • 2020-11-30 08:30

    (iReport example)

    Part I:

    • create a new blank jasper report as a wrapper report for different reports
    • data adapter = One empty record - Empty Rows
    • click "New..."
    • choose "empty rows"
    • click "next"
    • number of empty records = 1 (so you will simulate on record and only ONE detail band will be printed)
    • so the blank report is ready When you call this report out of your JAVA program, first open a data connection to the database and exchange this connection in the background. SubReports of our wrapper report can inherit this connection via parameters!

    Part 2)

    • add for every report you want to embed a new detail band.
    • each detail band contains a subreport (link to the other standalone report of course)
    • set the property "run to bottom" to "True" in the subreport definition of our wrapper report

    this concept works for me. depending on parameters, you can switch different bands on or off, of course.

    0 讨论(0)
  • 2020-11-30 08:36

    you can try this one

    JasperPrint jp1 = JasperFillManager.fillReport(reportFile1,reportParams,Connection);  
    JasperPrint jp2 = JasperFillManager.fillReport(reportFile2,reportParams,Connection);  
    for (int j = 0; j < jp1.getPages().size(); j++) {  
        //Add First report to second report
        jp2.addPage((JRPrintPage) jp1.getPages().get(j));  
    } 
    
    0 讨论(0)
提交回复
热议问题