Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

jasper reports - How to create watermark in the background band in iReport

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...