Getting the total number of pages in a report

0

Hello community,

 

I'm looking for a way to evaluate the total number of pages in a Jasper Report. I.e. NOT to render but to process it (!).

I know it is possible to render the total number of pages using the variable $V{PAGE_NUMBER} in a text field with evaluation time "Report". This isn't what I need - I need really to get the value of the varaible $V{PAGE_NUMBER}, evaluated within the "Report" scope, and use this value further (e.g. in expressions of elements placed in the page footer).

Is this possible with JasperReports?

 

Thanks & regards,

Peter

 

peter_tonev's picture
Joined: Jun 24 2013 - 4:45am
Last seen: 6 years 1 week ago

1 Answer:

1

Hi,

It all depends on what you actually want to do with the total number of pages. You can get that value only inside text field expression, because in JR, this is a feature of text field elements.

We call this "late fill" capability and is controlled using the evaluationTime property of text field elements.

So if you want to leverage the total number of pages to conditionally display some values in page footer, that should be OK.

But if you want to use it in variables calculations and things that have the potential of changing the total page number itself, that is not possible.

 

Imagine you use the total number of pages to display or not a subreport. Well, if that would be possible (is not) it means the the appearence of the subreport could affect the already calculated page total, which does not make sense.

 

II hope this helps.
Teodor

 

teodord's picture
4420
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 13 hours ago

Hi Teodor,

thanks a lot for your reply. I feared it would seem this way.

I think I'd better describe what we exactly are trying to do.

We have reports which are embedded in our web application using the REST API. Primary usage of our app is on iPads, where the available system ressources are limited, so our app have to be very performant.
Some of our reports can get very long (for our limitations) - about 20-30 pages, and then the app page starts to flutter upon actions like swiping, scrolling a.s.o. Along with this, the app gets very slow.
As a workaround we decided to introduce pagination in our reports for we noticed that the fluttering issue doesn't take effect when having only one-page reports.
We don't use the Jasper page-scrolling buttons for two reasons: as said, we deploy the REST interface and fetch the reports as HTML content using the XHTML exporter (so the JasperServer page scrolling buttons simply aren't there); and then we want the embedded Jasper reports to look like the rest of our application, so we use customisable elements like HTML elements, images or text.

Now, the idea was that our reports offer hyperlink-based buttons << previous | next >>. On click the << next >> button increments the REST parameter "page" by 1.
I.e. the hyperlink defined on the "next" button is a REST request for the same report with a certain page, and I attach to the url the string
"&page=".concat($V{varNextPage})
where varNextPage is defined as
($V{PAGE_NUMBER} + 1).toString()
A varPrevoiusPage would then be accordingly.
($V{PAGE_NUMBER} - 1).toString()
Now, in order not to get out-of-index error, we needed to define what happens if you're already on the first or the last page and try to go further.

The <<previous>> -button was easier, for the first page has obviously the number 1, so I used for the varPrevoiusPage the expression:
(
$V{PAGE_NUMBER} - 1 < 1
? "1"
: ($V{PAGE_NUMBER} - 1).toString()

)
I.d. if you're on the first page already and you click "previuos", you get the first page once again. (What is also possible would be to supess the <<previuos>>-button when on first page.

The hard part comes with the <<next>>-button when you're already on the last page. Our issue is how to tell if you're on the last page. Supposed there was a variable called e.g. TotalPageCount which gives us this value, we could define our varNextPage like
(
$V{PAGE_NUMBER} + 1 > TotalPageCount
? $V{PAGE_NUMBER}
: ($V{PAGE_NUMBER} + 1).toString()

)
Then on click on the <<next>>-button as you are already on the last page, you'd just stay there (or you could supress the "next"-button based on the expression $V{PAGE_NUMBER} == TotalPageCount).

I know such features from Crystal Reports, where you could define formula like (PageNumber = TotalPageCount), or user the Next function to get the next record, NextIsNull function to test it the next record value is NULL, all this together with functions for delayed evaluation like whilePrintingRecords, evaluateAfter a.s.o.

Can you propose some solution or workaround for our requirement as described above - it doesn't have to work this way we try, but rather have the same (paging) functionality within the HTML-exported report.
What would you do in our place?

Many thanks in advance
Peter

peter_tonev - 6 years 1 month ago
Feedback
randomness