Replace some text with a picture: how to draw the

2019-08-02 17:28发布

Datasoure

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

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

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.

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

The report's design in iReport:

enter image description here

The report's template

The jrxml file:

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

The generated output

And the generated report will be (via preview in iReport): enter image description here


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

enter image description here

1条回答
Deceive 欺骗
2楼-- · 2019-08-02 18:32

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,

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.

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.

The imageExpression is:

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

The report's design in iReport:

enter image description here

The report's template

The jrxml file:

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

The generated output

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

enter image description here

The second solution: using Html element

Datasoure

I've used the csv datasource (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

The report's design

I've used the Html element.

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.

The hc:htmlContentExpression is:

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

The report's design in iReport:

enter image description here

The report's template

The jrxml file:

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

The generated output

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

enter image description here


Notes

查看更多
登录 后发表回答