用图片替换一些文本:如何绘制图像而不是句子中的单词 [英] Replace some text with a picture: how to draw the image instead of word in sentence

查看:57
本文介绍了用图片替换一些文本:如何绘制图像而不是句子中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 csv 数据源( holidays.csv ):

name,date
New Year's Day,January 1st
Christmas Day,December 25th
Valentine's Day,February 14th

报告的设计

我放置了两个 textFields 来显示假期的名称和假期的日期.我已经将 image 元素放置在带有节日名称的 textField 下.

The report's design

I've placed two textFields for showing holiday's name and the holiday's date. And I've placed the image element for showing image under the textField with holidays name.

printWhenExpression 的帮助下,我正在显示假期的名称或图像.

With help of printWhenExpression I'm showing the holiday's name or image.

iReport 中的报告设计:

jrxml 文件:

<?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="holidays" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e0310045-780b-4af3-aa7b-ed7f5da1985f">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="date" class="java.lang.String"/>
    <detail>
        <band height="66" splitType="Stretch">
            <image scaleImage="RealHeight">
                <reportElement uuid="e7738ed8-673e-49e9-8c14-060378cb6a79" x="0" y="0" width="263" height="66">
                    <printWhenExpression><![CDATA[$F{name}.equals("Christmas Day")]]></printWhenExpression>
                </reportElement>
                <imageExpression><![CDATA["mc.jpg"]]></imageExpression>
            </image>
            <textField>
                <reportElement uuid="167148f8-ad51-4c87-9faa-27bf18f2160e" x="0" y="0" width="263" height="66" forecolor="#FF6666">
                    <printWhenExpression><![CDATA[!$F{name}.equals("Christmas Day")]]></printWhenExpression>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Forte" size="24" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="ad7c75b4-975b-478e-8a44-090a63c52896" x="263" y="0" width="263" height="66"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Arial" size="24" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

生成的输出

生成的报告将是(通过 iReport 中的预览):

The generated output

And the generated report will be (via preview in iReport):

我可以在报告中做什么以得到这样的结果(数据源也更改为圣诞节):

what can i do in the report to get a result like this(the datasource is also changed to this is an christmas day):

推荐答案

第一个解决方案:textField元素后面的图像

Datasoure

我使用了 csv 数据源( log.csv ):

The first solution: the image behind the textField element

Datasoure

I've used the csv datasource (log.csv):

level,message,image
DEBUG,Some debug message. We are starting,
INFO,Some info. Parameter1 value: 20,info.jpg
WARN,Some warning message. Something going wrong: value is not set,warning.jpg
ERROR,Some error message. The error occurs: file is corrupted,
FATAL,Some fatal message. The disk is full. OutOfMemoryError take place,

报告的设计

我放置了两个 textFields 来显示消息级别和消息.我将 image 元素放置在带有消息级别图标的 textField 下显示图像.

The report's design

I've placed two textFields for showing message level and the message. And I've placed the image element for showing image under the textField with message level's icon.

printWhenExpression 的帮助下,我正在显示消息级别的图标或文本.如果图标是在数据源(字段 image )中定义的,或者文本(字段 level )(如果未定义),则显示为我.

With help of printWhenExpression I'm showing the message level's icon or text. I'm showing the icon if it is defined in datasource (field image) or the text (the field level) if not defined.

imageExpression 是:

<imageExpression><![CDATA["d:\\path_to_icons\\" + $F{image}]]></imageExpression>

我们可以设置图像的完整路径(该路径加上数据源中的文件名).

We can set the full path to the image (the path plus file name from the datasource).

iReport 中的报告设计:

jrxml 文件:

<?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="log_sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a3c5b928-7218-48e8-9de5-7c9f328d3420">
    <import value="com.google.common.base.Strings"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="level" class="java.lang.String"/>
    <field name="message" class="java.lang.String"/>
    <field name="image" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <image scaleImage="RetainShape">
                <reportElement x="61" y="0" width="61" height="20" uuid="cefb0c61-75f7-4b8c-a92c-72bc728ddb0b">
                    <printWhenExpression><![CDATA[!Strings.isNullOrEmpty($F{image})]]></printWhenExpression>
                </reportElement>
                <imageExpression><![CDATA["d:\\path_to_icons\\" + $F{image}]]></imageExpression>
            </image>
            <staticText>
                <reportElement x="0" y="0" width="61" height="20" uuid="2fb8ab6c-9253-4a8d-80ac-08a7c963f13e"/>
                <textElement markup="none"/>
                <text><![CDATA[The level is:]]></text>
            </staticText>
            <textField>
                <reportElement x="61" y="0" width="61" height="20" uuid="9795b7ef-a38c-43d3-8867-f5cd85b602cc">
                    <printWhenExpression><![CDATA[Strings.isNullOrEmpty($F{image})]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{level}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="122" y="0" width="433" height="20" uuid="841e0cfa-1ca5-4e6b-8e12-d5f2ff65a2e2"/>
                <textElement markup="styled"/>
                <textFieldExpression><![CDATA["The message is: <i>" + $F{message} + "</i>"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

生成的输出

生成的报告将是(通过 iReport 中的预览):

我使用了 csv 数据源( text2replace.csv ):

text
This is a row with IMAGE
This is an another row IMAGE at the middle of the sentence
Another row. Just a text

报告的设计

我使用了 Html 元素.

借助 hc:htmlContentExpression 属性,我设置了html代码:我将html标记<img>替换了字符串"IMAGE" 的出现以绘制图像. .

With help of hc:htmlContentExpression attribute I've set the html code: I replaced the occurrences of string "IMAGE" with html tag <img> for drawing images.

hc:htmlContentExpression 是:

<hc:htmlContentExpression><![CDATA["<div style='font-weight: bold; font-style: italic; height: 20px;'>" + $F{text}.replaceAll("IMAGE", "<img style='width: 20px; height: 20px;' src='file:///e:/path_to_file/image.jpg' alt='image'/>") + "</div>"]]></hc:htmlContentExpression>

iReport 中的报告设计:

jrxml 文件:

<?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="replace_text" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5e2b75e0-8b53-41b0-b90a-f36d1022c233">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="text" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="20" uuid="06badd57-cfad-4909-b390-5a0747f4a297"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<div style='font-weight: bold; font-style: italic; height: 20px;'>" + $F{text}.replaceAll("IMAGE", "<img style='width: 20px; height: 20px;' src='file:///e:/path_to_file/image.jpg' alt='image'/>") + "</div>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </detail>
</jasperReport>

生成的输出

生成的报告将是(通过 iReport 中的预览):

注释

Notes

  • 我已经使用了.

  • I've used the method com.google.common.base.Strings.isNullOrEmpty(String) from Google's Guava library for checking if the image set or not. It is import string <import value="com.google.common.base.Strings"/> in my jrxml file.

另一个示例是此处

这篇关于用图片替换一些文本:如何绘制图像而不是句子中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