Style templates are a great way to abstract style definitions that are shared across a report out to an external file. This makes them easier to maintain and understand of course, but once your report library grows you will likely consider setting up one or a small number of shared style templates, probably with particular styles per report in sub templates.
The master template file, let’s call it shared_styles.jrtx, might look something like this:
<?xml version="1.0"?> <!DOCTYPE jasperTemplate PUBLIC "-//JasperReports//DTD Template//EN" "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd"> <jasperTemplate> <style name="Default" isDefault="true" fontName="Circus"/> <style name="distinct" forecolor="#A0A0A0" backcolor="#FFFFFF" \ vAlign="Middle" fontSize="13" isBold="true" isItalic="true"/> </style> </jasperTemplate>
And let’s imagine a single sub-template, sprocket_styles.jrtx:
<?xml version="1.0"?> <!DOCTYPE jasperTemplate PUBLIC "-//JasperReports//DTD Template//EN" "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd"> <jasperTemplate> <template><![CDATA[shared_styles.jrtx]]></template> <style name="quiet" backcolor="#FFFFCC" fontSize="8" \ forecolor="#CCCCCC"/> </jasperTemplate>
For this to work, you need to add the directory containing these two jrtx files to the classpath that is passed to jasperreports.
You can also add this directory to the classpath in iReport (Preferences), and both will load the styles appropriately. This approach lets you keep your styles separate and easy to work with, and it even minimizes the change required for each report to incorporate style templates: you only need to add a style reference to the appropriate sub-template for the report, and it will load the shared one.











