The jasper-report way would be to not combine reports in java but to use
a main report and include the different reports as subreports in this main
report, moving the page number to the main report.
However, if we like to combine in java as in questions, we can also use java to edit and correct the page numbers.
This idea is to set "markers" in report and then post-process the JasperPrint
, replacing the markers with the actual page number.
Example
jrxml (without shame using freemaker style)
<textField>
<reportElement x="435" y="30" width="80" height="20" uuid="14bad2ac-3a56-4cf4-b4dd-310b8fcd2120"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["${CURRENT_PAGE_NUMBER}"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="515" y="30" width="40" height="20" uuid="06567ba6-6243-43e9-9813-f6593528524c"/>
<textFieldExpression><![CDATA["${TOTAL_PAGE_NUMBER}"]]></textFieldExpression>
</textField>
java
Define my markers
private static final String CURRENT_PAGE_NUMBER = "${CURRENT_PAGE_NUMBER}";
private static final String TOTAL_PAGE_NUMBER = "${TOTAL_PAGE_NUMBER}";
after the fillReport
of my reports replace all my markers with the true numbers
//First loop on all reports to get totale page number
int totPageNumber=0;
for (JasperPrint jp : jasperPrintList) {
totPageNumber += jp.getPages().size();
}
//Second loop all reports to replace our markers with current and total number
int currentPage = 1;
for (JasperPrint jp : jasperPrintList) {
List<JRPrintPage> pages = jp.getPages();
//Loop all pages of report
for (JRPrintPage jpp : pages) {
List<JRPrintElement> elements = jpp.getElements();
//Loop all elements on page
for (JRPrintElement jpe : elements) {
//Check if text element
if (jpe instanceof JRPrintText){
JRPrintText jpt = (JRPrintText) jpe;
//Check if current page marker
if (CURRENT_PAGE_NUMBER.equals(jpt.getValue())){
jpt.setText("Page " + currentPage + " of"); //Replace marker
continue;
}
//Check if totale page marker
if (TOTAL_PAGE_NUMBER.equals(jpt.getValue())){
jpt.setText(" " + totPageNumber); //Replace marker
}
}
}
currentPage++;
}
}
Note: If it was code in one of my projects I would not nest this quantity of for and if statements, but extract code to different method's, but for clarity of post I have decided to post it all as one code block
Now its ready for the export
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
....
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…