JasperReportsfor Java Developers David R. Heffelfi nger $ 44.99 US £ 27.99 UK € 41.99 EU Prices do not include local sales tax or VAT where applicable Packt Publishing Birmingham - Mumbai www.packtpub.com JasperReports for Java Developers JasperReports is the world’s most popular embeddable open-source Java reporting library, providing Java developers with the power to easily create rich print and web reports. This book shows you exactly how to get started, and develop the skills to get the most from JasperReports. The book steers you through each point of report setup, from creating, designing, formatting, and exporting reports with data from a wide range of datasources, to integrating JasperReports with other Java frameworks. What you will learn from this book • What JasperReports is, and what it can do for you • Adding reporting capabilities to your application • Creating and formatting reports • Report layout and design • Working with database datasources and XML • Working with Java object datasources • Adding charts and graphics to a report • Working with crosstabs, subdatasets, and scriptlets • Working with the iReport report designer • Exporting reports to PDF, Excel spreadsheet or Word document format • Integrating JasperReports with other Java frameworks like Spring, JavaServer Faces, Struts, and Hibernate Who this book is written for This book is for Java developers who want to create rich reports for either the Web or print, and want to get started quickly with JasperReports to do this. No knowledge of Jasper Reports is presumed, although familiarity with Java, SQL, and XML is assumed. From T echnologies to Solutions JasperReports for Java Developers Create, Design, Format, and Export Reports with the World’s Most Popular Java Reporting Library David R. Heffelfinger JasperReports for Java Developers Create, Design, Format, and Export Reports with the World's Most Popular Java Reporting Library David R. Heffelnger BIRMINGHAM - MUMBAI JasperReports for Java Developers Create, Design, Format, and Export Reports with the World's Most Popular Java Reporting Library Copyright © 2006 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, Packt Publishing, nor its dealers or 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 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: July 2006 Production Reference: 1180706 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 1-904811-90-6 www.packtpub.com Cover Image by www.visionwt.com Credits Author David R. Heffelnger Reviewers Thomas M. Ose Meenakshi Singh Sarosh Khateeb Development Editor Douglas Paterson Assistant Development Editor Nikhil Bangera Technical Editor Priyanka Baruah Project Manager Patricia Weir Editorial Manager Dipali Chittar Indexer Mithil Kulkarni Proofreader Chris Smith Production Coordinator Manjiri Nadkarni Layouts and Illustrations Shantanu Zagade Cover Designer Manjiri Nadkarni About the Author David R. Heffelnger has been developing software professionally since 1995; he has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including Freddie Mac, Fannie Mae, and the US Department of Defense. He has a Masters degree in Software Engineering from Southern Methodist University. David is editor in chief of Ensode.net (http://www.ensode.net ), a website about Java, Linux, and other technology topics. I would like to thank everyone at Packt Publishing, particularly Douglas Paterson, Patricia Weir, Nikhil Bangera, and Priyanka Baruah, and the technical reviewer, Thomas Ose. This book wouldn't have been a reality without your help. I would especially like to thank my family for their support. A special dedication goes to my wife and daughter. Table of Contents Preface 1 Chapter 1: An Overview of JasperReports 7 Brief History of JasperReports 7 What is JasperReports? 8 The JasperReports Open-Source License 9 Features of JasperReports 9 Flexible Report Layout 9 Multiple Ways to Present Data 10 Multiple Ways to Supply Data 10 Multiple Datasources 10 Watermarks 10 Subreports 11 Exporting Capabilities 11 Class Library Dependencies 13 Typical Workow 13 Where to Get Help? 16 Summary 17 Chapter 2: Adding Reporting Capabilities to Java Applications 19 Downloading JasperReports 19 Setting Up Our Environment 22 JasperReports Class Library 23 Required Libraries for Report Compilation 23 Jakarta Commons 24 Optional Libraries and Tools 25 Apache ANT 25 JDT Compiler 26 JDBC Driver 26 Table of Contents [ ii ] iText 27 JFreeChart 27 Jakarta POI 27 Summary 28 Chapter 3: Creating Your First Report 29 Creating a JRXML Report Template 29 Previewing the XML Report Template 30 Creating a Binary Report Template 33 Compiling a JRXML Template Programmatically 33 Compiling a JRXML Template through ANT 36 Generating the Report 38 Viewing the Report 40 Displaying Reports on a Web Browser 43 Elements of a JRXML Report Template 45 <property> 46 <import> 46 <reportFont> 46 <parameter> 47 <queryString> 47 <eld> 47 <variable> 47 <group> 48 <background> 48 <title> 48 <pageHeader> 49 <columnHeader> 49 <detail> 50 <columnFooter> 50 <pageFooter> 51 <lastPageFooter> 51 <summary> 52 Summary 54 Chapter 4: Creating Dynamic Database Reports 55 Database for Our Reports 55 Generating Database Reports 57 Embedding SQL Queries into a Report Template 57 Generating the Report 60 Modifying a Report Query via Report Parameters 65 Database Reporting via a Datasource 70 Database Report Methods Compared 76 Table of Contents [ iii ] Summary 77 Chapter 5: Working with Other Datasources 79 Empty Datasources 80 Map Datasources 87 Java Objects as Datasources 92 TableModels as Datasources 99 XML as Datasource 105 Custom Datasources 109 Writing a Custom JRDataSource Implementation 110 Using the Custom JRDataSource Implementation 113 Summary 114 Chapter 6: Report Layout and Design 115 Controlling Report-Wide Layout Properties 116 Setting Text Properties 117 Styles 117 Setting Text Style for Individual Report Elements 120 Setting Styles for Text Segments 121 Setting a Report's Background 122 Adding Multiple Columns to a Report 125 A Few Things about Report Columns 128 Grouping Report Data 129 Report Expressions 133 Report Variables 137 Built-In Report Variables 146 Stretching Text Fields to Accommodate Data 146 Laying Out Report Elements 149 Setting the Size and Position of a Report Element 152 Using the <frame> Element 157 Hiding Repeated Values 158 Subreports 162 Summary 168 Chapter 7: Adding Charts and Graphics to Reports 169 Adding Geometrical Shapes to a Report 169 Adding Lines to a Report 169 Adding Rectangles to a Report 171 Adding Ellipses to a Report 172 Table of Contents [ iv ] Adding Images to a Report 173 Attributes of the <image> Element 175 evaluationTime 175 evaluationGroup 175 hAlign 175 vAlign 176 isLazy 176 isUsingCache 176 onErrorType 176 Adding Charts to a Report 177 Attributes of the <chart> Element 177 customizerClass 177 evaluationGroup 177 evaluationTime 177 isShowLegend 178 Chart Customization 178 Chart Datasets 179 Attributes of the <dataset> Element 179 Plotting Charts 180 Attributes of the <plot> Element 180 Pie Charts 181 Bar Charts 184 XY Line Charts 187 Other Types of Charts 190 Summary 190 Chapter 8: Other JasperReports Features 191 Report Localization 191 Scriptlets 194 Crosstabs 200 <columnGroup> 204 <crosstabCell> 204 <crosstabDataset> 204 <crosstabParameter> 204 <measure> 204 <parametersMapExpression> 205 <reportElement> 205 <rowGroup> 205 <whenNoDataCell> 205 Subdatasets 205 Adding Hyperlinks and Anchors to Reports 212 Bookmarks 215 Handling Very Large Reports 218 Table of Contents [ v ] Summary 220 Chapter 9: Exporting to Other Formats 221 Exporting Overview 221 Exporting to PDF 223 Exporting to RTF 225 Exporting to Excel 227 Exporting to HTML 229 Exporting to XML 231 Exporting to CSV 234 Exporting to Plain Text 237 Directing HTML Reports to a Browser 240 Summary 246 Chapter 10: Graphical Report Design with iReport 247 Obtaining iReport 248 Setting Up iReport 249 Creating a Database Report in Record Time 254 Creating a Report "From Scratch" 263 Creating More Elaborate Reports 273 Adding Images to a Report 274 Adding Multiple Columns to a Report 275 Grouping Report Data 278 Adding Charts to a Report 282 Help and Support 288 Summary 288 Chapter 11: Integrating JasperReports with other Frameworks 289 Integrating JasperReports with Hibernate 289 Integrating JasperReports with Spring 303 Integrating JasperReports with JavaServer Faces 309 Integrating JasperReports with Struts 314 Summary 320 Index 321 [...]... at the MySQL User Conference in California JasperSoft sponsors JasperReports' development, allowing Teodor and other JasperSoft developers to work full-time on JasperReports JasperSoft also provides commercial support and services for JasperReports and related products, including the iReport Visual Designer for JasperReports In addition to providing support for JasperReports and iReport, JasperSoft... command-line Java applications to generate reports with JasperReports After all, JasperReports is nothing but a Java class library providing an API to facilitate the ability to generate reports from any kind of Java application JasperReports requires a Java Development Kit (JDK) 1.3 or newer in order to successfully compile applications incorporating the JasperReports Java class library, and a Java Runtime... a third-party library to export JasperReports' reports to the OpenDocument Format (ODF) The OpenDocument Format is a standard XML-based file format specification for office applications developed by the Organization for the Advancement of Structured Information Standards (OASIS) OpenOffice.org version 2.0 uses ODF as its default format The JasperReports OpenDocument Format exporter was developed in... Microsoft's OLE 2 Compound Document format JasperReports takes advantage of Jakarta POI for exporting reports to XLS (Microsoft Excel) format More information about Jakarta POI can be found at http://jakarta.apache.org/poi/ • JAXP: JAXP refers to the Java API for parsing and transforming XML documents It is used by JasperReports to parse XML files and is included in Java SE 5.0 It has to be downloaded... Where to Get Help? JasperReports has official online forums and a mailing list where questions can be asked Both the forums and the mailing list archives can be found at the JasperReports site at http://sourceforge.net/projects /jasperreports The JasperReports website contains tips, tricks, JavaDoc API documentation, and a quick reference for JRXML elements We won't repeat this information in this book,... reports with JasperReports • Where to get support for JasperReports Brief History of JasperReports JasperReports was started by Teodor Danciu, in 2001, when he was faced with the task of evaluating reporting tools for a project he was working on The existing solutions that he found were too expensive for his project's budget Therefore, he decided to write his own reporting tool The project for which he... What is JasperReports? JasperReports is an open-source Java class library designed to aid developers with the task of adding reporting capabilities to Java applications Since it is not a standalone tool, it cannot be installed on its own Instead, it is embedded into Java applications by including its library in the application's CLASSPATH JasperReports is a Java class library, and is not meant for end... or producing charts) JasperReports Class Library JasperReports is distributed as a JAR file that needs to be added to the CLASSPATH of any application for which we wish to add reporting capabilities The JasperReports library requires a Java Runtime Environment (JRE) 1.3 or newer For all JasperReports -related tasks, we need to add the JasperReports library to our CLASSPATH The JasperReports library... address it [] An Overview of JasperReports This chapter presents an overview of JasperReports and explains its capabilities and features Here is a brief outline of the topics covered in this chapter: • A brief history of JasperReports • What JasperReports is, and what it can do for us • A brief discussion of the JasperReports open-source license • The features of JasperReports • JasperReports' class library... downloadable from http://www.sourceforge.net/projects /jasperreports JasperReports has its own requirements for proper and successful functioning: Java Development Kit (JDK) 1.4 or newer (http:/ /java. sun.com/javase/downloads/ index.jsp), ANT 1.6 or newer (http://ant.apache.org/), iReport 1.2 or newer (http://ireport.sourceforge.net/) Any operating system supporting Java can be used (any modern version of Microsoft . Solutions JasperReports for Java Developers Create, Design, Format, and Export Reports with the World’s Most Popular Java Reporting Library David R. Heffelfinger JasperReports for Java Developers. Word document format • Integrating JasperReports with other Java frameworks like Spring, JavaServer Faces, Struts, and Hibernate Who this book is written for This book is for Java developers. - Mumbai www.packtpub.com JasperReports for Java Developers JasperReports is the world’s most popular embeddable open-source Java reporting library, providing Java developers with the power