在JasperReports中设置文本字段的样式

ipa*_*lic 19 styles jasper-reports ireport

我知道如何在JasperReports中将内联样式应用于静态文本.可以对文本元素(文本字段)执行相同的操作吗?如果有,怎么样?

Ale*_*x K 37

是的,您可以为textField元素应用样式.

iReport使用

报告模板的示例:

<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.\nThe field's data with bold format<style isBold='true'>:" + $F{TASKS_SUBTASK} + "</style>\n<style isBold='true' isItalic='true' isUnderline='true'>The static underlined text with bold and italic format</style>"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

iReport Ultimate Guide关于markup属性的引用:

Markup属性允许您使用特定标记语言格式化文本.当您必须打印一些预格式化的文本(即HTML或RTF)时,这非常有用.可以在示例中使用简单的HTML样式标记(例如粗体和斜体)来突出显示文本的特定块.可能的值如下:


  • 无法对文本进行处理,文本的打印方式与提供的完全相同.
  • 样式
    此标记能够使用一组类似HTML的标记格式化文本,并且在Java环境中非常流行.它允许为文本,颜色,背景,样式等块设置特定字体.通常以编程方式格式化文本就足够了.
  • HTML
    如果要在报表中打印一些HTML文本,这就是您所需要的,但它的主要用途是格式化文本,因此不要期望能够打印表格或添加图像.
  • RTF
    将标记设置为此值,内容将被解释为RTF代码.RTF是一种以纯文本格式存储的流行文档格式.使用字符串:
    {\ rtf1\ansi\ansicpg1252\deff0\deflang1033 {\ fonttbl {\ f0\fswiss\fcharset0 Arial;} { 生成了插图19中"这是用RTF格式化的文本"的小文本.\f1\fnil\fprq2\fcharset0 Swift;}} {*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20这是RTF\par中的文本\ f1\fs52格式\ f0\fs20}
    该字符串实际上是使用简单文字处理器创建的RTF文件.
  • 报告字体
    这是预设字体的名称,将从中获取所有字符属性.不推荐使用此属性,只是出于兼容性原因(这就是标签被删除的原因.为了定义要在整个文档中使用的特定文本样式,请使用样式.

  • 使用的样品markup在这里.

    您可以style用于设置:

  • 共同属性
  • 图形属性
  • 边框和填充属性
  • 文字属性

    另一个样本在这里.

    DynamicJasper API使用

    如果使用DynamicJasper API,您可以在ar.com.fdvs.dj.domain.builders.ColumnBuilder类的帮助下设置样式:

    AbstractColumn columnState = ColumnBuilder.getNew()
    .addColumnProperty("state", String.class.getName())
    .addTitle("State").addWidth(new Integer(85))
    .addStyle(detailStyle).addHeaderStyle(headerStyle).build(); 
    
    Run Code Online (Sandbox Code Playgroud)

    样品在这里.

    JasperReports API使用

    如果使用JasperReports API,您可以设置样式,例如,借助net.sf.jasperreports.engine.base .JRBasePrintText类:

    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setStyle(boldStyle);
    
    Run Code Online (Sandbox Code Playgroud)

    样品在这里.

    • 我询问了内联样式,这可能是使用markup = styled.只需要小心地将样式字符串与动态元素连接起来,例如"<font face ='Arial Black'>"+ $ P {param} +"</ font>.请更新你的答案. (3认同)