Jasper Report in Java Example Code
Jasper Report Library, PDF Report and Java
I am using Eclipse with Jasper Report Plugin and Maven for Automation (Building and Dependencies Management). you can instead download JasperReports® Library, if not want to use Maven.
- I am keeping my project into a package which is in.co.dreamsdoor. I will simple refer beans.CLASSNAME.java. It actually be like in.co.dreamsdoor.beans.CLASSNAME.java.
- Now, Create a new project with Maven or a simple Project and add jasper reports dependency in pom.xml or add library Java archive (jar) files in case created a simple project.
- Now, Lets begin coding.
- Create a Maven Project using IDE and update pom.xml file, which will be in project root directory.
- Now, Our Project setup is ready. We will create Java Classes and Jasper JRXML file for design.
- I am arranging my code in package, Main Package for current project is in.co.dreamsdoor.
- Our main class PDFReporting.java is in the in.co.dreamsdoor package, Other FactoryBeans will kept on in.co.dreamsdoor.beans.
- I will create two FactoryBeans classes Student, Teacher.
- Check Student.java in in.co.dreamsdoor.beans.
- Teacher.java, Second FactoryBean Class.
- Now time to create main(). Also, Creating main section of Jasper Code in PDFReporting.java.
- I use to ArrayList<>() Object for holding studentList and teacherList.
- After adding elements, Pass studentList and teacherList in JRBeanCollectionDataSource() constructor to convert into JRBeanCollectionDataSource Object.
- Now, Use HashMap<String, Object>() and put all the JRBeanCollectionDataSource Object or any other types object[¹] into HashMap< "Key", "Value">().
- Example [¹] : Other types of Object putting into HashMap< "String", Object>(),
parameter.put("title", new String("Hi, I am Title"));
- Now, We can use either jrxml raw jasper file to generate PDF or can use jasper a compiled version of JRXML file. In Development use JRXML is okay to use because all the changes reflect on report at run-time without the compiled form of JRXML, We are using the below section of code.
// File name
static final String fileName = "src/main/resources/JasperDesign.jrxml"; JasperReport jasperDesign = JasperCompileManager.compileReport(fileName);
- See the below code snippet.
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperDesign, parameter, new JREmptyDataSource());
- In JasperFillManager.fillReport(), We are passing Compiled file stream of out JRXML file i.e. jasperDesign, Object of HashMap<>() i.e. parameter and Last Parameter is Object of JREmptyDataSource().
- At the end of the class, Creating a file stream ( OutputStream outputSteam ) and creating an empty file with pdf extension, After empty file stream filling the Report content into file.
File file = new File(outFile);
OutputStream outputSteam = new FileOutputStream(file);
JasperExportManager.exportReportToPdfStream(jasperPrint, outputSteam);
- Java Part of Code End, Now We will begin work with Jasper Report.
Create a empty jrxml file by following Below instruction. File must be kept on src/main/resources/JasperDesign.jrxml . Focus on the src/main/resources/.
File → Other → JasperSoft Studio → Jasper Report → All → Blank A4
→ Finish.
Now, Something like below screen is opened into your IDE.
- Delete bands by click on or by selecting from outline and then press delete button.
- Right Click on Parameters, In outline and create parameters
- We will create 3 parameter by referring String part (key) of and HashMap<String, Object>();
parameter.put("studentDataSource", studentCollectionDataSource);
parameter.put("teacherDataSource", teacherCollectionDataSource);
parameter.put("title", new String("Hi, I am Title"));
- Where title is of type String, and studentDataSource and teacherDataSource are of type net.sf.jasperreports.engine.data.JRBeanCollectionDataSource .
- Check into Properties section below the editor section. Setup proper class and Parameter name as put into HashMap and uncheck Is for Prompting is not required. title is of type String.
- Now, creating next studentDataSource of type net.sf.jasperreports.engine.data.JRBeanCollectionDataSource, See the below image. In the same way, We create for teacherDataSource.
- Now, As we know ArrayList holds multiple values, So we will create data-set and its field.
- Now click on Finish.
- Follow the same instruction and create data-set for teacher, Once done with. Create Fields for the Data-set by referring Factory Beans Classes Student and Teacher.
// Student.java
private int id;
private String name;
private String std; // Teacher.java private int id;
private String name;
private double salary;
- Now set the properties Like name and it's type
- Repeat the same for all the field that you want to display on report and also repeat same for Teacher.
- After adding all field it will looks below image.
- Now, Drag title Parameter into Title Band/Section of Report File.
- We will add 2 table to display our Student and Teacher data. Drag Table and Select Create a Table using existing dataset.
- Click on Next,
- After, Click on Parameter and Then Click finish and then click Next on Table Wizard and then select below
- Once down with the Field selection and Table Customization Window and select the option based on your choice. (Repeat same for second Dataset also).
- You are Done with work.
- Report looks like after work…
- Now, Run the Code and see the a file with name Reports.pdf is generated into resources folder. If code run successfully.
- Final PDF is something like this.
- Follow me on Rajendra Kumar Yadav.
- Find me on GitHub https://github.com/rajendrakumaryadav
- Find the code on github : https://github.com/rajendrakumaryadav/PDFReporting
- If you want to use the nearly same code with MySQL follow git repo: https://github.com/rajendrakumaryadav/JasperProject
Thanks you for Reading 📃 this article 📰… Happy 😊 Coding 🖥…
Source: https://medium.com/@_rajendrayadav/pdf-generation-using-jasper-report-and-java-81140d2bc80b
0 Response to "Jasper Report in Java Example Code"
Post a Comment