How to print the grand total only in the last page of a very long report? - jasper-reports

How can I make the total paramater appear only on the last page of a report with lots of concepts.
The report repeats istelf until the list of concepts is displayed in two or more pages. The total appears in all of them because the parameter (received as $PTotal) is on the footer of the page.
I need this to be displayed only in the footer of the last page. It doesnt matter if I leave that space as blank, I just need the parameter to be displayed only at the last page.
This is not a report composed of varios pages, it generates multiple pages until the list fits. Just to clarify.
How can I fix that parameter tag with a printWhenExpression tag?

If you just use the summary-band for this purpose?
.......
<summary>
<band height="146" splitType="Stretch">
<reportElement .../>
<textElement ...>
<font .../>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{total}]]> </textFieldExpression>
</band>
</summary>
</jasperReport>

Copy your footer band and rename it to a lastPageFooter band. Keep the original footer intact, but delete the textField for $P{total} from it. The lastPageFooter band, including your total, will only be printed on the last page of the report.

Try using a PrintWhenExpression of
$V{PAGE_NUMBER} == $V{PAGE_COUNT}

You can try to use "Last Page Footer" band

Related

How to show a variable value in jasper report title?

I am creating a jasper report, want to show the value of a column returned through last record of my query, in the title band. Kindly help to achieve.
To get the last value of your record, if this is what you are after, set the evaluationTime of your textField from the Title Band to Report.
The text field expression, in this case, is going to be evaluated when the end of the report is reached, thus producing the last value from your result set for a particular field.
The JRXML fragment might look like this:
<title>
<band height="100">
<textField evaluationTime="Report">
<reportElement x="72" y="16" width="100" height="24" uuid="698866c8-7d26-4bc7-8727-b4a56d239a53"/>
<textFieldExpression><![CDATA[$F{MyField}]]></textFieldExpression>
</textField>
...
</band>
</title>

I want "continue..." text when sub report will overflow to new page

