JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - Markup Sample (version 4.6.0) | |
|
|
|
Main Features in This Sample | |
| Creating Styled Text Using a Markup Language |
|
|
Secondary Features | |
Styled Text |
|
|
||||
top | |||||
|
|||||
Creating Styled Text Using a Markup Language | Documented by Sanda Zaharia | ||||
|
|||||
| Description / Goal |
| How to format text using a markup language such as HTML and/or RTF. | ||
| Since |
| 2.0.5 | ||
|
|||||
|
Structured Text Content
In order to achieve a particular look and feel, the text content in text elements can be customized with several style attributes such as text foreground color, text background color, font name, font size, font weight, etc. Some of these attributes ( forecolor ,
backcolor ) can be either set independently as report element attributes, or inherited from the
style attribute. If set, these style attributes do affect all the text content in the
report element.
But in some cases there is no need to apply a style to the entire text content. Users may want to highlight only few words within the text, without affecting the rest of the paragraph. There are situations when some words are needed in bold style, others require a specific font, and some characters have to be printed with a given color. In such cases, the solution is to use specific markup tags inside the text content, and to instruct the engine that the text content within the report element is no longer pure text, but structured XML content with specific processing rules. The XML content either includes style information in the text itself, or some other form of markup language. The markup Attribute
Here comes the markup option attribute to accomplish its job. It specifies type of markup
language that will be used inside the text element, and may have the following values:
html or rtf values are set, then the engine calls a dedicated markup processor class based on the
use of the javax.swing.JEditorPane processor. The structured XML fragment is parsed according to the
processing rules, and its content gets formatted as included style attributes require to be.
The Markup Sample This sample contains 3 structured text paragraphs. The first one is formatted using the markup="styled" value:
<staticText> <reportElement mode="Opaque" x="0" y="0" width="555" height="140" backcolor="#c0c0c0"/> <textElement markup="styled"> <font size="16"/> </textElement> <text><![CDATA[This is a <style isBold="true" isItalic="true" isUnderline="true">static text</style> element containing styled text. <style backcolor="yellow" isBold="true" isItalic="true">Styled text</style> elements are introduced by setting the <style forecolor="blue" isItalic="true">markup</style> attribute available for the <style isBold="true" forecolor="magenta">textElement</style> tag to <style forecolor="red" isItalic="true">styled</style> and by formatting the text content using nested <style isBold="true" forecolor="green">style</style> tags and simple HTML tags.]]> </text> </staticText>The second paragraph is read from the rtf.txt file in the /reports folder and stored in the $P{RtfText} parameter.
Its content gets decoded using the RTF markup processor:
<textField isStretchWithOverflow="true"> <reportElement mode="Opaque" x="0" y="150" width="555" height="140" backcolor="#c0c0c0" positionType="Float"/> <textElement markup="rtf"> <font size="16"/> </textElement> <textFieldExpression>$P{RtfText}</textFieldExpression> </textField>The third paragraph is read from the html.txt file in the /reports folder and stored in the $P{HtmlText} parameter.
Its content gets decoded using the HTML markup processor:
<textField isStretchWithOverflow="true"> <reportElement mode="Opaque" x="0" y="300" width="555" height="140" backcolor="#c0c0c0" positionType="Float"/> <textElement markup="html"> <font size="16"/> </textElement> <textFieldExpression>$P{HtmlText}</textFieldExpression> </textField>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/markup 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/markup/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |