JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - Table of Contents Sample (version 4.6.0) | ![]() |
|
|
|
Main Features in This Sample | |
| Creating Table-Of-Contents Structures |
| File Resolver |
![]() | ![]() | ![]() | ![]() |
|
|
top | |||||
|
|||||
![]() | Creating Table-Of-Contents Structures | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| How to create a table of contents with hyperlinks at the beginning of a document. | ||
| Since |
| 0.4.2 | ||
|
|||||
|
Table Of Contents - Overview
A table of content becomes very necessary when complex documents with huge number of pages are generated. But, being almost impossible to determine the total number of pages, or the current content of generated pages at report design time, there is no built-in report element in JasperReports to handle the table of contents. The document structure unveils itself step by step at report filling time. Therefore this is the proper moment for building a table of content. Once a print element is generated, its place within document is completely determined, so from now on we are able to find and reference that element. If the element is starting a new section, we can add a new entry in the table of contents, pointing here. The table of content cannot be finalized before the report is completely filled, so it can be very easily placed on the last page of the document in the summary section. But how about documents that require to start with a table of contents? With JasperReports this document
layout can be also realized, as shown in this sample.
Table Of Contents Sample There are various ways to build tables of contents, depending on everyone's free imagination. The way imagined here is the following:
Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/hsqldb within the JasperReports source project and run the > ant runServer command.
It will start the HSQLDB server shipped with the JasperReports distribution package. Let this terminal running the HSQLDB server.
Open a new command prompt/terminal window and set the current folder to demo/samples/tableofcontents within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/tableofcontents/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||
|
|||||
top | |||||
|
|||||
![]() | File Resolver | Documented by Sanda Zaharia | |||
|
|||||
| Description / Goal |
| Shows how to use file resolver implementations in order to locate file resources referenced from within reports using relative paths. | ||
| Since |
| 2.0.5 | ||
|
|||||
|
File Resolver - Overview
By default, the JR engine interprets resource locations as file system paths that can be either absolute or relative to the current user directory (given by the Java user.dir system
property). If the path constructed this way resolves to an existing file, the report resource
will be loaded from this file.
The default behavior can be changed by using the REPORT_FILE_RESOLVER parameter at fill time to pass a file resolver object. This object needs to be an instance of a class that implements the FileResolver interface. The interface contains a single method which is responsible for resolving a resource location to an abstract file path (represented by a java.io.File object).
When a file resolver is set via the report parameter, the engine will use it to interpret resource locations as file paths. If a file resolver returns a non-null file path for a resource location, the file located at the returned path will be used to load the resource. JasperReports ships with the built-in SimpleFileResolver implementation that interprets resource locations as paths relative to one or several file system folders. To create such a file resolver, the user has to specify one or more resources folders. When the resolver is asked to resolve a resource location, it interprets the locations as a path relative to each of the resource folders, and returns the first path that corresponds to an existing file. The SimpleFileResolver is called in this sample at fill time to resolve paths to compiled reports saved with the .jasper extension. See the example in the
src/TableOfContentsApp.java :
public void fill() throws JRException { long start = System.currentTimeMillis(); //Preparing parameters Map parameters = new HashMap(); parameters.put("ReportTitle", "Orders Report"); SimpleFileResolver fileResolver = new SimpleFileResolver( Arrays.asList(new File[]{new File("build/reports")}) ); fileResolver.setResolveAbsolutePath(true); parameters.put(JRParameter.REPORT_FILE_RESOLVER, fileResolver); JasperReport jasperReport = (JasperReport)JRLoader.loadObject("build/reports/TableOfContentsReport.jasper"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, getDemoHsqldbConnection()); jasperPrint = moveTableOfContents(jasperPrint); JRSaver.saveObject(jasperPrint, "build/reports/TableOfContentsReport.jrprint"); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); }Note: As shown in the API javadoc, the REPORT_FILE_RESOLVER parameter is now deprecated. It is highly recommended to switch to the JasperReportsContext approach wherever is possible. |
||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |