如何在iReport中创建背景带中的水印

时间:2012-12-29 15:46:34

标签: jasper-reports ireport

我正在尝试在报告中创建带有文字的水印。对于例如在报告的背景上重复水平打印this is a good report这样的文字。我知道这可以在背景乐队中完成,但不知道如何。

更新 您的解决方案有效我想以另一种方式实现这一点。我想在后台重复打印相同的文本。假设我想要打印文本welcome to a new year。我希望它以这种方式打印

welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
welcome to a new year welcome to a new year welcome to a new year welcome to a new year
...

所有这些都将在后台。我可以在不手动创建文本字段来填充页面的情况下实现这样的目的。

1 个答案:

答案 0 :(得分:15)

是的,你是对的。您可以使用背景波段打印水印文本(或图片)。 你应该为这个乐队设置 height - 我认为最好将此乐队的高度设置为与报告的高度相同。

例如,对于 A4 格式,我为背景的波段高度设置了 802 像素。计算此数字的公式为: 报告的高度( A4 格式的 842 像素) - 左边距宽度( 20 在我的情况下) - 右边距宽度( 20 在我的情况下)

设置背景频段的尺寸后,您可以添加 图像 staticText(textField) 此元素的元素(背景)。

您可以借助旋转属性旋转 staticText(textField) 元素的文本。这些属性的可用值是:

  • (无旋转 - 0度),
  • (旋转270度),
  • (旋转90度),
  • UpsideDown (旋转180度)。

在工作示例中,我使用了 styles 来设置 textField 轮换属性element我已将此属性的值直接设置为 textField 元素:

<?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="watermark_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="Rotated180" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="UpsideDown" fontName="Curlz MT" fontSize="40"/>
    <style name="Rotated270" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="Left" fontName="Curlz MT" fontSize="40"/>
    <style name="Rotated90" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="Right" fontName="Curlz MT" fontSize="40"/>
    <style name="NotRotated" mode="Opaque" forecolor="#FF33CC" hAlign="Center" vAlign="Middle" rotation="None" fontName="Curlz MT" fontSize="40"/>
    <queryString>
        <![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <background>
        <band height="802" splitType="Stretch">
            <image hAlign="Center">
                <reportElement uuid="42c96db8-4afd-4834-ba69-6b211df0c42f" style="Rotated180" mode="Opaque" x="118" y="372" width="318" height="58"/>
                <imageExpression><![CDATA["D:\\java\\jasper_reports\\happy_new_year_h.png"]]></imageExpression>
            </image>
            <staticText>
                <reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="NotRotated" x="106" y="22" width="342" height="63"/>
                <textElement/>
                <text><![CDATA[Happy New Year!]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated270" x="24" y="236" width="93" height="330"/>
                <textElement/>
                <text><![CDATA[Happy New Year!]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated90" x="445" y="236" width="93" height="330"/>
                <textElement/>
                <text><![CDATA[Happy New Year!]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="d7401e33-f1b3-49ca-85d1-2923d82508f6" style="Rotated180" x="106" y="702" width="342" height="63"/>
                <textElement/>
                <text><![CDATA[Happy New Year!]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Opaque" x="79" y="85" width="428" height="139" forecolor="#9999FF"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="Right">
                    <font fontName="Edwardian Script ITC" size="40" isItalic="true"/>
                </textElement>
                <text><![CDATA[Happy New Year!]]></text>
            </staticText>
        </band>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
                <textElement>
                    <font size="14" isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Sample of report with watermark]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Cost]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

图片 元素不支持旋转属性,但您可以手动创建所需的任何图像。

注意 :在此示例中,我使用 mode =“Opaque” 作为 < em> textField 图片 元素。

结果将是(通过 iReport 预览功能): The generated report


为了解决您的情况( 几个 文本作为水印),我已将几个 textField 元素添加到背景乐队。报告的模板是:

<?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="watermark_sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="68"/>
    <queryString>
        <![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <background>
        <band height="802" splitType="Stretch">
            <staticText>
                <reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="112" width="428" height="56" forecolor="#808080"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
                    <font fontName="Serif" size="40" isItalic="true"/>
                </textElement>
                <text><![CDATA[this is a good report]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="272" width="428" height="56" forecolor="#808080"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
                    <font fontName="Serif" size="40" isItalic="true"/>
                </textElement>
                <text><![CDATA[this is a good report]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="432" width="428" height="56" forecolor="#808080"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
                    <font fontName="Serif" size="40" isItalic="true"/>
                </textElement>
                <text><![CDATA[this is a good report]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="3bdd5d67-94f0-457f-8477-b1d0438d7bc9" mode="Transparent" x="63" y="594" width="428" height="56" forecolor="#808080"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None">
                    <font fontName="Serif" size="40" isItalic="true"/>
                </textElement>
                <text><![CDATA[this is a good report]]></text>
            </staticText>
        </band>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
                <textElement>
                    <font size="14" isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Sample of report with watermark]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Cost]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

注意 :在此示例中,我使用了 模式=“透明” < em> textField 元素。

报告的设计(在 iReport 中):

enter image description here

结果将是(通过 iReport 预览功能): Generated report in iReport


如果您想设置自定义旋转角度,可以在Watermark across the page in JasperReports帖子中找到更多信息。


更新 :您可以解决第二个问题,例如,借助StringUtils.repeat(java.lang.String, int)中的Commons Lang API方法。

您可以将 导入 说明(我的示例中为<import value="org.apache.commons.lang3.StringUtils"/>)添加到报告的模板中,并使用如下表达式:

<textFieldExpression><![CDATA[StringUtils.repeat("Welcome to a New Year! ", $P{numberOfRepeats})]]></textFieldExpression>

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="watermark_sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc290dd1-903c-4740-8484-16a2ebe872d2">
    <import value="org.apache.commons.lang3.StringUtils"/>
    <parameter name="numberOfRepeats" class="java.lang.Integer" isForPrompting="false">
        <defaultValueExpression><![CDATA[10]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT ID, NAME, COST FROM PRODUCT ORDER BY NAME]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <background>
        <band height="802" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement uuid="26290245-c5cf-4f28-b637-c5b0253910a5" mode="Transparent" x="21" y="112" width="470" height="56" forecolor="#808080"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
                    <font fontName="Serif" size="20" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA[StringUtils.repeat("Welcome to a New Year! ", $P{numberOfRepeats})]]></textFieldExpression>
            </textField>
        </band>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="6d708618-09e4-433d-ab48-bb9cbd0b832b" x="92" y="29" width="370" height="20"/>
                <textElement>
                    <font size="14" isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Sample of report with watermark]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Cost]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="414160e2-41c4-4326-8ec8-ff7c230ac2c3" mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#000000" backcolor="#3399FF"/>
                <box leftPadding="10" rightPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="100" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="5715c691-0b39-4530-9009-3b7179bb06f5" x="200" y="0" width="100" height="20"/>
                <box leftPadding="10" rightPadding="10">
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

结果将是(通过 iReport 预览功能):

Generated report in iReport

详细信息:

  • 在此示例中,我使用了 Commons Lang library v.3.1 。您应该将此库添加到应用程序的类路径中(在我的情况下,它是 iReport 的cp)。

iReport classpath

  • 我使用参数 numberOfRepeats java.lang.Integer )来设置要生成的词组数。