How to create watermark in the background band in iReport

Uchenna Nwanyanwu picture Uchenna Nwanyanwu · Dec 29, 2012 · Viewed 34.5k times · Source

I am trying to create a watermark with a text in a report. For e.g. have a text like this is a good report printed horizontally repeatedly at the background of a report. I know this can be done in the background band but don't know how.

UPDATE Your solution worked. I want to implement this in another way. I want to print the same text repeatedly at the background. Let's say i want to print the text welcome to a new year. I will want it to print this way

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

All these will be at the background. Can i achieve something like this without manually creating the text fields to fill the page.

Answer

Alex K picture Alex K · Dec 30, 2012

Yes, you are right. You can use Background band for printing watermark text (or picture). You should set the height for this band - I think it is better to set height of this band the same as the report's height.

For example, for A4 format I have set the 802 pixels for Background's band height. The formula for calculating this number was: the Report's height (842 pixels for A4 format) - the Left Margin's width (20 in my case) - the Right Margin's width (20 in my case).

After setting size for the Background band you can put image or staticText (textField) elements to this (Background) band.

You can rotate the text of staticText (textField) element with help of rotation attribute. The available values of this attribites are:

  • None (no rotation - 0 degrees),
  • Left (rotate 270 degrees),
  • Right (rotate 90 degrees),
  • UpsideDown (rotate 180 degrees).

In the working sample I've used styles for setting the rotation attribute for textField and for one element I've set value for this attribute directly to the textField element:

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

The image element does not support the rotation attribute, but you can create any image you want manually.

Note: in this sample I've used mode="Opaque" for textField and image elements.

The result will be (via iReport preview function): The generated report


For solving your case (several texts as watermark) I've put several textField elements to the Background band. The report's template is:

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

Note: in this sample I've used mode="Transparent" for textField elements.

The report's design (in iReport):

enter image description here

The result will be (via iReport preview function): Generated report in iReport


You can find more information in Watermark across the page in JasperReports post if you want to set custom rotation angle.


UPDATE: You can solve your second issue, for example, with help of StringUtils.repeat(java.lang.String, int) method from Commons Lang API.

You can add import instruction (<import value="org.apache.commons.lang3.StringUtils"/> in my sample) to your report's template and use the expression like this:

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

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

The result will be (via iReport preview function):

Generated report in iReport

Details:

  • In this sample I've used the Commons Lang library v. 3.1. You should add this library to the application's classpath (in my case it was iReport's cp).

iReport classpath

  • I've used the parameter numberOfRepeats (java.lang.Integer) for setting the number of phrases to generate.