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

发布于 2024-12-15 11:25:12 字数 72 浏览 2 评论 0原文

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

I know how to apply inline style to Static Text in JasperReports. Can the same be done for Text Elements (text fields)? If yes, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-12-22 11:25:12

是的,您可以为 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>

引用 iReport Ultimate Guide 关于 markup 属性:

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


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

  • 使用标记的示例位于此处

    您可以使用 style 进行设置:

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

    另一个示例位于此处

    使用DynamicJasper API

    如果使用 DynamicJasper API,您可以在 ar.com.fdvs.dj.domain.builders.ColumnBuilder 类:

    AbstractColumn columnState = ColumnBuilder.getNew()
    .addColumnProperty("状态", String.class.getName())
    .addTitle("状态").addWidth(new Integer(85))
    .addStyle(detailStyle).addHeaderStyle(headerStyle).build(); 
    

    示例位于此处

    JasperReports API 使用

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

    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setStyle(boldStyle);
    

    示例位于此处

  • Yes, you can apply style for textField elements.

    iReport using

    The sample of report's template:

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

    The quote from iReport Ultimate Guide about markup attribute:

    This Markup attribute allows you to format the text using a specific markup
    language. This is extremely useful when you have to print some text
    that is pre-formatted, that is, in HTML or RTF. Simple HTML style tags
    (like for bold and for Italic) can be used in example to
    highlight a particular chunk of the text. The possible values are as
    follows:

  • None
    No processing on the text is performed, and the text is printed
    exactly like it is provided.

  • Styled
    This markup is capable to format the text using a set of HTML-like tags and it is pretty popular in the Java environments.
    It allows to set a specific font for chunks of text, color, background, style and so on.
    It's often good enough to format the text programmatically.

  • HTML
    If you want to print some HTML text into your report, this is what you need, but it's primary use is to format text, so don't expect to be able to print tables or add
    images.

  • RTF
    Setting the markup to this value, the content will be interpreted as RTF code. RTF is a popular document format stored in pure text. The little piece of text saying “this is a text formatted in RTF” in Illustration 19 has been generated using the string:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0
    Arial;}{\f1\fnil\fprq2\fcharset0 Swift;}} {*\generator Msftedit
    5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 This is a text \f1\fs52 formatted \f0\fs20 in RTF\par }
    The string is actually an RTF file created using a simple word processor.

  • Report font
    This is the name of a preset font, from which will be taken all the character properties.
    This attribute is deprecated and it is there only for compatibility
    reason (that's why it the label is strukethrough. In order to define a
    particular style of text to use all over your document, use a style.

  • The sample of using markup is here.

    You can use style for setting:

  • Common properties
  • Graphics properties
  • Border and padding properties
  • Text properties

    The another sample is here.

    DynamicJasper API using

    In case using DynamicJasper API you can set style with help of ar.com.fdvs.dj.domain.builders.ColumnBuilder class:

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

    The sample is here.

    JasperReports API using

    In case using JasperReports API you can set style, for example, with help of net.sf.jasperreports.engine.base .JRBasePrintText class:

    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setStyle(boldStyle);
    

    The sample is here.

  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文