I think, this should be a feature, when the subreport detail band overflow to the next page, that "...continued" or something should came, but I can't able to solve that issue and I have tried to find out, if there is any solution, but nothing works..
Can any one have a better idea, how to get the solution?
The "normal" way to do is:
In subreport put the text in the detail band (if it is not there already), if text without datasource (just pass a new net.sf.jasperreports.engine.JREmptyDataSource(1) to the subreport so that detail band is displayed one.
Having text in detail band allows us to use the pageFooter band, note
title band and summary band overflows on new page without the
pageFooter. You can also use the summary band but then you need to set attribute isSummaryWithPageHeaderAndFooter="true" on jasperReport tag.
In subreport add the a pageFooterBand with your text
es.
<pageFooter>
<band height="50">
<staticText>
<reportElement x="446" y="18" width="100" height="20" uuid="efa2e741-c546-4261-bdb7-a4b211212f17"/>
<text><![CDATA[...continued]]></text>
</staticText>
</band>
</pageFooter>
This will display the text "...continued" on every subreport page, since we like to avoid it on last page add empty lastPageFooter
<lastPageFooter>
<band height="50"/>
</lastPageFooter>

Jasper Reports Show "Page X of Y" using a single text field

I would like to create one text field that contains Page X of Y, without splitting it in two parts, as per the common solution. My textfield contains "Page " + $V{currentPage} + " of " + $V{PAGE_NUMBER}" with evaluationTime=auto.
Let's say I have a report with 10 pages. Three are the Title Band, six are Detail Band and one is the Summary Band. My results show "Page 0 of 10" for the Title Bands, correct counts for the Detail Bands, then "Page 0 of 10" for the Summary Bands.
How do you ensure the variable is calculated everywhere, not only on Detail Band?
Jaspersoft Studio, 6+
For Jaspersoft Studio v6, or if the first page number is duplicated, try this solution, which uses $V{MASTER_CURRENT_PAGE} and $V{MASTER_TOTAL_PAGE} with an evaluation time of Master.
Jaspersoft Studio
For other versions of Jaspersoft Studio, try the steps outlined in the subsequent subsections.
Create Variable
Create a variable as follows:
Create a variable called V_CURRENT_PAGE_NUMBER
Select the variable to open its properties (illustrated below)
Set Expression to: 1
Set Initial Value Expression to: $V{PAGE_NUMBER}
If the page number shows 0, use $V{PAGE_NUMBER} + 1.
If the page number always shows 1 of Y, set Expression to $V{PAGE_NUMBER} instead of the initial value expression, and leave the initial value expression empty.
Set Reset type to: Page
These settings are illustrated in the following figure:
Setting the Expression to 1 prevents its value from being null. That is, if the footer shows Page null of 4 it probably means that the Expression hasn't been set.
The variable is created.
Add Page Footer
Add a Page Footer band as follows:
Select the report in the outline panel
Check Summary With Page Header And Footer to ensure the page footer appears on the summary page.
Add a Page Footer band.
The footer is added.
Create Text Field
Create a text field as follows:
Drag and drop a single text field onto the Page Footer band.
Select the text field.
Set Expression to: msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
Set Evalutation Time to: Auto
These settings are illustrated in the following figure:
The single text field is created.
Preview Report
For a report with three pages plus a summary page, previewing the report shows:
The summary page shows:
I tried this approach, but ended up with incorrect page numbers: {1/7, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7}.
For JasperReports 6+, use MASTER_CURRENT_PAGE and MASTER_TOTAL_PAGES system variables and remember to set the text field evaluation time to Master:
<textField evaluationTime="Master">
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[msg("Page {0} of {1}", $V{MASTER_CURRENT_PAGE}, $V{MASTER_TOTAL_PAGES})]]></textFieldExpression>
</textField>
See: http://jasperreports.sourceforge.net/sample.reference/book/index.html
The common approach, as you mentioned, uses two separated text fields:
Current page number
$V{PAGE_NUMBER} with EvaluationTime: Now
Total page number
$V{PAGE_NUMBER} with EvaluationTime: Report
For page history show like: Page: 1 of 5
Make the text field value to be like below and change the evaluation time to Master:
"Page: "+$V{MASTER_CURRENT_PAGE}+" of "+$V{MASTER_TOTAL_PAGES}
Source:
<textField evaluationTime="Master">
<reportElement x="224" y="0" width="186" height="15" uuid="6641bb8b-9f48-4832-942b-8b04220030e6">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement verticalAlignment="Top">
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA["Page: "+$V{MASTER_CURRENT_PAGE}+" of "+$V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>
Regarding the current page number, evaluationTime=now and $V{PAGE_NUMBER} is your answer.
Unfortunately I don't think you can achieve what you want because there is a bug in PAGE_COUNT when Split Allowed is on for the Detail band. Otherwise evaluationTime=now and "Page " + $V{PAGE_NUMBER} + " of " + $V{PAGE_COUNT}" would probably work.
This work for my (little different of Dave's Answer)
*Using JasperSoft Studio
Then put a Text field with the expression:
"Pág. " + $V{PAGE_NUMBER} +"/" + $V{V_CURRENT_PAGE_NUMBER}
Hope this help!
This should help, by using evaluationTime as Report
<textField>
<reportElement x="497" y="0" width="32" height="12" forecolor="#7E8083"
uuid="ef663cfd-4058-40bb-a6d9-de7f9a8164be"/> --update your elements here
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif" size="7" pdfFontName="OpenSans-Regular.ttf"/>
</textElement>
<textFieldExpression>
<![CDATA["Page " + $V{PAGE_NUMBER} + " of"]]>
</textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="529" y="0" width="7" height="12" forecolor="#7E8083"
uuid="ef663cfd-4058-40bb-a6d9-de7f9a8164be"/> --update your elements here
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif" size="7" pdfFontName="OpenSans-Regular.ttf"/>
</textElement>
<textFieldExpression>
<![CDATA[$V{PAGE_NUMBER}]]>
</textFieldExpression>
</textField>
If you want to see page no like Page 1 of 5 on right side of the report footer use ireport or jasper report Palette Tools(Page X of Y) is perfect.
If you want to see page no like Page 1 of 5 on left side of the report footer use ireport.
Please follow below steps
Create function variable as like
Please take a TextField
"Page "+$V{V_CURRENT_PAGE_NUMBER}+" of "+ $V{PAGE_NUMBER}

Right-aligned page number information in JasperReports

In JasperReports, I like to render page numbers in the style current-page / total-pages .
Studying the official demos you can find the following solution using three TextFields (because there is no built-in variable
for number of pages )
<!-- Right aligned current page -->
<textField>
<reportElement x="100" width="40" .../>
<textElement textAlignment="Right" ... />
<textFieldExpression class="java.lang.String">
<![CDATA[String.valueOf($V{PAGE_NUMBER})]]>
</textFieldExpression>
</textField>
<!-- Centered aligned slash -->
<staticText>
<reportElement x="140" width="5" .../>
<textElement textAlignment="Center" ... />
<text>
<![CDATA[/]]>
</text>
</staticText>
<!-- Left aligned total number pages (evaluationTime="Reports") -->
<textField evaluationTime="Report">
<reportElement x="145" width="40"/>
<textElement textAlignment="Left" ... />
<textFieldExpression class="java.lang.String">
<![CDATA[String.valueOf($V{PAGE_NUMBER})]]>
</textFieldExpression>
</textField>
However, this only works fine when the complete paging information is centered with respect to the page (with the slash in the middle).
What I like to achieve is to right-align the whole group in a way that the total-pages has a constant distance to the right border.
How to achieve this?
It's a harder problem that it seems at first. The key issue becomes clear when you try to be more precise about this claim, "there is no built-in variable for number of pages". All variables have an evaluation time. So the variable $V{PAGE_NUMBER} really is a built-in variable for the number of pages... but only when it is evaluated at report time.
Therefore your Total Pages field must be evaluated at Report Time.
Likewise, the very same variable $V{PAGE_NUMBER} really is a built-in variable for the current page number... but only when evaluated at Now or Page (or other appropriate time).
Therefore your Current Page field must be evaluated Now or Page.
Therefore these variables must be in different Text Fields so they can be evaluated at different times.
But this conflicts with your requirement. Since you cannot put these into the same text field, you cannot have the right-most item right-justified AND have the item to its left flow perfectly into it.
Depending on your exact situation you might be able to achieve acceptable workarounds. But my guess is that the effort involved in a workaround will be too high. You could imagine, for example, a scriptlet that runs after the report is finished filling. It could parse through the report to find the field "Page 3 of xxx" and replace xxx with the correct total. I'm not sure exactly how this would work; it sounds mostly like bad news. I don't recommend it.
Or maybe you could calculate the total number of pages somehow externally and pass this in to the report as a parameter. It would work if the number of pages depends directly on the number of rows, for example. But this could only work in very specially defined cases. It's a hack.
You should certainly log an enhancement request. I could imagine a special variable that does what you want when placed into a text field which evaluates at the magical time Auto. But for now I don't see any easy way to get what you want.
I've found a solution at http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=68429.
The secret seems to be that you define evalutionTime="Auto" in your text field Page {X} of {Y}.
Here's how it worked for me (again credit to http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=68429)
First define the variable CURRENT_PAGE_NUMBER
Variable class: java.lang.Integer
Calculation: Nothing
Reset type: Page
Variable Expression: $V{PAGE_NUMBER}
This simply copies the page number. (Interestingly the forum post said that one should set Reset type: None which works in case you want to display Page {X} of {Y} in a detail band but does not work if you like me want it to be shown in the page header band.)
After this you should place a text field where you want your Page {X} of {Y} to be – in my case right side of the page header, enter the expression:
msg("Page {0} of {1}", $V{CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})
and set
evaluationTime="Auto"
Since you now have both the current as well as the total number of pages in one text field, you can easily align it in any way you like.

Line is not visible in subreport jasper report?

I am using jasper report to create pdf document. I used one Subreport inside the main document. This Subreport consist of 3 textfield and one line. I use javabean datasource to populate the value. Now I could not see line if the datasource returns null value. Can anyone guide me how to solve this problem.
Below you can see the xml code which I used for drawing line in jrxml
file:
<line direction="TopDown">
<reportElement mode="Transparent" x="-6" y="17" width="480" height="0"
forecolor="#C6C8CA" key="line-1" isPrintInFirstWholeBand="true"/>
<graphicElement stretchType="NoStretch">
<pen lineWidth="0.25" lineStyle="Solid"/>
</graphicElement>
</line>
Is the line in the same Band with the text fields? If yes, then that's you problem. In Jasper if the datasource returns no elements. The band will not be rendered, as simple as that.
If you want the line to always appear put it in a band that is always rendered by default. Like Page_Header, Page_Footer ...etc.
This also happens when 2 items overlap. If your text box is over the line, change line's order by
Order->Bring to Front