JasperReports: changing font size by parameter 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 reduce the font size when INDIRIZZO length> 30 ...

But that did not work.

+4
source share
2 answers

You forgot to apply your own style to the text box.

The correct snippet would 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 example with fontSize :

 <?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> 
+10
source

I am using iReport 2.0.2, and although I can cut and paste the above and make it work when I try to execute my own version from scratch, this will not work.

What I have finished is to create two fields superimposed on each other with the opposite print in the expressions. I determined that given the window length, that 57 characters would be displayed correctly with a font of 10 pt.

My final print when the expressions ended as follows:

 Box 1 ($F{DATA1}.length() >= 58 ? Boolean.TRUE : Boolean.FALSE) (font size is lowered) Box 2 ($F{DATA1}.length() < 58 ? Boolean.TRUE : Boolean.FALSE) (10 pt font size) 

I know this is not elegant, but it works. A few days later, trying to get Style to work and fail, I looked at other options and settled on that.

Hope the style works for you, but if not, it could be a good plan B.

0
source

Source: https://habr.com/ru/post/1414142/


All Articles