iReports trick for page numbering including last page

By | October 11th, 2011|RationalPlan|Comments Off on iReports trick for page numbering including last page

When designing some reports which were expanding on multiple pages the need of numbering the pages occured.  The first thing to do was to search the internet and see how others managed this problem. The first idea that popped out was to use the Page X of Y tool from iReports located in the Palette view. It was a very easy and straight forward approach because there was no need to implement additional functionality, just a simple drag and drop to the Page Footer band and some additional tweaks just to look god.

However this approach proved to be a bad one when the page number grew to a 2 digit number and the two text fields overlapped and lost their alignment. Even more, on the last page of the report the page number was not shown. To properly solve this problem the RationalPlan team found a new way to show the page number on each page including the last one.

For showing the page number on the last page of the report the Last Page Footer band must be added by right clicking on it in iReports in the left panel called Report Inspector. After having both bands active (Page Footer and Last Page Footer) drag and drop the Page X of Y tool from the Palette view to the Page Footer band. Resize each text field to look like in the picture and align the text to right respectively to the left in each text field. Now the expression of each text item must be slightly changed by right clicking the text items and writing “Page ” + $V{PAGE_NUMBER} in the first one and ” of ” + $V{PAGE_NUMBER} in the second one. Make sure that the first text item has the evaluation time set to Now and the second one to Report. This setting is located in the Settings view in the Text field properties category.

The last thing to do is to copy the first set of the text items and paste it over the Last Page Footer band and align them accordingly and this is it. Now the text will appear as it should be regardless of the number of pages and the last page will be also numbered. Good luck!

JasperReports tutorial for JAVA using JasperViewer to preview reports

By | September 5th, 2011|RationalPlan|Comments Off on JasperReports tutorial for JAVA using JasperViewer to preview reports

JasperViewer it is a very useful component which allows you to preview the reports from your application before saving them  as documents. It also offers additional  functionality like printing the reports, saving them with different extensions (ODT, HTML, DOC, PDF and others), navigating through out the report’s pages and zooming in or out.  This was the perfect component for fulfilling the customers request here at RationalPlan because it was very easy and straightforward to use. The tricky part and also what I consider a drawback is that if you want to customize its appearance and functionality you can not do it.

First let us try using the viewer as it is without any customizations. The class that describes the viewer is JasperViewer. Looking at the API you can see that the constructors can receive three types of components: an InputStream, a JasperViewer or a String that describes the source file. Because we use a custom data source (click to see the tutorial) to fill our reports it was very simply for us to use the already generated JasperPrint object and pass it to the JasperViewer constructor.

[sourcecode language=”java”]
JasperPrint print = JasperFillManager.fillReport(projectCalendarStream, hm, mainReportDS);
JReportsViewer jReportsViewer = new JReportsViewer(print);
jReportsViewer.viewReport(); [/sourcecode]

In the first line of code we created the JasperPrint component with the JasperFillManager and then we pass it to the constructor in order to create the report. The last line just shows up the JasperViewer dialog.

The next step is to create a customized version of the JasperViewer component to fulfill our needs. Let’s say we want to allow the users to save the reports only as a PDF document. Here is the source code for JasperReports.

[sourcecode language=”java”]
public JReportsViewer(JasperPrint jasperPrint, boolean isExitOnClose, Locale locale) {
if (locale != null) {
setLocale(locale);
}
this.isExitOnClose = false;

initComponents();

this.viewer = new JRViewer(jasperPrint, locale);
List<JRSaveContributor> newSaveContributors = new LinkedList<JRSaveContributor>();
JRSaveContributor[] saveContributors = this.viewer.getSaveContributors();
for (int i = 0; i < saveContributors.length; i++) {
if (saveContributors[i] instanceof JRPdfSaveContributor) {
newSaveContributors.add(saveContributors[i]);
}
}
this.viewer.setSaveContributors(newSaveContributors.toArray(new JRSaveContributor[0]));
this.pnlMain.add(this.viewer, BorderLayout.CENTER);
}[/sourcecode]

This is one of the constructors that we kept because we use a JasperPrint object. We have deleted the other ones because we don’t use source files or InputStreams. As you can see inside this constructor we have a list of JRSaveContributors. These contributors represent the save extensions and as already mentioned we only want to keep the PDF format. To achieve this we look for the JRPdfSaveContributor and when we find it we add it to our list of contributors. Then we set this list as the new list of contributors.

This is a minor customization but if you want other complex ones you might need to take the source code, apply your changes, create the jar and then replace the original JasperReports jar with your own.

Hope this short tutorial helps. Good luck!

JasperReports and iReports tutorial for JAVA using subreports with different datasources

By | December 8th, 2010|RationalPlan|Comments Off on JasperReports and iReports tutorial for JAVA using subreports with different datasources

Here at RationalPlan we are working on a way to generate reports. We chose JasperReports for Java and iReports as the graphical interface for a visual creation of the report layout. It is a daunting task because there are no tutorials which describe in detail how to create a master report with many subreports each  with their different datasource and if you want to read the documentation you have to pay for it. After accomplishing what we proposed we thought that is a good idea to share our knowledge and experience.

1. Creating the main report layout

First let’s start by opening iReport and creating the main report as shown in the figure (in this tutorial there were used the latest versions of  iReport(3.7.6) and JasperReports(3.7.6)).

main report layout

If you don’t know how to make the main report to look like this search about iReports tutorial on different search engines or post here and we shall try to help you as much as we can. After finishing it press the “Preview” button and if there are no errors in the iReport console then you can compile your report by clicking “Compile report” icon. A new file with the .jasper extension will be created in the same folder as the .jrxml file.

2. Creating the main report custom datasource

After successfully completing the first step you must create the custom datasource in your java application. (more…)