JasperReports allows full flexibility of managing and interlinking of pagination for data spanning multiple subreports. The subreports may contain different types of data and may serve as building blocks for a single master report.
In this recipe you will learn how to make complex pagination for multiple types of data reports and interlinking pagination of each datatype using built-in features of JasperReports.
This recipe also teaches you how to use data-specific and general pagination in parallel with each other. Data-specific pagination is contributed by each building block (subreport), while the general pagination is contributed by the master report.
Refer to the installPostgreSQL.txt
file included in the source code download of this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code of this chapter also includes two files named copySampleDataIntoPGS.txt
and copyCustomerHistorySampletDataIntoPGS.txt
. The copySampleDataIntoPGS.txt
file will help you create a database named jasperdb6
and create a table named CustomerInvoices
with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data for this recipe. Similarly, the copyCustomerHistorySampletDataIntoPGS.txt
file will help you create a database named jasperdb6a
and create a table named CustomerHistory
with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data.
You will be using three JRXML files MultipleData.jrxml, CustHistorySubreport.jrxml
and ProductInvoices.jrxml
in this recipe. You will find these files in the Task6
folder of the source code download of this chapter. The MultipleData.jrxml
file is the master report, which uses the other two files as subreports. The master report has to refer to all its subreports using a complete path (you cannot use relative paths). This means you have to copy the three JRXML files to the C:JasperReportsCookBookSamples
folder in your PC. I have hard-coded this complete path in the master report (MultipleData.jrxml).
MultipleData.jrxml
file from the C:JasperReportsCookBookSamples
folder in your PC. The Designer tab of iReport shows a report with data in the Title, Customer Group Header 1, Detail1, and Detail2 sections, as shown in the following screenshot: CustHistorySubreport.jrxml
will open in a new tab. Drag-and-drop the Page number component from the Palette into the Page Footer section of the CustHistorySubreport.jrxml
subreport. A Text field with the expression $V{PAGE_NUMBER}
will appear in the Page Footer section of the CustHistorySubreport.jrxml
report. 80
. This will increase available space in the Page Footer section. $V{PAGE_NUMBER}
in the Page Footer section by dragging its left edge to the left-side and the right edge to the right side. Double-click the Text field and its expression will be selected. Press the Home key on your keyboard and type"Page +
. Then press the End key on your keyboard and type +" of + $F{CustomerName}
. After typing, click anywhere outside the Text field. The expression will become"Page "+ $V{PAGE_NUMBER} + of + $F{CustomerName}
. java.lang.String
. CustHistorySubreport.jrxml
subreport. Switch to the Preview tab of the master report, and you will see Page1 of Alice appearing at the bottom, as shown below. Check all the other pages as well. You will notice that only the first page of each customer is showing pagination; the subsequent pages don't carry any pagination. ProductInvoices.jrxml
will open in a new tab. In the Report Inspector, select the Page Footer section and change the Band height property in the Page Footer-Properties section to 80
. This will increase available space in the Page Footer section. ProductInvoices.jrxml
report. A Text Field with the expression $V{PAGE_NUMBER}
will appear in the Page Footer section of ProductInvoices.jrxml
report, as shown below. $V{PAGE_NUMBER}
in the Page Footer section by dragging its left edge to the left side and its right edge to the right side. Double-click the Text Field and the expression will be selected. Press the Home key on your keyboard and type"Page " + (
. Then press the End key on your keyboard and type + 1 ) + " of " + $F{CustomerName}
. After typing, click anywhere outside the Text Field. The expression will become"Page "+ ($V{PAGE_NUMBER}+1) + " of " + $F{CustomerName}
, as shown below: java.lang.String
. ProductInvoices.jrxml
subreport. Switch to the Preview tab of the master report, and you will see Page 1 of Alice is appearing at the bottom of page 1 and Page 2 of Alice is appearing at the bottom of page 2, as shown in the following screenshot. Check all the other pages as well. You will notice that each page of each customer is showing the correct pagination."Page "+$V{PAGE_NUMBER}+" of"
and" " + $V{PAGE_NUMBER}
respectively will appear in the Page Footer section.In step 3 of the recipe you inserted a page break after CustHistorySubreport.jrxml
subreport. Due to this page break, CustHistorySubreport.jrxml
subreport, which is a single-page report, will display its data on a separate page. The ProductInvoices.jrxml
subreport will fill multiple pages for each customer.
If each subreport contributes its natural pagination to the master report, the master will have Page 1 at two places (that is, the first page of each subreport). You corrected this problem by incrementing to the page numbers of the ProductInvoices.jrxml
subreport in step 15 of this recipe by using the expression ($V{PAGE_NUMBER}+1).
13.58.44.229