I have decided to pass this Q/A after receiving both questions in chat and comment on answer on how to handle page number in \"combined\" jasper reports
Whe
There's another easy solution which doesn't get into all those jasper classes like JasperPrint and JasperPages. The approach is to get No. of pages in previous prints and adding it to the PAGE_NUMBER variable of the next print.
Suppose we have three jasper prints. jasperPrint1, jasperPrint2, jasperPrint3 and because jasperPrint1 has dynamic content and variable no. of pages it's hard to know page number for the next prints. Solution for this is -
export the first print to temporary pdf file -
JasperExportManager.exportReportToPdfFile(jasperPrint1,
tempPdfFile.getAbsolutePath());
initialize a variable which keeps track of pages of all the previous prints with 0;
pafeSofar = 0
write a method to return no. of pages in a pdf file:
private int numberOfPages(String filePath) throws IOException {
PdfReader pdfReader = new PdfReader(filePath);
return pdfReader.getNumberOfPages();
}
Here PdfReader is a class from iText. You can add a maven dependency or manually download a .jar and add it to your class path.
Maven Dependency for itext pdf reader is following:
com.itextpdf
itextpdf
5.5.0
use this method to get no. of pages in previous report.
pagesSofar = pagesSofar + numberOfPages(tempPdfFile.getAbsolutePath());`
Create a parameter to pass this variable into and add that parameter to PAGE_NUMBER in other report.
parameterMap.put("previousPageNumber", pagesSofar);`
in your .jrxml file:
`
Here parameterMap is the map of your parameters that you'll pass in each report -
JasperPrint jasperPrint2 = JasperFillManager.fillReport(jasperReport3, parameterMap, new JREmptyDataSource());