jasper-report方法将不是在Java中合并报告,而是使用主报告,并将不同的报告作为子报告包含在此主报告中,从而将页码移至主报告中。
但是,如果我们希望像问题中那样结合使用Java,则也可以使用Java编辑和更正页码。
这个想法是在报表中设置“标记”,然后对进行后处理,然后
JasperPrint用实际的页码替换标记。
例
jrxml (使用Freemaker样式时不要感到羞耻)
<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
定义我的标记
private static final String CURRENT_PAGE_NUMBER = "${CURRENT_PAGE_NUMBER}";private static final String TOTAL_PAGE_NUMBER = "${TOTAL_PAGE_NUMBER}";在
fillReport我的报告之后,用真实数字替换所有标记
//First loop on all reports to get totale page numberint totPageNumber=0;for (JasperPrint jp : jasperPrintList) { totPageNumber += jp.getPages().size();}//Second loop all reports to replace our markers with current and total numberint 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++; }}注意:
如果这是我项目中的代码,则我不会嵌套大量的for和if语句,而是将代码提取到不同的方法中,但是为了清楚起见,我决定将所有内容都作为一个代码块发布
现在准备出口
JRPdfExporter exporter = new JRPdfExporter();exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));....



