Style a text field in JasperReports

Yes, you can apply style for textField elements.

iReport using

The sample of report’s template:

<jasperReport ..>
    <style name="ColoredField" style="Default" forecolor="#FF0000">
        <conditionalStyle>
            <style/>
        </conditionalStyle>
    </style>
    ...
    <detail>
        <band height="52" splitType="Stretch">
            <!--Using the style declared in this template-->
            <textField>
                <reportElement key="textWithStyle" style="ColoredField" mode="Opaque" x="0" y="10" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{TASKS_SERIES}]]></textFieldExpression>
            </textField>
            <!--Basic formatting (set font and indent) using-->
            <textField>
                <reportElement key="textWithoutStyle" x="100" y="10" width="100" height="20"/>
                <textElement>
                    <font fontName="Arial" size="14" isBold="true" isItalic="true" isUnderline="false"/>
                    <paragraph leftIndent="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{TASKS_TASK}]]></textFieldExpression>
            </textField>
            <!--Markup using: styled-->
            <textField>
                <reportElement x="200" y="10" width="590" height="42"/>
                <textElement markup="styled"/>
                <textFieldExpression><![CDATA["The static text without any format.\nThe field's data with bold format<style isBold='true'>:" + $F{TASKS_SUBTASK} + "</style>\n<style isBold='true' isItalic="true" isUnderline="true">The static underlined text with bold and italic format</style>"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The quote from iReport Ultimate Guide about markup attribute:

This Markup attribute allows you to format the text using a specific markup
language. This is extremely useful when you have to print some text
that is pre-formatted, that is, in HTML or RTF. Simple HTML style tags
(like for bold and for Italic) can be used in example to
highlight a particular chunk of the text. The possible values are as
follows:

  • None

    No processing on the text is performed, and the text is printed
    exactly like it is provided.
  • Styled

    This markup is capable to format the text using a set of HTML-like tags and it is pretty popular in the Java environments.
    It allows to set a specific font for chunks of text, color, background, style and so on.
    It’s often good enough to format the text programmatically.
  • HTML

    If you want to print some HTML text into your report, this is what you need, but it’s primary use is to format text, so don’t expect to be able to print tables or add
    images.
  • RTF

    Setting the markup to this value, the content will be interpreted as RTF code. RTF is a popular document format stored in pure text. The little piece of text saying “this is a text formatted in RTF” in Illustration 19 has been generated using the string:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0
    Arial;}{\f1\fnil\fprq2\fcharset0 Swift;}} {*\generator Msftedit
    5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 This is a text \f1\fs52 formatted \f0\fs20 in RTF\par }
    The string is actually an RTF file created using a simple word processor.
  • Report font
    This is the name of a preset font, from which will be taken all the character properties.
    This attribute is deprecated and it is there only for compatibility
    reason (that’s why it the label is strukethrough. In order to define a
    particular style of text to use all over your document, use a style.
  • The sample of using markup is here.

    You can use style for setting:

  • Common properties
  • Graphics properties
  • Border and padding properties
  • Text properties

    The another sample is here.

    DynamicJasper API using

    In case using DynamicJasper API you can set style with help of ar.com.fdvs.dj.domain.builders.ColumnBuilder class:

    AbstractColumn columnState = ColumnBuilder.getNew()
    .addColumnProperty("state", String.class.getName())
    .addTitle("State").addWidth(new Integer(85))
    .addStyle(detailStyle).addHeaderStyle(headerStyle).build(); 
    

    The sample is here.

    JasperReports API using

    In case using JasperReports API you can set style, for example, with help of net.sf.jasperreports.engine.base .JRBasePrintText class:

    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setStyle(boldStyle);
    

    The sample is here.

  • Leave a Comment