Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperReports: Change font size by Param value

<style name="blueStyle" >
    <conditionalStyle>
        <conditionExpression><![CDATA[($P{INDIRIZZO}).length()>30 ?  Boolean.TRUE : Boolean.FALSE]]></conditionExpression>
        <style  style="blueStyle"  fontSize="3"/>
    </conditionalStyle>
</style>
<parameter name="INDIRIZZO" class="java.lang.String"/>

[...]

<textField>
    <reportElement x="178" y="94" width="157" height="17"/>
    <textElement>
        <font fontName="Arial" size="9"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$P{INDIRIZZO}]]></textFieldExpression>
</textField>

I want to downsize the font when INDIRIZZO length is > 30...

But this didn't work....

like image 774
Luis C. Avatar asked May 24 '12 13:05

Luis C.


People also ask

How do I change font size in Jasper report?

Use the <style/> element instead. fontName The font name, which can be the name of a physical font, a logical one or the name of a font family from the registered JasperReports font extensions. size The size of the font measured in points. It defaults to 10.

How do I change page size in iReport?

Select the report List of Products, right-click on it, and choose Page Format…. The Page format… dialog box will appear, select A4 from the Format drop-down list, and select Portrait from the Page orientation section.


2 Answers

You forgot to apply your custom style to the textField.

The correct snippet will be:

    <textField>
        <reportElement style="blueStyle" x="178" y="94" width="157" height="17"/>
        <textElement>
            <font fontName="Arial" size="9"/>
        </textElement>
        <textFieldExpression class="java.lang.String"><![CDATA[$P{INDIRIZZO}]]></textFieldExpression>
    </textField>

My working sample:

<?xml version="1.0" encoding="UTF-8"?>
<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="conditional_styl" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <style name="style1" forecolor="#66FF66" backcolor="#009966">
        <conditionalStyle>
            <conditionExpression><![CDATA[$P{parameter1}.length() < 2]]></conditionExpression>
            <style forecolor="#FFCC00"/>
        </conditionalStyle>
    </style>
    <parameter name="parameter1" class="java.lang.String"/>
    <queryString>
        <![CDATA[SELECT DOCUMENTID FROM POSITIONS]]>
    </queryString>
    <field name="DOCUMENTID" class="java.lang.Integer"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement style="style1" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Another working sample with fontSize modifying:

<?xml version="1.0" encoding="UTF-8"?>
<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="conditional_styl" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <style name="style1" fontSize="6">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 2 == 0]]></conditionExpression>
            <style fontSize="8"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 3 == 0]]></conditionExpression>
            <style fontSize="10"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 5 ==0]]></conditionExpression>
            <style fontSize="12"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 7 ==0]]></conditionExpression>
            <style fontSize="14"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 11 ==0]]></conditionExpression>
            <style fontSize="16"/>
        </conditionalStyle>
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{DOCUMENTID} % 13 ==0]]></conditionExpression>
            <style fontSize="18"/>
        </conditionalStyle>
    </style>
    <queryString>
        <![CDATA[SELECT distinct DOCUMENTID FROM POSITIONS]]>
    </queryString>
    <field name="DOCUMENTID" class="java.lang.Integer"/>
    <detail>
        <band height="34" splitType="Stretch">
            <textField>
                <reportElement style="style1" x="0" y="0" width="100" height="34"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
like image 180
Alex K Avatar answered Sep 28 '22 02:09

Alex K


You can also use styled text to allow the text size to by changed dynamically. Set the textElement markup to styled and then add wrap a <style> tag around the contents of the textFieldExpression.

<textField>
    <reportElement x="10" y="10" width="150" height="13" />
    <textElement markup="styled"/>
    <textFieldExpression>
        <![CDATA["<style size=\"" + $V{fontSize} + "\">" + $F{name} + "</style>"]]>
    </textFieldExpression>
</textField>

In this case, the text size is specified by a Integer variable that is defined in the report but it could equally come from another field or report parameter.

<variable name="fontSize" class="java.lang.Integer">
    <variableExpression><![CDATA[12]]></variableExpression>
</variable>

This allows you to do some clever things like varying the text size depending on the length of the field:

<variable name="fontSize" class="java.lang.Integer">
    <variableExpression><![CDATA[$F{firstName}.length()]]></variableExpression>
</variable>

You can see that the font size of the names below the images is based on the length of the name:

dynamic text size

like image 30
Robert Hunt Avatar answered Sep 28 '22 03:09

Robert Hunt