JasperReports 3.6 Development Cookbook - Siddiqui Bilal - E-Book

JasperReports 3.6 Development Cookbook E-Book

Siddiqui Bilal

0,0
26,26 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail

JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. While such reports are pivotal in managing business information more effectively, creating and customizing them can get tedious.

This book will give you recipes to solve common JasperReports problems to make your life easier. It will take you through complex examples related to JasperReports with step-by-step instructions on how to solve them.

The author's experience in creating reports enables him to share over 50 recipes to develop crystal-clear business reports using the capabilities of JasperReports and the amazing features provided by its visual report designer tool: iReport.

This cookbook will arm you to develop user-friendly reports by using multiple types of data in the same report, thereby saving your time in sorting, arranging, or extracting data and making the whole process effortless. First you will learn to do basic things such as creating, sizing, positioning, and enhancing the titles, headers, footers of the report. You will move on to working with the body and footer of the report, including using parameters to filter records during report processing and implementing nested hierarchy. Second, you will learn to enhance the look and feel of your report by deploying and reusing styles, and by using designs, textual effects, background images, and watermarks. You will also learn to use mathematical and logical expressions such as counting the number of records with a particular field value, grouping records, applying styles on your data based on logical or mathematical conditions, and so on.

Then you will be able to do advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once. You will learn to build a cover page and table of contents for multi-page reports, display multi-dimensional page numbering, and much more. You will be able to design simple and multi-level summary reports, cross-tabs with dynamic rows and columns, create bar charts, and display data trend graphs in your reports. Lastly and very importantly, you will be able to generate reports from Java Swing applications after you learn how to use JavaWrappers for your JasperReport.

Create and enhance your business reports using the various functionalities of JasperReports and iReport

Approach

The entire book is a comprehensive collection of recipes meant to tutor you on making and enhancing reports. Each recipe provides you with easy step-by-step descriptions of the actions necessary to accomplish a specific task with explanations to help you to understand how and why each action was undertaken. Example values and code samples are used throughout the recipes, which makes adaptation for individual needs easy. Each recipe uses a concrete example for easy understanding.

Who this book is for

This book is for Java developers who want to use JasperReports to create user-friendly business reports. It is for those who may be familiar with JasperReports but want to dive into advanced JasperReports activities.

If you know how to use Microsoft Word you can also learn how to design business reports using iReport by following the recipes of this cookbook.

Although the primary audience of this cookbook are report designers, however Java and XML developers will find this book useful in understanding how Jasper's XML code works and how to wrap JasperReports functionality in your Java Swing or Web applications.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 297

Veröffentlichungsjahr: 2010

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

