Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By
Anand John
Programmer,
Department of Computer Applications
Christ College, Rajkot
TERMINOLOGY
USED
IN
PRINTING
Definition of a page
Gutenberg invented the first printing press. At that time, he had to create a
terminology to describe the layout of a page.
The left and right margins extend the length of the page minus
the top and bottom printer margins.
The gutter on the left side of the page provides an additional
margin that is used primarily for binding the pages in a book.
The printing API in java itself does not support the gutter.
The print API in java also fails to support margins.
We can design a print framework that will enable us to provide
gutter spacing.
Finally, the area in the middle of the page is called the
printable area.
At first glance, the page layout might look similar to the
BorderLayout that we are accustomed to in java.
However, in the BorderLayout, both top and bottom
components extend the width of the display area, whereas in
the physical page layout, the top and bottom margins are
contained between the left and right margins.
To simplify the concept, let's say that your program has a contract
with the printing subsystem to supply a given page at a given time.
The printing subsystem may request that your application render a
page more than once, or render pages out of sequence.
This model provides several advantages. First, by sending strips of
the page instead of the whole page to the printer, it allows the
application to print complex documents that would require more
printer memory than is available.
The application does not have to know how to print each strip; it only
needs to know how to render a given page. The API will take care of
the rest.
In this case, the printing subsystem might request that a page be
rendered several times depending on the number of strips required to
completely print the page.
Second, if the paper tray on a particular printer outputs the pages in
reverse order, then your application might be asked to print the
document in reverse order, so it will appear in the right order in the
output tray.
The print system in Java has two distinct models: the Printable
and the Pageable.
Though Printable can print simple documents, it features
several limitations, the major one being that all pages must
share the same format.
The Pageable model, on the other hand, offers much more
flexibility. It can create multipage documents, with each page
formatted differently.
2.
3.
4.
5.
6.
Interface Printable
public interface Printable
The Printable interface is implemented by the print methods of the
current page painter, which is called by the printing system to
render
a
page.
When
building
a Pageable,
pairs
of PageFormat instances and instances that implement this interface
are
used
to
describe
each
page.
The
instance
implementing Printable is called to print the page's graphics.
A Printable(..) may be set on a PrinterJob. When the client
subsequently initiates printing by calling PrinterJob.print(..)
control is handed to the printing system until all pages have been
printed.
It does this by calling Printable.print(..) until all pages in the
document have been printed. In using the Printable interface the
printing commits to image the contents of a page whenever
requested by the printing system.
if (printJob.printDialog()) {
try {
printJob.print();
}
catch (Exception
PrintException)
{ PrintException.printStackTrac
e();
}
Figure 2
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat = printJob.pageDialog (documentPageFormat);
book.append (new Document (), documentPageFormat);
To render attractive text, you must first understand the structure of a font.
Characters align along a line called the baseline, which is the vertical point
of reference for positioning a font.
The ascend is the distance between the baseline and the top of the tallest
character in a string.
The space between the baseline and a string's lowest glyph is the descend.
The leading represents the vertical distance between two characters, and the
font height is the ascend plus the leading plus the descend. Finally, we call a
string's length the advance.
All of these parameters are font dependent, meaning that text rendered
using an Arial font will not occupy the same physical space as the same text
rendered using a Lucida font.
Java offers eight default fonts: Serif, Sans Serif, Dialog, Dialog Input,
Lucida Sans, Lucida Sans Typewriter, Lucida Bright, and Monospace.
Java uses logical font names. These names automatically map to an
operating system's fonts.
The mapping occurs in the JDK directory's jre/lib/font.properties file.
If your application needs access to more fonts, set the java.awt.fonts
parameters of the Java command as in java D java.awt.fonts=[fonts
directory] ... -- to point to a font directory of Type 1 (Postscript) fonts or
True Type fonts.
The Java Graphics API supplies many classes that alleviate the task of
manipulating fonts.
TextLayout offers a great deal of functionality in rendering high quality
text. This class can render bidirectional text such as Japanese text, where
figures align right to left instead of the North American style, which flows
left to right.
Name
Type
Font
Class
FontMetrics
FontRenderContext Class
Description
The Font class represents an instance of a
font face. Use this class to create a new
font based on the available fonts on the
target system.
Example
Font normalFont = new Font ("serif", Font.PLAIN, 12);
Font boldFont = new Font ("serif", Font.BOLD, 12);
g2.setFont (normalFont);
g2.drawString ("This is a normal font ");
g2.setFont (boldFont);
g2.drawString ("bold ");
g2.setFont (normalFont);
g2.drawString ("attribute", 72, 72);
If we had to render an entire paragraph! We can simplify this example with an
AttributedString object.
AttributedString attributedString = new AttributedString ("This is a Bold attribute");
attributedString.addAttribute (TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD,
11, 14);
g2.drawString (attributeString.getIterator (), 72, 72);
Output will be:
This is a Bold Attribute
Printing images
Printing an image is as straightforward as loading it in an Image object
and invoking the Graphics2D.drawImage() method.
The process of printing an image is divided into three easy steps.
1. Create a URL that will point to the image you want to print.
2. Load the image using a MediaTracker class. With this class, you can
load GIF, JPEG, and PNG files. If you want to load other image types,
use the Advance Imaging API.
3. Draw the image using the drawImage() method of the Graphics2D class.
drawImage() needs six parameters. The first is the Image object to print;
the second and third parameters are coordinates for the image's top left
position, and the fourth and fifth parameters specify the image's width
and height. The last parameter is for the ImageObserver. The Document
extends the Component class. All Component classes implement the
ImageObserver interface.
1.
2.
3.
4.
5.
6.
7.
We are now able to evaluate its strengths and weaknesses of Java Print API
and so we can build a framework on top of it.
The print framework or print model will :
Be easy to use.
Provide the high-level functionality required to efficiently render pages.
Loosely couple with the Print API. Because the framework has no direct
ties to the Java Print API, it will allow greater support for other output
formats, such as PDF or HTML.
Provide a structure that clearly defines each component involved in
creating documents (document, page, paragraph, and so on).
Use an abstract measurement system. Developers will thus be able to work
with the measurement system of their choice or even create their own.
Provide a print-preview facility. The Java Print API has no support for
previewing the output before you print.
Support a standard and portable page-setup dialog. The print framework
will provide one page-setup dialog that will be uniform across all
platforms, but its functionality varies from platform to platform.
PFPage class
The PFPage defines a page and acts as a container of PFPrintObject
objects.
When the print() method invokes, it renders each object in the page.
The PFPage class does not store the page parameters directly; it uses a
PFPageFormat class for storage
Use the PFPageFormat class to set and store the page format data.
PFPageFormat computes the page area origin with the
getPageAreaOrigin() method, and the page area size using the
getPageAreaSize() method.
Use getPageFormat() to obtain a java.awt.PageFormat object.
Before returning a PageFormat object, getPageFormat() must first
evaluate the page orientation and set the page's margins and size
accordingly.
FPrintObject's rendering process is a bit more complex than PFPage's,
because each object must be rendered in relation to its parent object. The
parent object can be either the PFPage object onto which the print object
was added or another PFPrintObject.
Swing Component
Demonstration
Link to Printing Jar
JasperReports
Optional Libraries and Tools
There are a number of libraries that are required only if we wish to take advantage of
some of JasperReports' features. These optional libraries and their uses are listed next.
Apache ANT
JasperReports comes bundled with some custom ANT targets for previewing report
designs and for viewing reports serialized in JasperReports' native format. Although
not mandatory, it is very helpful to have ANT available to take advantage of these
custom targets.
We can use JasperReports-specific ANT targets. Therefore, ANT is required when
following the examples. ANT can be downloaded from http://ant.apache.org/ .
JDT Compiler
JDT stands for Java Development Tools. The JDT compiler is the Java compiler
included with the Eclipse IDE (Integrated Development Environment). The JDT
compiler is needed only when the JasperReports application is running under a Java
Runtime Environment (JRE), and not under a full JDK.
JDBC Driver
When using a JDBC datasource, the appropriate JDBC driver for our
specific RDBMS is needed. The following table lists popular relational
database systems and the required JAR files to be added to the
CLASSPATH (exact file names may vary depending on the version). The
names shown in the table reflect the latest stable versions at the time of
writing:
RDBMS Driver JAR Files
Firebird firebirdsql-full.jar
HSQLDBhsqldb.jar
MySQL mysql-connector-java-3.1.10-bin.jar
Oracle
classes12dms.jar
DB2 db2java.zip
PostgreSQL postgresql-8.0-312.jdbc3.jar
SQL Server mssqlserver.jar
msbase.jar
msutil.jar
Sybase
j conn3.jar
iText
iText is an open-source library for creation and manipulation of PDF files.
iText is needed only if you want to export your reports to PDF or RTF
format. Version 1.2.2 of the JasperReports project ZIP file includes iText
version 1.3.1. The file to be added to your CLASSPATH is itext-1.3.1.jar.
iText can be downloaded separately from http://www.lowagie.com/iText/.
JFreeChart
JFreeChart is an open-source library for creating professional-looking
charts including, but not limited to 2-D and 3-D pie charts, 2-D and 3-D
bar charts, and line charts. JFreeChart is needed in our CLASSPATH only
if you intend to add charts to your reports. JFreeChart version 1.0 RC1
can be found in the lib directory inside the JasperReports project ZIP file
version 1.1. The file to be added to the CLASSPATH is jfreechart-1.0.0rc1.jar.
JFreeChart
can
be
downloaded
separately
from
http://www.jfree.org/jfreechart/.
Thank you