views:

3849

answers:

5

Hi there,

I'm working on a system that includes a large number of reports, generated using JasperReports. One of the newer features is that you can define styles for reports.

From the available docs I believe there is some way to have an external file defining styles to use, and you can reference that in your jasper reports. This allows a single style to be used by multiple reports.

I can't find any concrete information on whether this is an actual feature, and if it is, how to use it. Does anyone know if it is possible to have external styles for jasper reports, and if so, how to do it?

Thanks, Jamie

+8  A: 

To answer my own question:

The way to do this is to use JasperReport templates. There is no documentation on the web that I can find on this mechanism, though there is a demo in the subversion source. A JasperReports template is one that ends in .jrtx, and may look similar to this (styles.jrtx):

<?xml version="1.0"?>
<!DOCTYPE jasperTemplate
  PUBLIC "-//JasperReports//DTD Template//EN"
  "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd"&gt;

<jasperTemplate>
    <style name="Report Title" isDefault="false" hAlign="Center" fontSize="24" isBold="true"/>
    <style name="Heading 1" isDefault="false" fontSize="18" isBold="true"/>
    <style name="Heading 2" isDefault="false" fontSize="14" isBold="true"/>
</jasperTemplate>

and then in your .jrxml file, include it as a template:

...
<template>"styles.jrtx"</template>
...

iReport also understands this, so your styles are imported and shown in iReport correctly (though I did notice sometimes it wouldn't pick them up an a reload or recompile was necessary).

Jamie Love
Thanks for posting the answer once you found it. We use JasperReports and honestly I did not realize it could do this. This is pretty nice. Thanks again.
jschoen
+3  A: 

You can also avoid specifying the actual file name in the <template> element by using a parameter passed into your report at runtime

<parameter name="TEMPLATE_FILE" isForPrompting="false" class="java.lang.String"/>

<template><![CDATA[$P{TEMPLATE_FILE}]]></template>

where $P{TEMPLATE_FILE} is the full path to the style resource

A: 

When a new style is added to the template(jrtx), it does not show up in the combo box (list of styles). But when we type the style name, the style gets applied when report is generated.

+2  A: 

I found this page to be very informative on the subject:

http://www.themeswiki.org/Designing%5Freports%5Fusing%5FJasperReports%5F3.5

Nathan Bubna
Wonderful resource, thank you.
Maxim Veksler
A: 
roshani