Sample Code for Multiple Page Jasper Report

My objective was to create a 5-page invoice file in PDF format.  Each page has different layout so it’s not possible to write all of them in just one file.  What I did is I created 5 JRXML file with multiple sub-reports.  I Used iReport-3.7.0 to create the jrxml files and compile into .jasper files.  This way, I don’t have to recompile the jrxml files into jasper files.

Another issue that was solved here was the page number issue.  You’ll notice that every jasper page will have its own page number and will start with “page 1”.  To solve that, we created an input parameter called “paramRunningTotal” and was used to set the initial page number of the succeeding page.  To get the total number of pages created by the previous page, you’ll have to get the return value of the previous page and pass that value unto the next page.  (Sample code below)

int runningTotal = 0;

t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicemainfile.jrprint”);
runningTotal += t00.getPages().size();

params.put(“paramRunningTotal”, new Integer(runningTotal));

I followed the sample pseudo code that I got from the net and you can also read that here:  How To Create Multiple Page Report In Jasper Reports

I’ll be sharing you the source code and you can download it from here.  If in case you encounter dependency issues such as “RBS-Importer”  just remove them.  You don’t need those dependencies.  I used Eclipse IDE version 3.3.2 (Europa) and Java 1.6

sample code for multiple page jasper report <to be posted soon>

For those who doesn’t want to download the whole project and just want to get the source code, see the code below.  I’ve been searching for this kind of sample code before but only got a pseudo code.  I really hope this helps you on your jasper report tasks.

If you need assistance on the JRXML files, just contact me via Email or chat with me via Yahoo Messenger: rqniefes@yahoo.com

RBSReporter.java

import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import antlr.collections.List;

import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.engine.export.JRXmlExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;

public class RBSReporter {

public static void main(String[] args) {
System.out.println(“Rens”);
ConnectionManager conmngr = new ConnectionManager();
HashMap params = new HashMap();
params.put(“paramInvoiceId”, new Long(109));
Connection conn =  conmngr.openConnection();
ArrayList printList = new ArrayList();
JasperPrint t00 = new JasperPrint();
int runningTotal = 0;
try {
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicemainfile.jasper”, “/rbs/ireports/jrprint/invoicemainfile.jrprint”,params, conn);
printList.add(JRLoader.loadObject(“/rbs/ireports/jrprint/invoicemainfile.jrprint”));

t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicemainfile.jrprint”);
runningTotal += t00.getPages().size();

params.put(“paramRunningTotal”, new Integer(runningTotal));
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file_subreport1.jasper”, “/rbs/ireports/jrprint/invoicepage2file_subreport1.jrprint”,params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file_subreport2.jasper”, “/rbs/ireports/jrprint/invoicepage2file_subreport2.jrprint”,params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file_subreport3.jasper”, “/rbs/ireports/jrprint/invoicepage2file_subreport3.jrprint”,params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file_subreport4.jasper”, “/rbs/ireports/jrprint/invoicepage2file_subreport4.jrprint”,params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file_subreport5.jasper”, “/rbs/ireports/jrprint/invoicepage2file_subreport5.jrprint”,params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage2file.jasper”, “/rbs/ireports/jrprint/invoicepage2file.jrprint”,params, conn);
printList.add(JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage2file.jrprint”));
t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage2file.jrprint”);
runningTotal += t00.getPages().size();

params.put(“paramRunningTotal”, new Integer(runningTotal));
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage3file_subreport1.jasper”,”/rbs/ireports/jrprint/invoicepage3file_subreport1.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage3file.jasper”,”/rbs/ireports/jrprint/invoicepage3file.jrprint”, params, conn);
printList.add(JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage3file.jrprint”));

t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage3file.jrprint”);
runningTotal += t00.getPages().size();

params.put(“paramRunningTotal”, new Integer(runningTotal));
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage4file_subreport1.jasper”,”/rbs/ireports/jrprint/invoicepage4file_subreport1.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage4file.jasper”,”/rbs/ireports/jrprint/invoicepage4file.jrprint”, params, conn);
printList.add(JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage4file.jrprint”));
t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage4file.jrprint”);
runningTotal += t00.getPages().size();

params.put(“paramRunningTotal”, new Integer(runningTotal));
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport1.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport1.jrprint”, params, conn);
//JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport2.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport2.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport3.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport3.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport4.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport4.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport5.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport5.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport6.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport6.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file_subreport7.jasper”,”/rbs/ireports/jrprint/invoicepage5file_subreport7.jrprint”, params, conn);
JasperFillManager.fillReportToFile(“/rbs/ireports/invoicepage5file.jasper”,”/rbs/ireports/jrprint/invoicepage5file.jrprint”, params, conn);
printList.add(JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage5file.jrprint”));
t00  = (JasperPrint) JRLoader.loadObject(“/rbs/ireports/jrprint/invoicepage5file.jrprint”);
runningTotal += t00.getPages().size();
params.put(“paramRunningTotal”, new Integer(runningTotal));
JRPdfExporter exporter=new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, printList);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, “/rbs/ireports/pdf/invoicefile.pdf”);
exporter.exportReport();
System.out.println(“done”);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Related posts:

  1. How to create Multiple Page Report in Jasper reports
  2. Jasper Reports: Invalid ClassDesc Exception on Tomcat Environment
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “Sample Code for Multiple Page Jasper Report”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
CommentLuv Enabled