JasperReports 3.6 Development Cookbook
Credits
About the Author
About the Reviewers
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Creating Static and Dynamic Titles and Headers
Introduction
Downloading, installing, and running JasperReports and iReport
How to do it...
There's more...
Download and install iReport for Linux, Mac OS X, and Solaris
Download older versions of iReport for Windows, Linux, Mac OS X, or Solaris
Creating your first "Hello World" report
How to do it...
How it works...
Creating and sizing the title for your report
Getting ready
How to do it...
How it works...
There's more...
Aligning multiple components simultaneously
Using dynamic titles that can change during report processing
Getting ready
How to do it...
How it works...
Inserting a company logo in the title of your report
Getting ready
How to do it...
How it works...
Adding a simple header to your report
Getting ready
How to do it...
How it works...
There's more...
Setting margins for your report and aligning the report header relative to report margins
Getting ready
How to do it...
How it works...
2. Working with the Body and Footer of your Report
Introduction
Displaying a field along with its label in the body of your report and handling null values
Getting ready
How to do it...
How it works...
Creating a simple table of records along with labels for each column
Getting ready
How to do it...
How it works...
Inserting a heading for a group of records
Getting ready
How to do it...
How it works...
Using parameters to filter records during report processing
Getting ready
How to do it...
How it works...
Implementing groups within groups — a nested hierarchy
Getting ready
How to do it...
How it works...
Adding a simple footer to your report
How to do it...
How it works...
Displaying general information or summary at the end of your report
Getting ready
How to do it...
How it works...
3. Enhancing the Look and Feel of your Report
Introduction
Deploying and reusing styles in your report
Getting ready
How to do it...
How it works...
Setting background color for data
Getting ready
How to do it...
How it works...
Using HTML tags and bullet lists
Getting ready
How to do it...
How it works...
Expanding a field vertically to accommodate large text
Getting ready
How to do it...
How it works...
Applying formatting pattern to the value of a data field
Getting ready
How to do it...
Using background images and watermarks in your report
Getting ready
How to do it...
4. Working with a Variety of Data Sources
Introduction
Creating a report from relational data
Getting ready
How to do it...
There's more...
Connecting to an XML datasource
Getting ready
How to do it...
Creating a report from XML data using XPath
Getting ready
How to do it...
How it works...
Using multiple relational databases to generate a report
Getting ready
How to do it...
How it works...
Creating a report from model beans of Java applications
Getting ready
How to do it...
How it works...
5. Multi-page Reports
Introduction
Building a cover page for your multi-page report
Getting ready
How to do it...
How it works...
Creating a simple, one-page TOC for your report
Getting ready
How to do it...
How it works...
Applying a style to your simple TOC
Getting ready
How to do it...
How it works...
Resetting page numbering with the start of a particular record
Getting ready
How to do it...
How it works...
Implementing complex multi-dimensional page numbering
Getting ready
How to do it...
How it works...
Showing multiple types of data in the same report
Getting ready
How to do it...
How it works...
Managing pagination of multiple types of data in a report
Getting ready
How to do it...
How it works...
6. Multi-column Reports
Introduction
Dividing the body of a report into multiple columns
Getting ready
How to do it...
How it works...
Displaying groups of data in separate columns
Getting ready
How to do it...
Displaying data as name-value pairs in multiple columns
Getting ready
How to do it...
Filling your report horizontally in multiple columns
Getting ready
How to do it...
How it works...
Using subreports to design a multi-column report
Getting ready
How to do it...
How it works...
7. Summary Report, Crosstabs, and Graphs
Introduction
Designing a simple summary report
Getting ready
How to do it...
How it works...
Designing a multi-level summary report
Getting ready
How to do it...
How it works...
Designing a crosstab—a table with dynamic rows and columns
Getting ready
How to do it...
How it works...
Displaying data trends as a graph in your report
Getting ready
How to do it...
How it works...
There's more...
Embedding a bar graph inside a tabular view
Getting ready
How to do it...
8. Java Wrappers for your JasperReports
Introduction
Creating a Java wrapper for your report
Getting ready
How to do it...
How it works...
Compiling and viewing your report in a Java Swing application
Getting ready
How to do it...
How it works...
Printing the hardcopy of your report using a Java Swing application
Getting ready
How to do it...
Creating an Excel report from a Java Swing application
Getting ready
How to do it...
Creating a JasperReport on the fly in a Java web application
Getting ready
How to do it...
How it works...
There's more...
Index

JasperReports 3.6 Development Cookbook

JasperReports 3.6 Development Cookbook

Copyright © 2010 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: June 2010

Production Reference: 1220610

Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.

ISBN 978-1-849510-76-9

www.packtpub.com

Cover Image by Vinayak Chittar (<[email protected]> )

Credits

Author

Bilal Siddiqui

Reviewers

Nadeem Ghafoor Chaudhry

Sagara Gunathunga

Lloyd H. Meinholz

Bhavani P. Polimetla

Acquisition Editor

Dilip Venkatesh

Development Editor

Mayuri Kokate

Technical Editors

Ishita Dhabalia

Vinodhan Nair

Indexer

Monica Ajmera Mehta

Editorial Team Leader

Gagandeep Singh

Project Team Leader

Priya Mukherji

Project Coordinator

Zainab Bagasrawala

Proofreader

Sandra Hopper

Graphics

Geetanjali Sawant

Production Coordinators

Aparna Bhagat

Arvindkumar Gupta

Cover Work

Aparna Bhagat

