English 中文(简体)
贾斯珀报告:根据参数值修改字体大小
原标题: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>

我想在 & gt; 30 长度 < strengen@em> INDIIRIZZO 长度为 & gt; 30 时缩小字体大小...

但是,这没有奏效......

最佳回答

您忘了对 text Field 应用您的自定义样式。

正确的片段将是:

    <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>

我的工作样本:

<?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>

另一工作样本, 由 < engener@ em> 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>
问题回答

您也可以使用样式式文本允许文本的大小通过动态修改而允许。 将文本元素标记设置为 styped , 然后在文本内容周围添加包绑 标记 。

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

在此情况下, 文本大小由报告中定义的 < code> Integer 变量指定, 但它可能同样来自另一个字段或报告参数 。

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

这样您就可以做一些聪明的事情, 比如根据字段的长度而改变文本大小 :

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

您可以看到图像下方名称的字体大小是基于名称的长度 :

我使用i Report 2.0.2 ,虽然我可以剪切和粘贴上面的文字并使其发挥作用,但当我尝试从头开始自己的版本时,它行不通。

最后,我所做的是创建两个字段,在表达式表达式时用相反的打印相互覆盖。我确定,鉴于框的长度,57个字符将正确显示在 10 pt 字体处。

当我的表情最后变成这样时,

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)

我知道这虽然不优雅,但有效。 经过几天试图让“风格”运作和失败,我研究了其他选择,解决了这个问题。

我希望这种风格对你有用,但如果不是的话,这或许是一个好的B计划。





相关问题
org.xml.sax.SAXParseException with JasperReports [closed]

Sorry for dummy question, but i lost my whole day to start with JasperReport. The problem is that i always receive this exception: "org.xml.sax.SAXParseException: Premature end of file." when i am ...

Unwanted font Helvetica in PDF from Jasper

When I create a PDF from a Jasper Report, the resulting PDF declare to use "Helvetica" font, even if it doesn t contain text. Unfortunately I cannot embed "Helvetica" font, because it is not among the ...

Jasper Reports Excel output

I need to generate a report in Excel format using Jasperreports. I am using iReports 3.7.0 The reports gets generated with no issues except the size of empty cells. Can somebody please tell how to ...

user report generation by various attributes

User table contains the following attributes (dateOfBirth, race, gender, ...). We would like to generate a report in the following format. Year Race All Male Female 2000 Asian 2000 1000 1000 ...

Convert RTF files to DOC

I generate a report in doc format using Jasper, Jasper uses JRRTFExporter to generate doc reports, but wen i try and compare doc reports using POI it throws exception stating some header issues. Is ...

IReport Case in Variable

I write this code in one variable in the IReport $F{wbsWbkRef.wbkStdRef.stdBlRtntp.blCode} != null ? ReportUtil.getFirstEntity($P{JPA_ENTITY_MANAGER}, "SELECT "+ "CASE WHEN std.stdBlRtntp.blCode ...

热门标签