The Print When Expression

The Print When Expression

The print when expression in Jasper Studio or Jasper iReport is very useful in so many requirements and it is defined as the name itself implies
i.e Print /Show a String, Column and so on Based on a condition passed.
Now here are some requirements on how/when we can implement The Print when expression.

Requiremnt: Show a particular column when a parameter is selected
Here we made a parameter Called ShowLocation with values either ‘Y’ or ‘N’/blank space
now when ‘Y’ is selected then a Location column should show and when ‘N/blank space then it doesn’t show.

2 ways i did approach this:

1. I made a 2 Table Components one with the Location Column added and the other without the Location Column added.
one on top the other.
so when my parameter calls a ‘Y’ value it picks the Table with the Location Column and vice versa.

This approach is good but can be time and performance consuming

the 2nd approach was:

2. Only one Table Component was used, Opened the Table Component and selected the Column i want to restrict/show based on the parameter passed, in my scenario or requirement it is the Location column as
shown in the image below:


now i would select the cell button

Cellin the properties Tab and then Select the
Print When Expression.

Now here in the print when expression box i would write :
$P{showlocation}.equals(“Y”) as shown above


and you are good to go for a preview.
Now Let’s have to put a N or blank space in the parameter showlocation we get the following output
and now let’s pass ‘Y’ in the showlocation parameter

So Now with this condition it will show the column when the parameter is checked in with a ‘Y’ Value.
and voila the output:


Sohail Izebhijie.

Passing values as parameters to Subreport from main report in Jasper

Tool used: ireport

What is subreport?

Before going into details, let me tell you what is subreport and what is the relation between a main report and subreport. A subreport is a report which is used inside another report.  This functionality helps us during complex designs where the data comes either from different portions of a single document or from different datasources.

In some cases, there are some values which needs to be used both on the main report and the subreport. The values can be either the parameters used to call the report, variables or can be the fields generated from the query. In those cases we need to pass those values as parameters to the subreport which in turn can be used in the subreport. The main things that we need to take care of are the datatype and the naming convention.

How to pass values to subreport?

The following are the steps:

  1. First thing is to create the subreport and link the subreport with the main report. For example: Let the name of the main report be Main.jrxml and the subreport be Subrpt.jrxml.



You now have to link the subreport element that you just added to the main report, to your subreport file (the one you created in step 1) In order to do that, select the subreport element in the main report, and modify its parameters as follows:

  • Subreport Expression: “repo:/public/MainReport/Subrpt.jrxml”               à Subreport path
  • Expression Class: java.lang.String
  • Connection Type: Use a connection expression

Connection Expression: $P{REPORT_CONNECTION}


2. Now the link has been established between the main report and the subreport. We will get these parameters by passing them from the main report to the subreport. The rule is,for every field(be it either a variable/a query field/ a parameter) we want to pass to a subreporta similar parameter (with another name) has to exist in the subreportso that we can make the mapping. So we have to create a parameter on the subreport side which will have the same name as the name of the field/variable on the main report and the datatype should be the same also. For example: If the name of main report variable is X with datatype java.lang.Integer then the subreport should contain the same parameter of X with datatype java.lang.Integer. This is how Jasper understands and therefore allows the data to flow into the subreport.

3. Go to the main report and click on the subreport band. In the properties section on the right hand side corner of the report, go to parameters option(2nd last option in ireport). Click on the expression tab and a pop up window named Subreport Parameters will open. See below screenshot for better understanding.


4.While passing the parameter to the report, the Name section is the section where the name of the parameter present in the subreport is specified and in the expression section is the value you want to pass from the main report. In the above screenshot, I am just passing the parameter of the main report as my passing parameter to the subreport.

Note: It is advisable to keep the name of the parameter in subreport and the parameter’s name in the main report same. However if you want to change the name of the subreport parameter then we have to first mention over here and then mention the same name in the subreport. But we can’t change the data type of the parameter unless we are changing the type in the connection expression.

Hence, this is how we pass values(parameters/variables/fields) of main report as parameters to the subreport. Now we use that parameter anywhere in the subreport.

Thanks, Nitish



Generating Jasper reports in different formats using a JSON file as a Data Source

This article helps the reader how to fill a report with data from a JSON data source ( Generating Jasper reports in different formats using a JSON file as a Data Source ). JSON stands for Java Script Object Notation. Suppose that we have a file in the following format. JSON File The above file consists of a JSON array. The above JSON array consists of five objects of type students separated by commas. The array notation uses []. The JRXML file consists of the following data to generate the report regarding the student details. This JRXML is generated using iReport 5.6.0.


The JRXML detail tag consists of the following tags. TextFields

Using the above JSON file data we can fill the JRXML and generate reports in different formats like PDF, HTML, XLS, CSV etc. The Jasper’s API converts the .jrxml into .jasper file and out of the .jasper file a jasperprint object can be made, which can be converted into various formats for printing purposes. Using Jasper’s API requires the following jars in your class path. Add the following dependencies to your pom.xml so that maven will automatically manage the dependencies for you. Dependencies


Java code to generate the different reports.


The above code consists of an interface JRDataSource, which is implemented by JsonDataSource. The JsonDataSource’s constructor takes a file that consists of the data. For the sake of readability I have used another method which takes 4 arguments – one, the type of file to be generated; two, the JRXML file; three, the data source; four the destination of the file where the file has to be saved.

Code 2

The above printToFile method takes the data from the data source and fills it in the JRXML and generated the .jasper file which is actually the jasper print object. The jasper print object is used to generate different types of files based on the fileType argument. The above piece of code also demonstrates the use of the new API related to JRXlsExporter and JRCsvExporter, which are actually used to generate excel and csv files respectively.


Helical IT Solutions

Internationalization of jasper reports – Hello world example

Hi guys.. small work out but very use full..

Generally customers want to see data in their own language.

This example is developed using iReport 5.1.0 pro, Jasper server pro and foodmart db with postgresql.

Sources/References :

Example : Converting English to Spanish

1) From Jasper Community

2) From google translator


google_translator 3) Locale Codes



1) File->New ->Save report  to your fav directory.

2) Write a simple query and drag a field to title band( Not necessary to do but iReport requires some query and field otherwise it’ll show empty report).

3)Take Text field(Not static text) and write the following.


NOTE: $R is the special syntax used for internationalization of text in iReport.

4) Right click on report Name -> Select properties -> Give Resource bundle Name

Ex: localizationdemo

This is resource bundle name , do not give extension for this.



5) Publish your report to Jasper Server and in resources section add the bundle files.

You need to add two files..

One file contains general English text(USA) another consists internationalized text

In this example second file having spanish(B’z you are converting English to spanish)

 Right click on Resources -> Add -> Resource Bundle  id ,Name and browse for the files

You must add two files.

In this example:

File 1 ID & Name :  (You can give any of your name , extension is not mandatory). Write this text in file

localization.text1=hello world

File2 ID & Name : and add the file from your local machine. Write this text in file

localization.text1=hola mundo


In jasper server Change locale to es-Spanish


output Meet us @