About the Author

Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the founder of XML4Java.com, a company focused on simplifying e-business. After graduating in 1995 with a degree in electronics engineering from the University of Engineering and Technology, Lahore, he began designing software solutions for industrial control systems. Later, he turned to XML and used his programming experience in C++ to build web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. Bilal is a technology evangelist and a frequently published technical author.

Bilal has been focusing exclusively on Java and XML-based open source tools and solutions since 2006. He has extensively used popular open source products such as JasperReports, ADempiere, Openbravo, and Eclipse. Bilal is a strong advocate of open source tools and is engaged not only in designing solutions based on open source tools but also collaborating with local universities in Lahore to train software and IT personnel in using open source technologies.

Special thanks to my wife's patience, which was often put to the test while I was working on this book. I'm also thankful for the support of Rainer Maier—my friend and business partner, who was quite convinced that I should just work on this book and sleep instead of working on our mutual projects. Finally, I am also thankful to my colleagues, Tariq and Zia, for their ideas to make this book more interesting and useful.

About the Reviewers

Nadeem Ghafoor Chaudhry received his B.S. and M.S. in Computer Science from the University of Massachusetts, Lowell, U.S.A. He worked in the software industry in the U.S. for about four years. Then he switched to academia in Pakistan and taught both undergraduate and graduate courses at different institutes of higher learning in Pakistan. Currently, he is engaged as Assistant Professor in the department of Computer Science of COMSATS Institute of Information Technology, Lahore, Pakistan.

COMSATS Institute of Information Technology (CIIT) is a renowned institute for higher learning in Pakistan. Currently, the CIIT is offering 47 different degree programs divided into 20 undergraduate and 27 graduate programs in the fields of Information and Communication Technology, Management Sciences, Electrical Engineering, Chemical Engineering, Mathematics, Physics, Bio-sciences, Development Studies, Environmental Science, Meteorology, and Architecture and Design. The present student strength is around 20,000 with faculty corpus of more than 1,600. The CIIT has already produced approximately 10,000 graduates.

Sagara Gunathunga holds a special degree in Computer Science (B.Sc.) from the University of Peradeniya, Sri Lanka and a degree in Information Technology (BIT) from the University of Colombo, Sri Lanka. He is an Apache Axis PMC member, an Apache committer for several projects that include Apache Web Service and Apache Woden. His research interest focuses on SOA, web services, distributed systems, and modularity systems.

He also has a sound industry experience in J2EE-related technologies, including ORM tools, dependency injection frameworks, and reporting. Currently, he is playing a Tech-Lead role in a U.S.-based software services company named Aeturnum Inc.

Sagara also maintains his blog at http://ssagara.blogspot.com/, and you can get in touch with him at<[email protected]>.

All my thanks go to my mother for supporting me.

Lloyd H. Meinholz works as an IT professional in the Washington D.C. area. He has more than 20 years of experience in developing software in a variety of programming languages and production environments. Lloyd has focused on Java, Linux, and other open source technologies for the past 12 years. He has been employed by start-up companies as well as Fortune 500 companies and worked in both the public and private sectors. Lloyd has found JasperReports and iReport to be a great reporting solution for many of his employers.

Bhavani P. Polimetla has been learning and working in the IT Industry since 1990. He graduated as a Bachelor of Computer Science and Master of Computer Applications from Andhra University, India. He worked on standalone Swing applications to Grid computing and N-tire architecture. He has worked with the world's top clients including three from Fortune 50 companies. At present, he is working as an independent Java consultant in Atlanta, Georgia, U.S.A.

To demonstrate his skills, he completed 25+ certifications in the spectrum of J2EE, database, and project management subjects. He also achieved many awards for many of his projects. He reads and writes poetry in his free time. More information is available at his website www.polimetla.com.

Preface

JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. While such reports are pivotal in managing business information more effectively, creating and customizing them can get tedious.

This book will give you recipes to solve common JasperReports problems to make your life easier. It will take you through complex examples related to JasperReports with step-by-step instructions on how to solve them.

The author's experience in creating reports enables him to share over 50 recipes to develop crystal-clear business reports using the capabilities of JasperReports and the amazing features provided by its visual report designer tool: iReport.

Create and enhance your business reports using the various functionalities of JasperReports and iReport.

