2

I need to enable the reports with 3 types of the page headers: "first page header", "middle pages header" and "last page header". All of these headers can have different number of report elements and on different positions. Same goes for the footers.

enter image description here

NOTE: The report must support the report elements like jr:table or jr:list in its Detail band. These element must be supplied with the data via xml datasources.

I was able to simulate first page header by using Title band, but I'm having problems enabling "middle pages header" and "last page header". This is what I'm trying to do:

<pageHeader>
<band height="100" splitType="Stretch">
    <frame>
        <reportElement x="0" y="0" height="100" width="555" uuid="f788c74b-6853-4bc7-8ed7-5e3d538287c9" isRemoveLineWhenBlank="true">
            <printWhenExpression>
                <![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
            </printWhenExpression>
        </reportElement>
        <staticText>
            <reportElement x="1" y="1" width="100" height="20" uuid="9777208b-5323-4045-aa49-a849d5c00c89"/>
            <text><![CDATA[LAST PAGE HEADER TEXT 1]]></text>
        </staticText>
        <staticText>
            <reportElement x="50" y="10" width="200" height="50" uuid="15313676-09e6-4d17-ac75-2df99f61bfee"/>
            <text><![CDATA[LAST PAGE HEADER TEXT 2]]></text>
        </staticText>
    </frame>
    <frame>
        <reportElement x="0" y="0" height="100" width="555" uuid="65577abd-8717-477a-f27e-c70e9eba46af" isRemoveLineWhenBlank="true">
            <printWhenExpression>
                <![CDATA[new Boolean($V{PAGE_NUMBER} != 1) && new Boolean($V{PAGE_NUMBER} != $V{PAGE_COUNT})]]>
            </printWhenExpression>
        </reportElement>
        <staticText>
            <reportElement x="70" y="0" width="300" height="100" uuid="0a866eb3-85cf-4376-d6a4-21b534d36df0"/>
            <text><![CDATA[MIDDLE PAGE HEADER TEXT 1]]></text>
        </staticText>
    </frame>
</band>
</pageHeader>

I'm using frame blocks along with printWhenExpression. Depending on the printWhenExpression condition, only one of two frame blocks should be printed (with all of its content). First frame block should render "last page header", and second one should render "middle pages header".

The problem lies within the first frame ("last page header") printWhenExpression:

<printWhenExpression>
    <![CDATA[new Boolean($V{PAGE_NUMBER} == $V{PAGE_COUNT})]]>
</printWhenExpression>

In this case, $V{PAGE_NUMBER} always equals $V{PAGE_COUNT} because of the evaluation time.

Is there any way to check if the current page is the last page? Also, is there any other way to enable "middle pages" and "last page" headers?

NOTE: I was able to implement similar logic for the footers by using the frames and Last Page Footer band!

6
  • Hi @PetterFriberg. I'm trying to implement your solution, but currentPage variable returns null on all pages except the first one.
    – mrm
    Dec 9, 2015 at 21:19
  • I'm using "Jaspersoft iReport Designer 5.6.0" with "JDK 1.7.0_79".
    – mrm
    Dec 9, 2015 at 21:25
  • If you copy the variable expression in your jrxml and the textField in your pageHeader (setting position if its out of band), you should see the different text's on the different page groups... I have tried it in iReport Designer 5.6.0 and exporting to pdf with jasper report 6. Dec 9, 2015 at 21:39
  • I have done everything you suggested but still without luck. Here is the jasper template that I'm using along with the xml datasource.
    – mrm
    Dec 9, 2015 at 21:44
  • Let us continue this discussion in chat. Dec 9, 2015 at 21:48

1 Answer 1

4

First of all: $V{PAGE_COUNT} is

the number of records that were processed when generating the current page. Hence the number of records you have in the page, not a page number count...

In pageFooter and lastPageFooter the reportElement can have negative Y coordinates (if IDE does not allow this then you need to edit manually the jrxml) es. y="-700", which allows you to put information in pageHeader and last pageHeader...

FIRST PAGE

in frame use:

<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()==1)]]></printWhenExpression>
  • pageHeader use title band or put frame in pageFooter and use negative y coordinate.
  • pageFooter, use pageFooter band


MIDDLE PAGE

on frame use:

<printWhenExpression><![CDATA[new Boolean($V{PAGE_NUMBER}.intValue()>1)]]></printWhenExpression>
  • pageHeader put frame in pageFooter and use negative y coordinate.
  • pageFooter, use pageFooter band.


LAST PAGE

  • pageHeader, put frame in lastPageFooter and use negative y coordinate.
  • pageFooter, use lastPageFooter band


NOTE: To generate space for this virtual pageHeader include an empty pageHeader with desired band height

Have Fun!

3
  • Since I need to support lists or tables in the reports, it seams that the workaround with negative Y coordinates on Last page footer is the only solution. Thank you @Petter, as always very precise answer.
    – mrm
    Dec 10, 2015 at 8:48
  • You shouldn't have removed your workaround with custom currentPage variable, it can be useful for someone.
    – mrm
    Dec 10, 2015 at 8:54
  • @mrm I think this is cleaner from jasper report point of view... (even if its not nice in IDE).. the currentPage was 1/2 hack and as so it did not work with components... (if someone likes to find it they can check revision history.... ; ) Dec 10, 2015 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.