4

我正在尝试在报告中创建带有文本的水印。例如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
...

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

4

1 回答 1

19

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

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

设置背景带的大小后,您可以将图像静态文本(textField)元素放入此(背景)带。

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

  • (无旋转 - 0 度),
  • 向左(旋转 270 度),
  • (旋转 90 度),
  • 颠倒(旋转 180 度)。

在工作示例中,我使用样式设置textField的旋转属性,对于一个元素,我将此属性的值直接设置为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>

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

注意:在这个示例中,我对textField图像元素使用了mode="Opaque" 。

结果将是(通过iReport预览功能): 生成的报告


为了解决您的问题(几个文本作为水印),我将几个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>

注意:在这个示例中,我对textField元素使用了mode="Transparent" 。

报告的设计(在iReport中):

在此处输入图像描述

结果将是(通过iReport预览功能): 在 iReport 中生成的报告


如果您想设置自定义旋转角度,您可以在 JasperReports 帖子的页面上的 Watermark中找到更多信息。


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

您可以将导入指令(<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预览功能):

在 iReport 中生成的报告

细节:

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

iReport 类路径

  • 我使用参数numberOfRepeats ( java.lang.Integer ) 来设置要生成的短语数。
于 2012-12-30T14:25:15.837 回答