What this book covers

Chapter 1, Creating Static and Dynamic Titles and Headers, focuses on basic things such as creating, sizing, positioning, and enhancing the titles and headers of the report.

Chapter 2, Working with the Body and Footer of your Report, covers working with the body and footer of the report, including using parameters to filter records during report processing and implementing nested hierarchy.

Chapter 3, Enhancing the Look and Feel of your Report, shows you how to enhance the look and feel of your report by deploying and reusing styles, and by using designs, textual effects, background images, and watermarks.

Chapter 4, Working with a Variety of Data Sources, focuses on advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once.

Chapter 5, Multi-page Reports, teaches you to build a cover page and table of contents for multi-page reports, conditional reset page numbering, display multi-dimensional page numbering, design multi-page reports using sub-reports, create separate header and footer for each sub-report in your Master report, and much more.

Chapter 6, Multi-column Reports, covers topics such as dividing the body of a report in multiple columns, grouping records with common value in a report column, filling data in report columns horizontally and vertically, inserting column break in your report, controlling the size of each column independently, and using sub-reports to design multi-column reports.

Chapter 7, Summary Report, Crosstabs, and Graphs, covers designing simple and multi-level summary reports, cross-tabs with dynamic rows and columns, creating bar charts, embedding a bar inside a tabular view, and displaying data trend graphs in your reports.

Chapter 8, Java Wrappers for your JasperReports, covers generating a report from a Java Swing application, generating a report from your web application, printing a hard copy of a report on the fly from within your Java code, viewing your report as a PDF, or a web page, or an XLS sheet, and so on.

Chapter 9, Using Mathematical and Logical Expressions, covers mathematical and logical expressions such as counting the number of records with a particular field value, grouping records, applying styles to your data based on logical or mathematical conditions, and so on.

Note

Chapter 9, Using Mathematical and Logical Expressions, is not present in the book but is available as a free download from: http://www.packtpub.com/sites/default/files/downloads/0769os-chapter-9.zip

What you need for this book

The following software products are used in this JasperReports 3.6 Development Cookbook:

JasperReports version 3.6.0iReport version 3.6.0PostgreSQL version 8.4Apache Tomcat version 6.0.16Java Development Kit (JDK) version 1.6.0_12

Who this book is for

This book is for Java developers who want to use JasperReports to create user-friendly business reports. It is for those who may be familiar with JasperReports but want to dive into advanced JasperReports activities.

If you know how to use Microsoft Word, you can also learn how to design business reports using iReport by following the recipes of this cookbook.

Although the primary audiences of this cookbook are report designers, Java and XML developers will find this book useful in understanding how Jasper's XML code works and how to wrap JasperReports functionality in your Java Swing or web applications.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: Open the Pattern.jrxml file from the Task5 folder in the source code for this chapter.

A block of code is set as follows:

<staticText> <reportElement x="107" y="10" width="340" height="33"/> <textElement> <font fontName="Verdana" size="24"/> </textElement> <text><![CDATA[Monthly Customer Invoices]]></text> </staticText>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<jasperReport <title> <band height="94" splitType="Stretch"> <!-- other JasperReports XML tags --> <image> <reportElement x="365" y="10" width="180" height="80"/> <imageExpression class="java.lang.String"> <![CDATA["X:\\Images\\packt.png"]]> </imageExpression> </image> </band> </title> <!-- other JasperReports XML tags --> </jasperReport>

Any command-line input or output is written as follows:

uncompress iReport-nb-xx.tar.gz

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Select Currency from the Category column and click the OK button."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an email to<[email protected]>, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail<[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Note

Downloading the example code for the book

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration, and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to any list of existing errata. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or web site name immediately so that we can pursue a remedy.

