External Styles in JasperReports
Use JasperReport templates. 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">
<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><![CDATA["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).
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