How to create Multiple static pages in Ireport / Jasper Report ?

Developers working in Business Intelligence Tools – (Reporting Tools) will often have the necessity of designing Static pages . These Static pages has to be linked into single report.

Developers has to Design Forms with many pages like Income tax return , Passport application , College application , Profile application etc. This can be achieved in an easy way.

First create a *.jrxml of the form using Ireports IDE or Jasper reports. Create separate *.jrxmls for each and every page.

In that the fields to be filled at run time has to be added as parameters.

The value to these parameters has to be send from the java class invoking the Complied Jasper files.
To pass the value to the parameters we can use HASH MAP
  
Map reportparametermap1 = new HashMap();
Map reportparametermap2 = new HashMap();
Map reportparametermap3 = new HashMap();

reportparametermap1.put("Parameter1","First Name");
reportparametermap1.put("Parameter2", "Middle Name");
reportparametermap1.put("Parameter3", "Last Name");


Next we have to link the separate pages designed ,as separate *.jrxmls into single one. For this we can use the following logic.

JasperPrint class is used to fill the compiled jasper file with dynamic values. To fill the jasper we can use the following code snippet
  
JasperPrint firstjasperprint = new JasperPrint();
firstjasperprint =JasperFillManager.fillReport(getClass().getResource
("com/page1.jasper").openStream(), reportparametermap1);

JasperPrint secondjasperprint = new JasperPrint();
secondjasperprint =JasperFillManager.fillReport(getClass().getResource
("com/page2.jasper").openStream(), reportparametermap2);

JasperPrint thirdjasperprint = new JasperPrint();
thirdjasperprint =JasperFillManager.fillReport(getClass().getResource
("com/page3.jasper").openStream(), reportparametermap3);


In the JasperPrint Class we have provision to add pages and remove pages from the JasperPrint. We can use these methods to get our expected result.

GetPages() method will give the pages present in the JasperPrint. The value returned by the function is List of JRPrintPage.

Consider we have 3 static jasper.We need to Link this into one.

1.Then take the first jasper
2.Use fillReport() to fill with parameter values. The Value returned by this function will be JasperPrint.
3.Repeat the step 1, 2 for the remaining 2 Jasper files
4.Now with GetPages() method for the 2nd jasper file get the pages available in that.
5.Then With addPage() add the pages obtain in GetPages() (from above step) with the first jasper.
6.The same step can be repeated for all the remaining jaspers.

  
JasperPrint firstsecondlinked =
multipageLinking(firstjasperprint, secondjasperprint);
JasperPrint firstsecondthirdlinked = multipageLinking(page1, thirdjasperprint);

public JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) {
List pages = page2.getPages();
for (int count = 0; count <
pages.size(); count++) {
page1.addPage(pages.get(count));
}

return page1;
}


Sample full code for Multi page Linking:

  
/* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
*
* @authored by vijayan.A
* @reviewed by Krishnaveni.T
*/
public class MultiPageLinking {

private Map fillReportParameters() {
Map parametersMap = new HashMap();
Map reportparametermap1 = new HashMap();
Map reportparametermap2 = new HashMap();
Map reportparametermap3 = new HashMap();

reportparametermap1.put("Parameter1", "First Name");
reportparametermap1.put("Parameter2", "Middle Name");
reportparametermap1.put("Parameter3", "Last Name");

reportparametermap2.put("Parameter21", "Address1");
reportparametermap2.put("Parameter22", "Address2");
reportparametermap2.put("Parameter23", "Address3");

reportparametermap3.put("Parameter31", "Phone no");
reportparametermap3.put("Parameter32", "Mobile no");
reportparametermap3.put("Parameter33", "Fax no");

parametersMap.put("reportparametermap1", reportparametermap1);
parametersMap.put("reportparametermap2", reportparametermap2);
parametersMap.put("reportparametermap3", reportparametermap3);

return parametersMap;
}

private JasperPrint fillJasperPrint(String jasperpath, Map parametersMap)
throws IOException, JRException {
JasperPrint jasperprint = new JasperPrint();
jasperprint = JasperFillManager.fillReport(getClass().
getResource(jasperpath).openStream(), parametersMap);
return jasperprint;
}

private JasperPrint getLinkedStaticpages() throws IOException, JRException {
Map page1param, page2param, page3param;
page1param = (Map) fillReportParameters().get("reportparametermap1");
page2param = (Map) fillReportParameters().get("reportparametermap2");
page3param = (Map) fillReportParameters().get("reportparametermap3");

JasperPrint firstsecondlinked = multipageLinking(fillJasperPrint("com/page1 .jasper", page1param), fillJasperPrint("com/page2.jasper", page2param));
JasperPrint firstsecondthirdlinked = multipageLinking(firstsecondlinked, fillJasperPrint("com/page3.jasper", page3param));
return firstsecondthirdlinked;
}

private JasperPrint multipageLinking(JasperPrint page1, JasperPrint page2) {
List pages = page2.getPages();
for (int count = 0; count <
pages.size(); count++) {
page1.addPage(pages.get(count));
}

return page1;
}

private void viewer(JasperPrint page1) {
JasperViewer viewer = new JasperViewer(page1, false);
viewer.setVisible(true);
}

public static void main(String arg[]) throws IOException, JRException {
MultiPageLinking multipages = new MultiPageLinking();
JasperPrint allpages = multipages.getLinkedStaticpages();
multipages.viewer(allpages);
}
}

2 comments:

Unknown said...

In List it results in that the jrprintpage has not been found.

Unknown said...

I'm using netbeans the jrprintpage has resulted in a error.

Post a Comment

Page copy protected against web site content infringement by Copyscape