Tommy
Tommy

Reputation: 1985

Can JasperReport insert some specified images/content to a particular page number?

Let say i want to print a image on page 2.

If page 1 has a lot of content, page 1 content will split to page 1 and page 3,

while page 2 is still that image.

Can that be done in pdf generation ?

Thanks in advance.

Upvotes: 1

Views: 2103

Answers (1)

Alex K
Alex K

Reputation: 22867

Yes, you can use printWhenExpression property of image element.

The working sample - I add condition to show image on 2nd page only. My report design in iReport is: Report design in iReport

The jrxml file 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="image_expression" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <queryString>
        <![CDATA[select id from address]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <title>
        <band height="47" splitType="Stretch">
            <image scaleImage="RetainShape">
                <reportElement x="247" y="0" width="32" height="33"/>
                <imageExpression><![CDATA["Number1.png"]]></imageExpression>
            </image>
            <staticText>
                <reportElement x="147" y="13" width="100" height="20"/>
                <textElement textAlignment="Center">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Title]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="59" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <image scaleImage="RetainShape">
                <reportElement x="59" y="0" width="32" height="33">
                    <printWhenExpression><![CDATA[$V{PAGE_NUMBER}==2]]></printWhenExpression>
                </reportElement>
                <imageExpression><![CDATA["Number2.png"]]></imageExpression>
            </image>
        </band>
    </detail>
    <pageFooter>
        <band height="34">
            <image scaleImage="RetainShape">
                <reportElement x="523" y="1" width="32" height="33">
                    <printWhenExpression><![CDATA[$V{PAGE_NUMBER}==2]]></printWhenExpression>
                </reportElement>
                <imageExpression><![CDATA["Number3.png"]]></imageExpression>
            </image>
            <textField>
                <reportElement x="44" y="14" width="80" height="20"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="124" y="14" width="40" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>

The result will be (via preview function in iReport), first page of report will be: The first page

and the second page:

The second page

and the last third page:

The third page

UPDATE:

You can add variable for counting row's number at the page and use this variable in printWhenExpression.

For example, if I want to show image only on the second page at the third row my template will be like this:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    ...
    <variable name="rowAtPage" class="java.lang.Integer" resetType="Page">
        <variableExpression><![CDATA[$V{rowAtPage} + 1]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    ...
    <detail>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="59" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <image scaleImage="RetainShape">
                <reportElement x="59" y="0" width="32" height="33">
                    <printWhenExpression><![CDATA[$V{PAGE_NUMBER} == 2 && $V{rowAtPage} == 3]]></printWhenExpression>
                </reportElement>
                <imageExpression><![CDATA["Number2.png"]]></imageExpression>
            </image>
        </band>
    </detail>
    ...
</jasperReport>

The result will be:

The second page, show image for only one row

Upvotes: 3

Related Questions