Gedalya Gedalya - 5 months ago 42
Java Question

Blank Pages After Subreport Using JasperReports

I built a report template; which uses a base report for headers and footers, and then a subreport into which a report can be injected. In general this works fine; however I have noted that there are blank pages after the subreport has finished generating in the master report.

It occurs to me that this is simply caused by the fact that the master report template and the subreport are not on the same page when it comes to pagination; for example the subreport I am testing uses its own datasource and has grouping.

Is there a way to cut off the extraneous pages? My master report template has the following property set (to no avail):

When No Data Type: No Pages


The subreport has the same setting.

I found one helpful post here:
JasperReports: How to remove new page blank in subreport

This suggested adding the following parameter to the
reportElement
tag for the actual
subreport
:

isRemoveLineWhenBlank="true"


Doing this actually removed all but one extra blank page; which is a huge success. But still, one blank page left.

This is what the source looked like after the change:

<subreport overflowType="Stretch">
<reportElement x="-20" y="0" width="792" height="20" isRemoveLineWhenBlank="true" uuid="e6d4ec67-6ce6-4bbb-b40b-edfb1cfb4722">


Due to the general nature of the problem; especially when articulating the question in the title, I am finding it hard to find relevant articles on the subject. If there are any relevant pages/blog posts/articles, the info would be most appreciated.

Answer

If you generate reports with Java you can delete last empty page as workaround.

JasperPrint jp = JasperFillManager.fillReport(report, params, dataSource);
for (Iterator<JRPrintPage> iterator = jp.getPages().iterator(); iterator.hasNext();) {
     JRPrintPage page = iterator.next();
     if (!iterator.hasNext() && page.getElements().isEmpty()) {
          iterator.remove();
     }
 }