Adding an Image and Subreport to the Report Unit

A subreport is a JasperReport that’s embedded within another report: the master report. As the Master report executes, each time the server encounters a subreport element it executes the subreport. The server embeds the subreport content into the output of the Master report. Subreports can process records from a query different from the Master report's query.

To add an image from the repository to a report unit:

1. Continuing the previous example, expand the Images folder in the Repository Navigator, select the JR Logo image.
2. Drag the image to the Designer. Drop the image in the page footer band of the open report, report1.jrxml.

The plug-in adds the image element to the report, setting an expression in repo: syntax, which refers to the location of the image in the repository.

In the figure “A Repository Image Added to a Report”, you'll see the selected image file in the Repository Navigator, the image element in the page footer band of the report, and the location expression at the top and bottom of the properties list.

A Repository Image Added to a Report

To add a subreport to a report unit:

1. In iReport, assuming report1.jrxml is still open from the previous procedures, perform these steps:
a. From the palette, drag a Subreport element to the detail band of the report in the Designer.

The Subreport wizard appears.

b. On the Subreport page of the wizard, select one of these options:
Create a new report
Use an existing report
Just create a subreport element

For this example, assume you have a pre-existing and compiled JRXML file named subreport.jrxml. Select Use an existing report.

c. Browse to the jrxml version of the report, subreport.jrxml.
d. Select the .jrxml file. Click Open, then Next.

The Connection expression page of the Subreport wizard appears, offering these options:

Use the same connection as the master report

Fills the master report when the subreport is JDBC-based. The JDBC connection is passed to the subreport for execution.

Use another connection

Sets up a different JDBC connection.

Use a JRDatasource expression

Uses a JRDataSource object to fill the subreport. You will need to write an expression capable of returning a JRDataSource object.

Use an empty data source

Sets the data source expression to new JREmptyDataSource(). This expression creates a special data source that provides a single record with all the field values set to null. Useful for displaying static content in a subreport.

Don’t use any connection or data source

Used for displaying static content in a report.

e. Select the first option, Use the same connection used to fill the master report. Click Next.

The Parameters page of the subreport wizard appears.

If the report has parameters, you select the Expression drop-down next to the name of the parameter, the scroll and select the field that contains the parameter.

f. In this example, there are no parameters. On the Parameters page, click Next.

The Subreport expression page of the wizard appears.

g. On Subreport expression page of the wizard, select one of these options:
Store the directory name in a parameter

If you’re creating reports to run or preview in iReport, choose this option.

Use a static absolute path reference

If you’re creating reports to run or preview in iReport, choose this option.

the figure “A Subreport Expression” shows the Subreport expression page.

For this example, choose the Store the directory name in a parameter option, then click Finish.

A Subreport Expression

2. Stretch the subreport element until it’s wide enough to fill the band.
3. Select the subreport in the master report’s Report Inspector and edit the size of the subreport:
a. In the properties panel, edit the print margins. Set the Left and Top properties to zero because a subreport is meant to be a portion of a page, not an entire document.
b. Set the Height property to a very small value, such as 1 pixel. When the subreport element is filled with subreport data, it dynamically expands in height, so you can set its height to a very small value to leave room in the band for other elements.
4. In iReport, click File > Save to save changes to the report.
5. Synchronize the changes you made in iReportto the Report Unit Example on the server by replacing its Main JRXMLwith the JRXML you just altered in iReport:
a. In the Repository Navigator, expand the Report Unit Example folder.
b. Right-click the Main JRXML, and select Replace with current document.

The JRXML validation dialog appears. the figure “JRXML Validation” shows the JRXML validation dialog.

JRXML Validation

The server detects that the report unit includes a subreport, and the subreport needs a different expression. In iReport, subreports need this expression:

$P{SUBREPORT_DIR} + reportName.jasper

In the server, subreports need this expression:

repo : reportName expression

c. If the Subreport resource is not selected, click the checkbox to select it, then click continue to upload the subreport and the proposed expression to the repository.

When you upload a report to the server that uses the repo: syntax to refer to resources in the repository, the server prompts you to locate the resources for JRXML validation. For more information, refer to the JasperReports Server Ultimate Guide.

A message confirms that the JRXML was updated.

d. Click OK.

In the Repository Navigator, the subreport jrxml appears in the Resources folder of the Report Unit Example.

6. To run the report from iReport:
a. Right-click the Report Unit Example folder in the Repository Navigator.

You cannot run a subreport alone on the server; you must run the entire report unit.

b. Select Run JasperServer Report from the context menu.
7. To run the report in the server, first you need to link a data source to the report:
a. Log into the server.
b. Click View > Repository. Select the report unit in the repository.
c. Click Edit.
d. On the Set Up page, upload report1.jrxml.
e. On the Data Source page, link a data source to the report. For example, select the following data source from the repository:

/Analysis Components/Analysis Data Sources/FoodmartDataSourceJNDI