Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - Templates Sample (version 4.0.1) | ![]() |
|
|
|
Main Features in This Sample | |
| Style Templates |
![]() | ![]() | ![]() | ![]() |
|
|
top | |||||
|
|||||
![]() | Style Templates | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| How to reuse report style definitions from external style templates. | ||
| Since |
| 2.0.0 | ||
|
|||||
|
Styles in JasperReports
In a given report, elements can have different appearances. They can have borders, foreground and background colors, alignments, text decorations, etc. JasperReports provides the <style /> element in order to manage all these appearance characteristics.
A <style /> element is defined by the following attributes:
<box /> and <pen /> styles.
In some situations, a style should be applied only if a certain condition is met. For example, consecutive rows in a table should alternate their background colors. JasperReports provides conditional styles in order to manage conditions for styles. Style Templates Any type of report element can reference a report style definition using its style
attribute. By doing so, all the style properties declared by the style definition that are
applicable to the current element will be inherited. Style properties specified at the report
element level can be used to override the inherited values.
But what if there are several reports that require the same report style to be applied? One can write the same report style definition in each report, but this is not an optimized approach. Better than that, and recommended, is to use style templates instead. Report styles can also be defined in external style template files that are referenced by report templates. This allows report designers to define in a single place a common look for a set of reports. A style template is an XML file that contains one or more style definitions. A template can include references to other style template files, hence one can organize a style library as a hierarchical set of style template files. Style template files use by convention the *.jrtx extension, but this is not mandatory. The <jasperTemplate /> element is the root of a style template file. The <template />
element is used to include references to other template files; the contents of this element
is interpreted as the location of the referred template file.
The <style /> element is identical to the element with the same name from report design
templates (JRXML files), with the exception that a style in a style template cannot
contain conditional styles. This limitation is caused by the fact that conditional styles
involve report expressions, and expressions can only be interpreted in the context of a
single report definition. Each style must specify a name, and the style names have to be
unique inside a style template.
The following is an example of a styles template file which refers a base template file named base_styles.jrtx , overrides 2 parent styles and defines other 3 own styles:
<?xml version="1.0"?> <!DOCTYPE jasperTemplate PUBLIC "-//JasperReports//DTD Template//EN" "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd"> <jasperTemplate> <template><![CDATA[base_styles.jrtx]]></template> <!-- Override the default font from base_styles.xml --> <style name="Regular" style="Base" isDefault="true" fontSize="12"/> <style name="Special Emphasis" style="Emphasis" fontSize="12" forecolor="red"/> <!-- Override the Strong style from base_styles.xml. Note that the Very Strong style from base_styles.xml will still inherit the original Strong style. --> <style name="Strong" style="Base" isUnderline="true" fontSize="14"/> <style name="Serif" style="Base" fontSize="12" fontName="DejaVu Serif"/> <style name="Serif Note" style="Serif" isUnderline="true"/> </jasperTemplate>A report can use style templates by explicitly referring them in its definition. References to a style templates are included in JRXML reports as <template /> elements. Such an
element contains an expression that is resolved at fill time to a style template instance.
The template expression can only use constants/literals and report parameters. Variables and fields cannot be used because the template expressions are evaluated before the report calculation engine is initialized. If the template expression evaluates to null, the engine ignores the template reference. Below is an example of how to reference a style template in a report: <jasperReport ...> <template>”report_styles.jrtx”</template> <!-- parameters can be used in style template expressions --> <template>$P{BaseTemplateLocation} + ”report_styles.jrtx”</template> <template class=”java.net.URL”>$P{StyleTemplateURL}</template> <parameter name=”BaseTemplateLocation”/> <parameter name=”StyleTemplateURL” class=”java.net.URL”/> ... Style template locations are interpreted in the same manner as image or subreport locations, that is, the engine attempts to load the location as an URL, a disk file or a classpath resource. The class attribute in the <template /> element represents the class for the style template source expression. Possible values are:
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/templates 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/templates/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||
|
|
© 2001-2010 Jaspersoft Corporation www.jaspersoft.com |