Skip to Content

This blog is the continuation of Part-I: Print Web Dynpro Java applications using JasperReports, in order to get the concept I would recommend you to go through it before reading this post.

Part-II will discuss the working of JasperReports practically, using Java code. In this example we will create a JasperReport template that will take name as input and will print greetings with name, just like this.

Hello and welcome Jawed Ali.

To develop the template, open the iReport application. We will be using this graphical tool to design our report template. Go to File menu and select New Document, or simply press Ctrl+N. Create Report dialogue box will open, give your report a suitable name and press Ok button. We will use all the default values for the remaining parameters.

image

JasperReport document is divided into section, with specific purpose, called bands. For our example we only require title and detail band. Rest of the bands will be hidden by setting their height property to zero. Set height of detail band to 200 and title band to 50. Now add Static Text Element into title band, adding element is simple, select the Static text Element from the toolbar and draw the element into title band. As shown in the figure 2.  Right click on the static text element and select properties. Change the font properties to your preference and set text “Hello Report Template” in static text tab. Similarly add another Static Text Element, into detail band and set the text “Hello and Welcome”.

image

Now add Text Field Element, which will display the name as passed from outside.  Before we add text field we need to define a field (A field is variable that takes values from external source i.e. program generating report and displays into Text Field Elements). Check out the document structure section on your left side. Right click on the document node and select Add > Field, a new window will open. Put NAME into field name and select java.lang.String as field class type, give your field some description if you want. Select Text Field Element from tool bar and place it into detail band. Its time to like the NAME field with Text Field Element, right click on the element and select properties, now select Text Field tab and put $F {NAME} into Text Field Expression and close the window. Save the template to your desirable location with name HelloReport.jrxml and we will store this file in the project directory later.

Its time to create Java program for generating PDF report. Open your developer studio and create new Java Project “JRTest”, as shown in the figure 3:

image

In the navigation section right click on the JRTest node and select properties to set project specific configurations. Select Source tab and change the Source to JRTest/src and Output folders to JRTest/bin. Now select Libraries tab and add libraries by clicking Add External Jars button. After you are finished configuring your properties may look like the figure 4:

image

 

image

Now you have done your required configuration, it’s time to write some code. First we have to move our Report template, created earlier, to the source directory i.e. JRTest/src. The reason for moving the template here is simple so that we don’t have to worry about the location of the file, type of operating system and etc. Now we need to create two Java classes i.e. HelloReport.java and Test.java. HelloReport class will be used to generate the PDF and Test class will be used for running the HelloReport. I have separated these two functionalities, so that we could use HelloReport.java with our Dynpro Application (will be discussing in next blog of the series) without any configuration.

When you are finished your navigation panel should look like as shown in figure 5. We are all set to execute our project. Upon successful execution it will create a PDF file “HelloJasperReport.pdf” in your project root directory i.e. “/JRTest”. 

 

 

 

 

 

 

 

 

 

 

//HelloReport.java

package com.maknology.test;

import java.util.ArrayList;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;

/**
 * @author jawed
 *
 */
public class HelloReport {
    private String reportPath = null;
    private JRMapCollectionDataSource dataSourceMap = null;
       
    public HelloReport()
    {
        System.out.println(HelloReport.class.getResource(“/HelloReport.jrxml”));   
        dataSourceMap = new JRMapCollectionDataSource(new ArrayList());
    }

    public HelloReport(ArrayList dataList)
    {
        dataSourceMap = new JRMapCollectionDataSource(dataList);
    }
   
    public byte[] getReportData() throws Exception
    {
        byte[] pdfData = null;
        try
        {
            JasperReport report = JasperCompileManager.compileReport(HelloReport.class.getResourceAsStream(“/HelloReport.jrxml”));                                                   
            JasperPrint reportPrint = JasperFillManager.fillReport(report, null, dataSourceMap);           
            pdfData = JasperExportManager.exportReportToPdf(reportPrint);

        }catch(Exception e)
        {
            e.printStackTrace();   
            throw e;       
        }
        return pdfData;
    }
}

//Test.java

package com.maknology.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author jawed
 *
 */
public class Test {
    public static void main(String args[]) throws Exception
    {
        OutputStream opStream = null;
        try{
            //Preparing Report and its Data
            HashMap data = new HashMap();
            data.put(“NAME”, “Jawed Ali”);
            ArrayList dataList = new ArrayList();
            dataList.add(data);
       
            HelloReport report = new HelloReport(dataList);
           
            //Generating Report
            opStream = new FileOutputStream(new File(“./HelloJasperReport.pdf”));           
            opStream.write(report.getReportData());
                           
            System.out.println(“Report generated.”);       
        }catch(Exception e)
        {
            System.out.println(“Error while generating report.”+ e.getMessage());
            e.printStackTrace();
        }finally{
            opStream.flush();
            opStream.close();
        }
    }
}

 

In the Part-III: Print Web Dynpro Java applications using JasperReports of this blog we will see how to integrate this example into your WebDypro Java application.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Pietro Rossi
    Compliments of the good blog and I wanted to ask when it comes out the chapter “Part III: We will be integrating JasperReports into WDJ applications.”?
    Thanks
    Regards
    Banana
    (0) 

Leave a Reply