Table Of ContentJ
a
s
p
e
r
JasperReports
R
e
for Java Developers
p
o
r
t
JasperReports is the world’s most popular embeddable open-source Java reporting library, s
providing Java developers with the power to easily create rich print and web reports. f
o
This book shows you exactly how to get started, and develop the skills to get the most
r
from JasperReports. J
a
v
The book steers you through each point of report setup, from creating, designing, formatting, a
and exporting reports with data from a wide range of datasources, to integrating JasperReports D
with other Java frameworks. e
v
e
What you will learn from this book lo
p
• What JasperReports is, and what it can do for you
e
• Adding reporting capabilities to your application r
s
• Creating and formatting reports
• Report layout and design
F r o m T e c h n o l o g i e s t o S o l u t i o n s
• Working with database datasources and XML
• Working with Java object datasources
• Adding charts and graphics to a report
• Working with crosstabs, subdatasets, and scriptlets JasperReports
• Working with the iReport report designer D
a
• Exporting reports to PDF, Excel spreadsheet or Word document format v
i
• Integrating JasperReports with other Java frameworks like Spring, JavaServer Faces, d
Struts, and Hibernate R
. for Java Developers
H
Who this book is written for e
f
f
This book is for Java developers who want to create rich reports for either the Web or print, and e
l
want to get started quickly with JasperReports to do this. No knowledge of Jasper Reports is fi
n
presumed, although familiarity with Java, SQL, and XML is assumed. g
e
r
Create, Design, Format, and Export Reports with the World’s
Most Popular Java Reporting Library
$ 44.99 US Packt Publishing
£ 27.99 UK Birmingham - Mumbai
€ 41.99 EU www.packtpub.com
Prices do not include
local sales tax or VAT
where applicable David R. Heffelfinger
JasperReports for
Java Developers
Create, Design, Format, and Export Reports with
the World's Most Popular Java Reporting Library
David R. Heffelfinger
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 Editorial Manager
David R. Heffelfinger Dipali Chittar
Reviewers Indexer
Thomas M. Ose Mithil Kulkarni
Meenakshi Singh
Proofreader
Sarosh Khateeb
Chris Smith
Development Editor
Production Coordinator
Douglas Paterson
Manjiri Nadkarni
Assistant Development Editor
Layouts and Illustrations
Nikhil Bangera
Shantanu Zagade
Technical Editor
Cover Designer
Priyanka Baruah
Manjiri Nadkarni
Project Manager
Patricia Weir
About the Author
David R. Heffelfinger 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 Workflow 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
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
<field> 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
[ ii ]
Table of Contents
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
[ iii ]
Table of Contents
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
[ iv ]
Table of Contents
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
[ v ]