我有一个jrxml,通过java我在bean集合中设置了一个List<List<?>>
。现在,我的最终列表有5个列表(也可以超过5个),因此jrxml将所有报告都视为一个报告中的不同报告,而我无法获得该报告的合并页数。该报告显示所有5个报告的第1-5页。
<textField evaluationTime="Master">
<reportElement x="660" y="14" width="58" height="14" forecolor="#1A75B4" uuid="24876562-c6ab-424d-9ac6-769ef9b54079">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
</reportElement>
<textElement textAlignment="Right">
<font fontName="Albany WT" size="10"/>
</textElement>
<textFieldExpression><![CDATA["Page " + $V{MASTER_CURRENT_PAGE}]]></textFieldExpression>
</textField>
<textField evaluationTime="Master">
<reportElement x="725" y="14" width="50" height="14" forecolor="#1A75B4" uuid="5c06c90b-79f2-450b-9f43-7eb00676871b">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
</reportElement>
<textElement textAlignment="Left">
<font fontName="Albany WT" size="10"/>
</textElement>
<textFieldExpression><![CDATA[" of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>
编辑2(添加了Jasper打印代码),此处helperReturnObject是列表列表:
List<JasperPrint> prints = new ArrayList<JasperPrint>();
helperReturnObject.getTemPlatepaths().forEach(t -> {
try
{
int index = helperReturnObject.getTemPlatepaths().indexOf(t);
JasperReport jasperReport = null;
if (!developMentFlag)
{
jasperReport = (JasperReport) JRLoader.loadObject(JasperGatewayClass.class.getResourceAsStream(t));
}
else
{
try
{
jasperReport = (JasperReport) JRLoader.loadObject(new FileInputStream(new File("path")));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(
helperReturnObject.getBeanCollections().get(index));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
helperReturnObject.getParameters().get(index), dataSource);
prints.add(jasperPrint);
}
catch (/*JRException | NullPointerException*/ Exception e)
{
System.out.println(e.getMessage() + "----------------------ERROR----------------");
e.printStackTrace();
}
});
最终编辑修改3(现在可以使用。必须更新代码才能使其适用于页面标题区域):
for (JasperPrint jp : prints)
{
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)
{
System.out.println(jpe.getClass().getTypeName());
// Check if text element
if (jpe instanceof JRTemplatePrintFrame)
{
JRTemplatePrintFrame jpf = (JRTemplatePrintFrame) jpe;
List<JRPrintElement> jpeElements = jpf.getElements();
for (JRPrintElement jpeElement : jpeElements)
{
if (jpeElement instanceof JRTemplatePrintText)
{
JRTemplatePrintText jpt = (JRTemplatePrintText) jpeElement;
// Check if current page marker
if (CURRENT_PAGE_NUMBER.equals(jpt.getValue()))
{
jpt.setText("Page " + currentPage + " of"); // Replace marker
continue;
}
// Check if total page marker
if (TOTAL_PAGE_NUMBER.equals(jpt.getValue()))
{
jpt.setText(" " + totPageNumber); // Replace marker
}
}
}
}
}
currentPage++;
}
}
答案 0 :(得分:0)
如果页码在详细信息栏或页脚内,则代码here将起作用。但是由于我的要求是在页眉中添加页码,所以我不得不更新代码。
for (JasperPrint jp : prints)
{
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)
{
System.out.println(jpe.getClass().getTypeName());
// Check if text element
if (jpe instanceof JRTemplatePrintFrame)
{
JRTemplatePrintFrame jpf = (JRTemplatePrintFrame) jpe;
List<JRPrintElement> jpeElements = jpf.getElements();
for (JRPrintElement jpeElement : jpeElements)
{
if (jpeElement instanceof JRTemplatePrintText)
{
JRTemplatePrintText jpt = (JRTemplatePrintText) jpeElement;
// Check if current page marker
if (CURRENT_PAGE_NUMBER.equals(jpt.getValue()))
{
jpt.setText("Page " + currentPage + " of"); // Replace marker
continue;
}
// Check if total page marker
if (TOTAL_PAGE_NUMBER.equals(jpt.getValue()))
{
jpt.setText(" " + totPageNumber); // Replace marker
}
}
}
}
}
currentPage++;
}
}