Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

jasper reports - Style a text field in JasperReports

I know how to apply inline style to Static Text in JasperReports. Can the same be done for Text Elements (text fields)? If yes, how?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.
The field's data with bold format<style isBold='true'>:" + $F{TASKS_SUBTASK} + "</style>
<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:
{ tf1ansiansicpg1252deff0deflang1033{fonttbl{f0fswissfcharset0 Arial;}{f1fnilfprq2fcharset0 Swift;}} {*generator Msftedit 5.41.15.1507;}viewkind4uc1pardf0fs20 This is a text f1fs52 formatted f0fs20 in RTFpar }
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...