Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Tuesday, December 01, 2015

Tip : Half colored text in Static Text field (OR) Text field in Jasper Reports (Or) Basic usage of Markup property in Jasper Design Studio 6.x

Hi Folks,

Some times, small things in any technology becomes harder to find and consumes time to check whether it is possible or not or how to implement it.

Today, I have come across a small requirement that is to print half colored text taken in a "Static Text Field" or "Text Filed" in Jasper reports.

Thanks to the folks who looked into it very quickly & solved it out.

I would like to share it the community on how to do this with above two elements on behalf of my folks findings. Hope you may find it useful.

Problem Statement & Solution can be understood from below image. 


Software used to Test this example : 
1) Jasper Design Studio 6.1 professional / you can use CE Studio also.
2) Empty Data source.
3) Jasper Server is not taken to test it.

Steps :
1) Create a JRXML report as usual with no need of any data source and in the report properties select when  no data type= All sections, No detail.

2)
# Drag and drop "Static Text" element from Palette to "Title" band and adjust height and width.
# Give below expression in it.
# Select the "Static Text" and go to its properties and Markup = html

<span style="color: red;">This is red colored text</span> <br>
<span style="color:green;">This is green colored Text</span>  <br>
<span> These texts are in single Static Text Field and there in single lines</span>


NOTE : As Markup=html the <br> will work to print the text in new line. 


3) Now,
#  Drag and drop "Text" element from Palette to "Title" below to above(step-2) element.
# Give below expression in it.
# Select "Text" element and go to its properties and give "Markup=styled"

"<style forecolor='#F71C19'> Hi,I am red in color..!</style>"+"\n"+
"<style forecolor='#15D636'> Hi,I am green in color..!</style>" + "\n"+
"<style forecolor='#000000'>Hello,we both are in single Text Filed and in separate lines..!</style>"


NOTE : As the lines of code is enclosed in double quotes the sentences inside them work as "JAVA" language strings and in Java we use "\n" to separate the lines. 

NOTE : Read is used to high light the code section in the article. 

4) Save the report and preview

5) Sample output will look like as shown in image in the beginning.

References : 
1) http://jasperreports.sourceforge.net/sample.reference/markup/

2) Crystal to Jasper conversion.
( In crystal it is very easy, one can select the amount of text placed in element with mouse cursor and apply the color)   


JRXML code
 <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.1.final using JasperReports Library version 6.0.0  -->
<!-- 2015-12-02T01:06:10 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Upload from server UI" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="da3cf4d1-d5ae-4e7d-acf6-5a6900abf344">
    <property name="ireport.jasperserver.url" value="http://172.16.0.28:8081/ppmreports/"/>
    <property name="ireport.jasperserver.user" value="superuser"/>
    <property name="ireport.jasperserver.report.resource" value="/organizations/training/Examples/PublishfromStudio2_files/main_jrxml"/>
    <property name="ireport.jasperserver.reportUnit" value="/organizations/training/Examples/PublishfromStudio2"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="285" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="555" height="140" uuid="d8016ec9-0d13-4c4f-8f2f-6ee73349d052"/>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="html">
                    <font size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[<span style="color: red;">This is red colored text</span> <br>
<span style="color:green;">This is green colored Text</span>  <br>
<span> These texts are in single Static Text Field and there in single lines</span>]]></text>
            </staticText>
            <textField>
                <reportElement x="0" y="145" width="555" height="134" uuid="26119933-1eb9-49bb-923b-190f735f9a59"/>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="styled">
                    <font size="16" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["<style forecolor='#F71C19'> Hi,I am red in color..!</style>"+"\n"+
"<style forecolor='#15D636'> Hi,I am green in color..!</style>" + "\n"+
"<style forecolor='#000000'>Hello,we both are in single Text Field and in separate lines..!</style>"]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <columnHeader>
        <band height="45" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="163" splitType="Stretch"/>
    </detail>
</jasperReport>


 Did you find it useful ? Please click on share to help the community. 

Cheers.!
Thank you for landing on this page.