Thursday, May 10, 2007

An easy way to remove blank page generated by JasperReports

If your JasperReports report has one more sub-reports and it was generated as PDF file. In some circumstances, a blank page might be found within PDF file. Here is my solution to easily remove it.

private void removeBlankPage(List<JRPrintPage> pages) {

for (Iterator<JRPrintPage> i=pages.iterator(); i.hasNext();) {
JRPrintPage page = i.next();
if (page.getElements().size() == 0)
i.remove();
}
}

This method should be called before you flush your JasperPrint instance to PDF.

Followed with the sample code, it would be better to know the object hierarchy of JasperPrint.

  1. 1. A JasperPrint has one or more JRPrintPage, you can get it via getPages() method of JasperPrint. It returns a List of JRPrintPage. If you have three elements, then your printer will print 3 pages.
  2. 2. A JRPrintPage has one ore more JRPrintElement, each element might be a string of text, or a picture, or a rectangle, etc. You can change its position or content dynamically, or even add new JRPrintElement into JRPrintPage.

8 comments:

Unknown said...

you are my hero for today, thank you so very much

Oskar said...

it works perfectly!!!

Gerrit Griebel said...

Worked, but didn't help in the end as my "empty" pages are not actually totally empty, they have just a page header and footer. And the problem is that page numbers appear in the footer: so page 5 is followed by page 7, when 6 was removed.

Gerrit Griebel said...

Fixed my problem the right way: there was some space below content in one band which I removed and so that this spacing is not wrapped to next page and causing an empty page.

Amruth C K said...

Can you please let me know where exactly i should call this function (removeblankpages()) to avoid a empty page?

i checked in Template & JAR also but there is no JasperPrint Function .

johny kumar said...

Thanks sir it was great solution . i try hundred way to solve my problem . this is one help ful ...
thanks alot..

Mr.lin said...

it's work perfectly! so much thank you :)

Unknown said...

Hello! How to use it? Where is place to put this code?