Page count of multiple reports

Hello,

We are using JasperReports 3.7.1 and have three reports, which are already compiled. They will be merged to one PDF File with JasperExportManager.

In the PDF File it says:

  • First report: 1 to 3 pages
  • Second report: 1 to 10 pages
  • Third report: 1 to 5 pages.

The requirement is that in the PDF is one numberation, in this case 1 to 18 pages. How can you count the pages although there are three seperate reports?

 

Thanks in advance,

jrcate

 

jrcate's picture
68
Joined: Aug 19 2011 - 2:31am
Last seen: 12 years 1 month ago

4 Answers:

Hi,

At fill time you could pass the last page number from the previous report as parameter to the next report, and then evaluate the current page number using this parameter in your calculations.

HTH,

sanda

shertage's picture
22320
Joined: Sep 26 2006 - 8:06pm
Last seen: 2 months 3 weeks ago
Thanks for your answer!

It would be awesome, if you can give me an example on this scenario. (pseudo code)

Thanks,
jrcate
jrcate's picture
68
Joined: Aug 19 2011 - 2:31am
Last seen: 12 years 1 month ago

Hi,

This is a simplified example with 2 reports, Report1 and Report2:

  • in the Report2.jrxml declare a report parameter:

<parameter name="initialPageValue" class="java.lang.Integer"/>

  • in the Report2.jrxml use a modified expression for the textfield containing the page number :

<textFieldExpression class="java.lang.String"><![CDATA["Page " + ($P{initialPageValue} + $V{PAGE_NUMBER})]]></textFieldExpression>

  • at report filling time, see the code area below.

HTH,

sanda

Code:
Map parameters1 = new HashMap();
// set your parameters here
 
JasperPrint jPrint1 = JasperFillManager.fillReport(           
  "Report1.jasper",
  parameters1,
  <your_JRDataSource_here>
);
 
Map parameters2 = new HashMap();
// set your parameters here
 
parameters2.put("initialPageValue", jPrint1.getPages().size());
 
JasperPrint jPrint2 = JasperFillManager.fillReport(           
  "Report2.jasper",
  parameters2,
  <your_JRDataSource_here>
);
 
// process the reports further
 
</td></tr></tbody></table>
shertage's picture
22320
Joined: Sep 26 2006 - 8:06pm
Last seen: 2 months 3 weeks ago

With @shertage's solution alone we cannot number the pages "1 of 5", because report 1 does not know how many pages the second report will have. You get

1 of 3
2 of 3
3 of 3
4 of 5
5 of 5

My solution to this was to fill the first report twice:

  1. Build report 1, page count 3 in this example. You have to use the real data source to know how many pages it will be. Throw away the JasperPrint that results.
  2. Build report 2, initial page count 3, adds pages 4 of 5 and 5 of 5.
  3. Build report 1 again, passing in initial page count of 3 + report 2's page count of 2  = total page count 5.
fireslinger's picture
Joined: Sep 13 2018 - 3:00pm
Last seen: 5 years 1 week ago
Feedback