JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - Stretch Sample (version 4.6.0) | ![]() |
|
|
|
Main Features in This Sample | |
| Stretching Text Fields |
![]() | ![]() | ![]() | ![]() |
|
|
top | |||||
|
|||||
![]() | Stretching Text Fields | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| How to make text fields resize dynamically and render all their runtime text content. | ||
| Since |
| 0.1.0 | ||
|
|||||
|
Pixel-Perfect Representation and Report Element Stretch
JasperReports prepares documents ready to be printed out in a pixel-perfect representation. In order to accomplish this, each report element provides four mandatory attributes used to determine the absolute position of the element within the document layout: two coordinates ( x and y ) and two rectangular dimensions (width and
height ). When all elements in the document contain static information only,
these four attributes are sufficient to completely localize an element within the page, because
the amount of space needed to represent static data can be calculated at report design time.
But dynamic elements may be involved in a report design too. These elements may acquire their data at runtime, and one cannot calculate the most appropriate dimensions needed to display the entire element's information. It is possible that when running the report the content of a text element do not fit into its precalculated area. In this case the engine will either truncate the text content or, if stretching is allowed for that element, increase the height of the element to accommodate the content. To do so, runtime measurements and calculations are required. Usually, the stretching process refers to the height adjustment only. When stretching report elements, adjusting the width could affect also the page width and raise unexpected errors at runtime (for instance, truncated information could be printed out on pages). This is why stretching an element let its width unchanged, while its height gets definitely enlarged in order to make room for all information that have to be displayed. Report Element Size Any report element's size can be calculated using the two mandatory attributes width and height , measured in pixels. They can be set
only at report design time and afterwards their value remains invariant.
This is a satisfactory solution for elements with static content only, because one can very simple to determine the rectangle area needed to expose the entire element content, and the element won't need any size adjustment at runtime. However, for dynamic elements both content and actual size are completely determined at runtime. Therefore some element stretching settings are necessary to instruct the reporting engine to ignore the element's original size attributes and allow it to stretch in height. Even in this case, the static width and height attributes still
remain mandatory, since the element may not be smaller than the originally specified dimensions.
Stretching elements in height can be managed very well, due to the section split facility: with very few exceptions (ie the column and page footers), report sections can also stretch beyond the initial specified height, and split onto next page (see the spliType attribute in the <band> element).
Report Element Position The other two mandatory attributes in a report element are the x and y coordinates,
measured in pixels, that mark the absolute position of the top-left corner of the specified element within
its parent report section.
These coordinates remain invariant if all elements in the report would have only static content. Things change when dynamic text fields are implied and some of them stretch beyond the initial dimensions to allow the whole information to be displayed at runtime. This may affect the neighboring elements in the same report section, especially those placed immediately below them. When a given element stretches in height, all elements below should rearrange themselves in order to avoid their overlapping. Usually they have to be translated with the same amount downwards along the vertical axis. As consequence, the report section containing that element will be stretched too. The positionType attribute specifies the behavior that a report element will have if
the layout of the report section in which it is been placed is stretched.
There are three possible values for the positionType attribute:
The stretchType Attribute
When stretchable text fields are present on a report section, the height of the report section itself is affected by the stretch. In this case the engine should be instructed how to represent the other report elements existing in a stretched section, in order to preserve as much as possible the pixel-perfect representation. The stretchType attribute of a report element can be used to customize the stretch
behavior of the element when the enclosing report section stretches itself according to the
text fields which stretch themselves. The element response to the modification of the report section
layout could be:
Dynamic Text Fields Text fields are elements with specific stretch behavior. When their content does not fit within their boundaries, the engine either truncate them to fit within the initial dimensions, or recalculates their height in order to make room for all the text content. Text fields are the elements which require dynamic text measurement at runtime and then start the element size recalculation for all the elements in the affected section. After text fields are measured and stretched, all other elements in the same section are resized or repositioned, according to their own stretching attributes. To decide whether a text field content gets truncated at runtime or no, one can use the isStretchWithOverflow attribute in the <textField/> element.
If true , then the text field height will be automatically increased until the
whole text content can be displayed. The default value is false .
Notes:
In some situations preserving the initial layout takes precedence over any other runtime modification. If the text content gets wider than the available area, then a truncation mechanism is applied in order to keep only the part of text which fits within the original boundaries. If one choose the text truncation as desired behavior for a text element, a series of custom properties can be set to indicate how to perform the truncation:
Text Truncation and Element Stretching Examples This sample provides some examples of element stretching and text truncations. One can see various combinations between different elements stretching attributes ( positionType , stretchType , isStretchWithOverflow ):
<elementGroup> <line> <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="5" y="5" width="1" height="16" isPrintWhenDetailOverflows="true"/> <graphicElement/> </line> <textField isStretchWithOverflow="true"> <reportElement x="10" y="5" width="100" height="16" isRemoveLineWhenBlank="true"/> <textElement textAlignment="Justified"> <font size="12"/> </textElement> <textFieldExpression class="java.lang.String"> This is a FIRST long chunk of text that will cause the text field to stretch outside its defined height and force other elements to move downwards. </textFieldExpression> </textField> <line> <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="135" y="5" width="1" height="16" isPrintWhenDetailOverflows="true"/> <graphicElement/> </line> </elementGroup>or text truncation properties: <staticText> <reportElement x="145" y="205" width="130" height="100"> <property name="net.sf.jasperreports.text.truncate.at.char" value="true"/> </reportElement> <textElement> <font size="10"/> </textElement> <text>Text elements can also be truncated at the last character that fits the element reserved area; the behavior is triggered by setting a property. This sentence might not fit fully in the space reserved for the element. </text> </staticText>Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/stretch within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/stretch/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |