JasperReports logo version 7.0.0 Home - Samples - Configuration - Functions - FAQ - API (Javadoc)

JasperReports - Images Sample

Shows how images could be used in a report template.

Main Features in This Sample

Images

Images

Documented by Sanda Zaharia

Description / Goal
How to render images in reports using the build-in image element.

Since: 0.1.0

Other Samples
/demo/samples/horizontal
/demo/samples/jasper

Images in JasperReports

Below is an image:

This is a jpg image

Images are the most complex graphic objects in a report. Just as for text field elements, their content is dynamically evaluated at runtime, using a report expression. The process of image rendering depends on a set of attributes and expressions available for the image report elements (<element kind="image"/>).

Image Attributes

If the scale type for the image is Clip or RetainShape and the actual image is smaller than its defined size in the report template or does not have the same proportions, the image might not occupy all the space allocated to it in the report template. In these conditions the image can be both horizontally and vertically aligned within its report element. Two attributes are responsible for the image alignment:

Image Expression

The value returned by the image expression is the source for the image to be displayed. The image expression is introduced by the element and can return values from only the limited range of classes listed following:

Image Hyperlinks

In JasperReports image elements provide hyperlink support. All available hyperlink expressions are allowed to be part of an image element. See also the hyperlink sample.

Image Examples

This reports/ImagesReport.jrxml report template contains a series of image examples, having a specific combination of the attributes above.

The first image in the report is loaded from the current folder at export time.

<element kind="image" width="150" height="40" onErrorType="Error">
  <expression><![CDATA["dukesign.jpg"]] ></expression>
  <box>
    <pen lineWidth="0.5"/>
  </box>
</element>

The last image in the report template is loaded from https://jasperreports.sourceforge.net website and is scaled to retain the image shape. It also provides a hyperlink expression pointing to its remote location:

<element kind="image" mode="Opaque"
  x="400" width="150" height="100" backcolor="#DDFFDD" scaleImage="RetainShape" onErrorType="Icon" linkType="Reference" lazy="true">
  <expression><![CDATA["https://jasperreports.sourceforge.net/jasperreports.svg"]] ></expression>
  <hyperlinkReferenceExpression><![CDATA["http://jasperreports.sf.net/jasperreports.svg"]] ></hyperlinkReferenceExpression>
</element>

Running the Sample

Running the sample requires the Apache Maven library. Make sure that maven is already installed on your system (version 3.6 or later). In a command prompt/terminal window set the current folder to demo/samples/images within the JasperReports source project and run the following command:

> mvn clean compile exec:exec@all

It will generate all supported document types containing the sample report in the demo/samples/images/target/reports directory.




© 2001- Cloud Software Group, Inc. www.jaspersoft.com