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

JasperReports - Text Export Sample

Shows how the pure text exporter could be used to export reports to simple text files.

Main Features in This Sample

Exporting to Pure Text Format

Exporting to Pure Text Format

Documented by Sanda Zaharia

Description / Goal
How to export reports to plain text files.

Since: 1.0.1

Plain Text Export - Overview

One could imagine situations which require the text information from a JasperReport document to be saved separately as pure text document. In this case graphics, images, styles and pixel perfectness are not conserved. Text elements are converted into simple texts to be written in documents with predefined page widths and heights, measured in characters.

The JRTextExporter implementation is intended to serve to this purpose. Users can specify the desired page width and height (in characters), then the engine will try to best fit text elements into the corresponding text page. Taking into account that positions and sizes are measured in pixels in a JasperReports template, and page dimensions in a text document are set in number of characters, some precautions must be taken when creating reports for text export:

Plain Text Export - Parameters And Properties

The basic idea when exporting to plain text output is to find the appropriate pixel/character ratio and convert sizes in the report in order to generate enough space for the exported text. The following exporter configuration settings and properties are very helpful to accomplish such a task:

Other useful configuration settings are:

The Text Export Sample

The demo/samples/text/reports/TextReport.jrxml template contains an example of report design prepared for the plain text export.

One has to pay attention to the following properties defined in the report:

<property name="net.sf.jasperreports.export.text.character.width" value="7.238"/>
<property name="net.sf.jasperreports.export.text.character.height" value="13.948"/>

They assume that characters are 7.238px wide and 13.948px high. Having a page width of 798px and a page height of 1000px, it follows that pages in the generated text document will count 110 characters per line and 71 lines per page.

Also notice that element sizes are set enough large to make room for all their content to be represented in the plain text output.

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/hsqldb within the JasperReports source project and run the following command:

> mvn exec:java

This will start the HSQLDB server shipped with the JasperReports distribution package. Let this terminal running the HSQLDB server.

Open a new command prompt/terminal window and set the current folder to demo/samples/text within the JasperReports source project and run the following command:

> mvn clean compile exec:exec@all

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




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