Please contact us at<[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at<[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Creating Static and Dynamic Titles and Headers

In this chapter, you will learn:

Downloading, installing, and running JasperReports and iReportCreating your first "Hello World" reportCreating and sizing the title for your reportUsing dynamic titles, which can change during report processingInserting company logo in the title of your reportAdding a simple header to your reportSetting margins of your report and aligning the report header relative to report margins

Introduction

This chapter is about the titles and headers of your reports.

Imagine you are looking for a particular recipe in a cookbook. Perhaps the quickest way of doing this is to go through the table of contents, which is just a list of the titles of all the recipes contained in this cookbook. So it is important that each title says what the recipe teaches.

Similarly, the title of your report will help identify the report among other reports. The title perhaps decides the objective of designing a report.

A title normally appears on just the first page of your report. The header appears on every page. While a title helps to identify the report among other reports, a header distinguishes the report among other copies of the same report. You can understand this with an example.

Consider you are designing a report showing the data for all invoices issued for a particular customer during a specific month. "Monthly Customer Invoices" can be an appropriate title for this report. Once you have designed the monthly customer invoices report, you will periodically generate copies of this report for a specific customer and for a specific month (that is, each copy of the report will be specific for these two parameters). Therefore, it is a good idea to include these two parameters in the header of your report.

This means that a report header represents the data on which a report is generated.

Downloading, installing, and running JasperReports and iReport

iReport is a powerful visual tool for JasperReports to graphically design reports. It is much easier to generate JASPER code using the graphical features of iReport as compared to writing the JASPER code by hand.

In order to create reports using JasperReports, you will only need to download iReport from the iReport project website hosted at SourceForge.net ( http://sourceforge.net/projects/ireport ).

As the JasperReports library comes bundled with iReport, you don't need to separately download and install JasperReports.

This recipe just shows you how to download, install, and run iReport.

How to do it...

Visit the iReport project website at http://sourceforge.net/projects/ireport. The iReport project homepage at SourceForge.net will open.Click on the View all files button, and a list of all available versions of iReport will appear. I used version 3.6.0 of iReport, which is the latest at the time of writing these recipes. Therefore, expand iReport-3.6.0, by clicking on it. It will show all the files related to iReport-3.6.0. Download the file named iReport-nb-3.6.0-windows-installer.exe. This is an executable file.Double-click the Windows executable file. A setup wizard will run, which will show you the installation steps and ask you for the folder where you want to install iReport. Just provide the destination folder and the installation will finish.After the installation is finished, iReport will run and show a welcome window. Click on File and select New from the File menu. A New file dialog will appear containing several icons such as Report, Style, Chart Theme, and so on. Each icon represents a type of file you can create. The Report icon comes pre-selected, which means iReport allows you to create reports by default.Now your iReport is up and running and you are ready to try the recipes of this cookbook.

There's more...

The five steps given above show you how to download and install iReport for Microsoft Windows. iReport also supports other operating systems such as Linux, Mac OS X, and Solaris. The following is a short guideline to download and install iReport for these operating systems.

Download and install iReport for Linux, Mac OS X, and Solaris

You can download iReport for Linux, Mac OS X, or Solaris by following the steps listed below:

Visit the sourceforget.net website at http://sourceforge.net/projects/ireport. The iReport project homepage at SourceForge.net will open.Click the View all files button, to the right of the Download Now! button. A page will open containing the latest downloads for all operating systems in the Newest Files section of the download page. You will find that separate downloads of iReport are available for Linux and Solaris as tar.gz files and for Mac OS X as DMG files. Download iReport for your operating system.For Linux and Solaris, you just need to unzip the iReport-nb-XX.tar.gz files to your required folder. If you are using Linux, open the Linux Terminal and type the following command in the terminal:
tar -zxf iReport-nb-xx.tar.gz

Note

Visit http://www.linuxhelp.net/newbies/#unzip to learn more about unzipping tar.gz files.

If you are using Solaris, you will extract the package files with the uncompress command, as shown below:
uncompress iReport-nb-xx.tar.gz
After you finish extracting the archive, run the ireport.exe file from the iReport/bin folder.

Download older versions of iReport for Windows, Linux, Mac OS X, or Solaris

If you need to download an older version of iReport, click on the View all files button to the right of the Download Now! button on the iReport project home page. A page containing a list of available versions will open. You will find the latest downloads for all operating systems in the Newest Files section and older versions in the All Files section of the download page.

Note that I have used iReport version 3.6.0 in this cookbook. iReport versions dated before 2008 are referred to as iReport classic, which might behave differently from the recipes of this cookbook.

Creating your first "Hello World" report

This recipe is a quick starter for someone who is using iReport for the first time. It shows you what views of your report the tool offers and what type of report components are available. This recipe also teaches you how to quickly make a simple "Hello World" report.

How to do it...

The following ten steps will show you how to make your first "Hello World" report.

Run iReport. Click on File and select New from the File menu. A New file dialog will appear, which contains several icons such as Report, Style, Chart Theme, and so on. Each icon represents a type of file you can create. The Report icon comes pre-selected, which means iReport allows you to create reports by default.Click on the Report icon on the top-left corner of the New file dialog. You will see a number of report templates available to start building your report.Select the Blank A4 report template and click the Open this Template button at the bottom. Blank A4 is the simplest of all templates and, therefore, is a good starting point to learn iReport.Next, you will be prompted to enter a filename for your report. Specify a name of your choice. I call my first report ASimpleBlankA4Report, as shown next. Click the Next button.The Finish dialog will appear saying that you have successfully created your new report. Click on Finish to dismiss it.iReport will open your blank report in its default view, which shows three tabs: Designer, XML, and Preview. The Designer tab shows the various sections of your report such as Title, Page Header, Column Header, and so on, as shown in the following screenshot:You will see a Palette of alphabetically arranged components (for example, a break, a chart, a static text, or a text field component) on the right of the main iReport window, as shown next. Drag a Static Text component from the Palette and drop it into the Title section of your report. Similarly drag-and-drop another Static Text component into the Page Header section of your report.Double-click on the Static Text component of the Title section and type Hello World in it as shown:Click on the XML tab, which shows the JasperReports XML (JRXML) code that iReport authors in response to your designer actions in the Designer tab. You will see that XML code for JasperReports changes every time you drag-and-drop a component from the palette into the designer view of your report.Finally, switch to the Preview tab, which shows what your "Hello World" report will actually look like:

How it works...

JasperReports has defined its own XML-based markup language called JasperReports XML, or JRXML for short. JRXML code contains all the information that is required to specify everything about your report. Therefore, designing a report using JasperReports means you have to perform the following steps:

Write JRXML code manually according to the requirements of your report.Parse and compile the JRXML code to generate a JASPER file for your report.Combine your JASPER files with application data to generate your report view. This process is called filling.Export your report view to a popular format such as PDF or XLS.

You normally need to write Java code to compile, fill, and export your JasperReport.

iReport is a JRXML authoring, compiling, filling, and exporting tool. This means that if you can author JRXML manually and write Java code to perform these tasks (compiling, filling, and exporting), you don't need iReport.

However, it is really troublesome trying to write JRXML manually. The graphical tools and components in iReport make it very easy and quick to author the JRXML according to the design requirements of your reports. iReport performs the following tasks for you to help you generate your report:

As a visual authoring tool, it helps you to generate JRXML by just dragging-and-dropping of components. It actually translates your visual actions to JRXML code.It compiles the JRXML to generate a JASPER report file.It also combines JASPER files with application data to generate a print preview for your screen and printer.Then it may export this report preview to a popular format such as PDF or XLS.

iReport provides three integrated views to make your work easier: the designer view (where you design your report graphically), the preview view (where you can see the effects of your design), and the XML view (where you can view and edit the actual XML of a JRXML file).

To see the XML view of iReport, switch to the XML tab and you will find the following JRXML code for the Blank A4 report:

<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <background><band splitType="Stretch"/></background> <title> <band height="79" splitType="Stretch"/> </title> <pageHeader> <band height="35" splitType="Stretch"/> </pageHeader> <columnHeader> <band height="61" splitType="Stretch"/> </columnHeader> <detail> <band height="125" splitType="Stretch"/> </detail> <columnFooter> <band height="45" splitType="Stretch"/> </columnFooter> <pageFooter> <band height="54" splitType="Stretch"/> </pageFooter> <summary> <band height="42" splitType="Stretch"/> </summary> </jasperReport>

You can see from this code that there is a JRXML tag for every section of your report. For example, there is a<title> tag which wraps the title of your report. Similarly, you can see a<pageHeader> tag, which wraps the header.

Each JRXML tag for a section has a<band> child, which specifies two things about the section: the height of the section through a height attribute and the splitting behavior of the section.

In steps 8, 9, and 10 of this recipe, you played a little with the Designer, XML, and Preview tabs, respectively. You will need these tabs in almost every report you prepare using iReport.

The Designer tab is perhaps where you will work most of the time. It shows a window in which you will design your report by using different components such as static text, text field, and so on. These components are available in a palette of components on the right of your main iReport window.

In step 7 of this recipe, you played with the palette of components to quickly have an idea of how components are used in your report. The Palette window is split into two sections named Report Elements and Tools. The former contains several graphical components (for example, Static Text, Text Field, Frame, and so on), whereas the latter contains tools such as Current date and Page number. You will learn how to use these tools in Chapter 2,Working with the Body and Footer of your Report.

Creating and sizing the title for your report

A self-explanatory title for your report helps identify the report among other reports. A popular style of designing self-explanatory titles is to have a main title and a subtitle. The main title is normally brief and the subtitle is a more elaborate form of the main title.

In this recipe, you will learn how to provide a main title and a subtitle for your business report. We are going to create, size, position, and color the title.

Getting ready

There is no database connection required in this recipe.

If you have not made any database connection so far in your iReport installation, you will see an Empty datasource selected in a drop-down list just below the main iReport menu. This is the requirement to run this recipe. If any other option (for example, some database connection) is previously selected, then change it to Empty datasource from the drop-down list, as follows:

How to do it...

the following steps are very simple, and demonstrate how you can design a basic title for your report:

Open the ASimpleTitle.jrxml file from the Task3 folder of the source code for this chapter. The Designer tab of iReport will show a blank report in the Designer tab of its main window. You can see various sections of your report. The first section at the top of the report is Title, which is a placeholder for the title of your report.Now you will insert a title into your report. A palette of components is available on the right of the main iReport window. Select the Static Text component from the palette and drag it into the Title section in the Designer tab.Double-click and edit the Static Text component to provide a title for your report (for example "Monthly Customer Invoices").You will see that some of the title text will disappear from view. This is because your title is bigger than the standard rectangular boundary of the static text component, which contains the title. So, enlarge the rectangle boundary of this component by dragging one of its corners.Now you will give your title a prominent look by increasing its font size. Select the title; the properties of the title field will appear in the Properties window just below the palette of components. Find the Size property among the Text properties and set it to 24.Similarly, find the Font property and set it to Verdana, as shown next:Your report title will get a bold prominent look, as shown here:You can see from the previous image that the title is not well positioned within the Title section. In order to position the title, right-click on the main title, and a pop-up menu will appear; select Position, and another pop up will appear; select Center Horizontally. This will center your title horizontally in the Title section.Next, you insert a subtitle for your report by dragging another static text component from the palette into the Title section of your report and repeating steps 2, 3, and 4. I have provided a self-explanatory subtitle "All invoices for a specific customer, in a specific period of time and for a specific product."Now, repeat step 5 for the subtitle and choose 13 as the font size.Switch to the Preview tab, which shows what your report looks like.You have successfully designed the title of your report. Save it by selecting Save from the File menu.

How it works...

Switch to the XML tab and you will see the following JRXML code:

<?xml version="1.0" encoding="UTF-8"?> <jasperReport> <!-- other JasperReports XML tags --> <title> <band height="79" splitType="Stretch"> <staticText> <reportElement x="107" y="10" width="340" height="33"/> <textElement> <font fontName="Verdana" size="24"/> </textElement> <text><![CDATA[Monthly Customer Invoices]]></text> </staticText> <staticText> <reportElement x="24" y="48" width="512" height="21"/> <textElement> <font size="13"/> </textElement> <text> <![CDATA[All invoices for a specific customer, in a specific period of time and for a specific product]]> </text> </staticText> </band> </title> <!-- other JasperReports XML tags --> </jasperReport>

I have shown only the<title> tag in the code and omitted other JRXML tags that are not relevant to this discussion.

The<title>