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

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}

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>

How to relocate summaryband in jasperstudio

I am appending the contentn of my table of content like here: Append an In-Report ToC
Now I have my TOC at the summary band. How can I move it to the second page? Is there any better way to create table of content in jasperstudio?
As recommended in coments i used jasper book. The problem is in the table of content i get dots instead of real text which should be there.
but as you can seen in the picture by hovering the mouse on the dots the real text is shown although without page number!
Here is the XML generated with jasperstudio:
<textField evaluationTime="Group" evaluationGroup="id" hyperlinkType="Reference" bookmarkLevel="2">
<reportElement isPrintRepeatedValues="false" x="-4" y="37" width="534" height="20" printWhenGroupChanges="id" uuid="f6d9376f-7106-4292-b491-7229b297ce04"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA[$F{id}.toString()}]]></textFieldExpression>
<anchorNameExpression><![CDATA[$F{id}.toString()]]></anchorNameExpression>
<hyperlinkReferenceExpression><![CDATA[$F{id}.toString()]]></hyperlinkReferenceExpression>
<hyperlinkTooltipExpression><![CDATA[$F{id}.toString()]]></hyperlinkTooltipExpression>
</textField>

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.

Make lines with iReport?

How can I do this with iReport (using iReport v4.0)?
Here is jrxml file
it depend on the size of Sub-report, check the sub-repot preview, if you want to increase the size of sub-rport in main report , for this you have to increase the size of sub-reportas where it is separately created.
You could try using background band for those lines.
Maybe inside your parent report
When you create a sub-report you can delete; Page Header, Column Header and Page Footer bands on your main report. After then, you can drag and drop sub-report icon on Summary part of your main report. You will probably use ColumnHeader and Detail bands on your Sub-Report. (It depends on your design. If you use subreports recursively, you can use again summary band.) Later,you will select Remove Report Margins on your subreport. To get expected result; your subreport icon Width on main report should equal your subreport page width.
Adding borders worked great for me. Expand your text element to cell size, right click -> Padding and Borders and add them all around. It's easier than having to handle draw lines. Less elements and faster editing.
You can add lines via the iReport Designer gui interface using the Palette (go to Window > Palette, then click Line, then drag it over to the Designer).
You can also add lines by modifying the jrxml code directly. I haven't found guidance in the documentation on this. But this answer to another question shows how to create a vertical line:
<line>
<reportElement x="0" y="0" width="1" height="30"/>
</line>
And it also shows how to create a horizontal line:
<line>
<reportElement x="0" y="0" width="30" height="1"/>
</line>
Also, the JRXML Sources and Jasper Files section of the documentation has some more involved code such as the following, which uses a direction parameter with value BottomUp (there are also references to TopDown in the community site or here):
<line direction="BottomUp">
<reportElement key="line" x="1" y="4" width="554" height="1"/>
</line>
And this, which uses positionType parameter of FixRelativeToBottom:
<line>
<reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555"
height="1"/>
</line>

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

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