用图片替换一些文字 [英] Replace some text with a picture

查看:96
本文介绍了用图片替换一些文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 JasperReports 报告中的图像替换文本(无论在何处找到TEXT,应该用图像替换)。有没有人知道如何做到这一点?

I want to replace a text (wherever it finds a TEXT, it should be replaced with an image) with an image in the JasperReports report. Does anyone have any idea how to do that?

我在网上搜索但找不到任何东西所以请不要评价为负。

I searched over the internet but couldn't find anything so please don't rate me negative.

推荐答案

您可以借助 printWhenExpression 属性来解决此任务。

You can solve this task with help of printWhenExpression attribute.

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

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



报告的设计



我已经放置了两个 textFields 来显示假期的名字和假期的日期。我已经在 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):

注释

Notes

您可以使图像的表达式和使用复杂化,例如获取图像名称的变量(或带路径的名称)。

You can complicate the image's expression and use, for example variable for getting image name (or name with path).

这篇关于用图片替换一些文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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