Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Printing in Java, Part 1 - JavaWorld October 2000

Printing in Java, Part 1 - JavaWorld October 2000

Tutorial Details:

Printing in Java, Part 1
Printing in Java, Part 1
By: By Jean-Pierre Dubé
Acquaint yourself with the Java printing model
elcome to the first article in a five-part series on printing in Java. In this series, you will learn the strengths and weaknesses of the Java printing API. My goal is to help you build a framework that will work on top of the API to ease the burden of creating printed output. This framework will allow you to create pages with running headers/footers, and insert paragraphs, images, and tables.
This month, I will explain the terminology used in printing and introduce the Java printing model and API. As you will see throughout this series, printing using the API is not easy; rendering complex pages using a higher-level API might be helpful. That is why our goal will be to build a framework that will provide all the fundamental functionality required to effortlessly render pages. But first, let's learn the basics.
Printing in Java: Read the whole series!
Part 1: Acquaint yourself with the Java printing model
Part 2: Print your first page and render complex documents
Part 3: Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API
Part 4: Code the print framework
Part 5: Discover the print framework's support classes
Definition of a page
Before we dive into the technicalities of the printing API, let's start by defining some terminology that I will use throughout this series. Although this terminology might seem trivial, it will help clear up some confusion about margins.
As you probably know, Gutenberg invented the first printing press. At that time, he had to create a terminology to describe the layout of a page. Here is Gutenberg's definition of a page:
Figure 1. Layout of a portrait page
Figure 2. Layout of a landscape page
In Figures 1 and 2, we can see that the page is divided into several areas. The printer margins make up the page's periphery. They are printer-dependent and define the minimum area that the printer needs to feed the page. Printer margins are not user-definable. We seldom use or know the sizes of printer margins, but some printer manufacturers will publish them in their user manuals. In Java, you do not need to know these measurements; the API returns the dimensions of the printable area.
Just inside the printer margins are the margins that define the contour of the page. Notice that 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. On a page printed in duplex mode -- that is, with printing on both sides -- the gutter can also be found on the page's right side. To obtain the total usable left or right margin, you add the gutter to either the left or right margin. The printing API itself does not support the gutter, but our print framework will enable you to define one. As strange as it may seem, the print API also fails to support margins. The only way to set margins is to set the location and size of the printable area.
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. 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.
Units of measurement
When working with the Graphics2D class, it is important to understand the difference between device space and user space. In the device space, you work in pixels using the resolution of the device. A square of 100 pixels by 100 pixels drawn on a device that has a resolution of 1,024 pixels by 768 pixels will not be the same size as it is when rendered on a device that has a resolution of 1,600 pixels by 1,400 pixels. The reason is simple: because the second device features more pixels per inch, the square will appear smaller.
User space, on the other hand, allows us to think in terms of measurement units, regardless of the device's resolution. When you create a Graphics2D object for a given device (printer or screen), a default transform is generated to map the user space to the device space. In user space, the default is set to 72 coordinates per inch. Instead of thinking in terms of pixels, you think in terms of units. A 1-by-1-inch square is 72 units by 72 units. A letter-size page (8.5 by 11 inches) is 612 by 792 points. When using the print API, you must set your mind to work with units because all the classes work in the user space.
Java printing system
The Java printing system has evolved considerably in its last two releases. Starting with version 1.2, the printing system allows you to use the Java 2D API -- one of the most advanced graphical APIs built as part of a programming language -- to render a page. This 2D API allows whatever is drawn on the screen to be rendered on paper.
Although more advanced now, the printing API still only supports the printer currently selected by the user at any given time. Java does not support printer discovery -- obtaining a list of available printers and their features on a given computer. Available printers can either be local or networked. When using the API, no way exists for obtaining a printer list programmatically; only if the print dialog is displayed can the user select a printer. This is a feature that Sun, which is adhering to the Internet Printing Protocol, will address in the next version of Java (1.4).
The printing model changed completely in Java 1.2. In previous versions of Java, the rendering process was not optimized at all. In Java 1.1 for example, printing a simple page required a great deal of memory and was very slow. Java 1.2 streamlined and optimized the rendering process. This redesigned API is based on a callback model, in which the printing subsystem, not your program, controls when a page is rendered. This model is more object-oriented in nature than the one used in JDK 1.1, in which the application was in charge of the printing process.
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.
Rendering models
There are two printing models in Java: Printable jobs and Pageable jobs.
Printables
Printable jobs are the simpler of the two printing models. This model only uses one PagePainter for the entire document. Pages are rendered in sequence, starting with page zero. When the last page prints, your PagePainter must return the NO_SUCH_PAGE value. The print subsystem will always request that the application render the pages in sequence. As an example, if your application is asked to render pages five through seven, the print subsystem will ask for all pages up to the seventh page, but will only print pages five, six, and seven. If your application displays a print dialog box, the total number of pages to be printed will not be displayed since it's impossible to know in advance the number of pages in the document using this model.
Pageables
Pageable jobs offer more flexibility than Printable jobs, as each page in a Pageable job can feature a different layout. Pageable jobs are most often used with Book s, a collection of pages that can have different formats. I will explain the Book class in a moment.
A Pageable job has the following characteristics:
Each page can have its own painter. For example, you could have a painter implemented to print the cover page, another painter to print the table of contents, and a third to print the entire document.
You can set a different page format for each page in the book. In a Pageable job, you can mix portrait and landscape pages.
The print subsystem might ask your application to print pages out of sequence, and some pages may be skipped if necessary. Again, you don't have to worry about this as long as you can supply any page in your document on demand.
The Pageable job doesn't need to know how many pages are in the document.
Books
Also new since version 1.2 is the Book class. This class allows you to create multiple-page documents. Each page can have its own format and its own painter, giving you the flexibility to create sophisticated documents. Since the Book class implements the Pageable interface, you could implement your own Book class when the provided Book class lacks the features that you require.
A Book class represents a collection of pages. When first created, the Book object is empty. To add pages, you simply use one of the two append() methods (see my explanation of this class in the API section for more details). This method's parameters are the PageFormat object, which defines the physical characteristics of the page, and a PagePainter object, which implements the Printable interface. If you don't know the number of pages in your document, simply pass the UNKNOWN_NUMBER_OF_PAGES value to the append() method. The printer system will automatically fin


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Printing in Java, Part 1 - JavaWorld October 2000

View Tutorial:
Printing in Java, Part 1 - JavaWorld October 2000

Related Tutorials:

Displaying 1 - 50 of about 4061 Related Tutorials.

JDO UNPLUGGED - PART 1
. ============================================================================== BOOKS FOR REFERENCE: 1. Java Data Objects (May... JDO - Java Data Objects Tutorials, JDO Java Data Object, JDO Tutorial, JDO UNPLUGGED - PART I JDO UNPLUGGED - PART I
 
Printing Table in JRuby
Printing Table in JRuby Printing Table in JRuby... in this part of JRuby tutorial we will tell you how to take input in  JRuby from...:         JRubyTable.java # Printing Table in JRuby
 
JDO UNPLUGGED - PART II
JDO - Java Data Objects Tutorials, JDO Java Data Object, JDO Tutorial, JDO UNPLUGGED - PART II JDO UNPLUGGED - PART II... www.jcp.org and selecting JSR-12 or can be downloaded from sun java website. Goto
 
String Exercises 1 - Answers
Java: String Exercises 1 - Answers Java: String Exercises 1 - Answers Answers to the String Exercises 1. 3 -- s refers to exactly the same string as a. ERROR -- t
 
Java Virtual Machine(JVM)
of Java architecture and it is the part of the JRE (Java Runtime Enviroment... to run. JVM is a part of Java Run Time Environment that is required by every... environment, it is impossible to run Java software. JVM forms the part
 
Simple Linked List Exercise 1
Java: Simple Linked List Exercise 1 Java Notes: Simple Linked List Exercise 1 Name... strings and puts them in a doubly linked list. 1 2 3 4 5 6
 
Common Interview Questions Page -1
Common Interview Questions Page -1 Common Interview Questions Page -1       ...;       Question:1. Tell Me a Little
 
Printing Command Line arguments in JRuby
Printing Command Line arguments in JRuby Printing... of JRuby tutorial we will discuss about printing command line arguments in JRuby...;   CommandLine.rb # Printing commandline arguments
 
Java Interview Questions - Page 1
Java Interview Questions,interview questions java Java Interview Questions - Page 1     ...;         Java Interview
 
New Page 1
and use application data without using java code. EL was introduced in JSTL 1.0... be a map key.. If the first value is a Java Bean, then second value must be a bean... one is ${bigFive[0]} The second one is ${bigFive["1"]} EL Operators
 
String Exercises 1
Java: String Exercises 1 Java: String Exercises 1 Name...() __________ 1 + a __________ a.toUpperCase() __________ "Tomorrow".indexOf
 
Constructor Chaining Exercise 1
Java: Constructor Chaining Exercise 1 Java NotesConstructor Chaining Exercise 1 Name _______________________________ The first line of every constructor must be either
 
Programming: Initials 1
Java: Programming: Initials 1 Java: Programming: Initials 1 Name ________________________________________ Write a program that asks for names and displays the initials. Ask the user
 
Example of printing Text message passed from XML to JSP
Example of printing Text message passed from XML to JSP Example of printing Text message passed from XML to JSP... towards showing how to construct a Java object from an XML document. For this what
 
Bayanihan Linux 4 Beta 1 has been released
Bayanihan Linux 4 Beta 1 has been released Bayanihan Linux 4 Beta 1 has been released  Bayanihan Linux 4 Beta 1 is now available... and Technology Institute last October 2001. This was the first initiative
 
JFreeChart - An Introduction
java chart library. David Gilbert founded the JFreeChart project in February 2000
 
Core Java Interview Question Page 1
Core Java Interview Question, Interview Question Core Java Interview Question Page 1    ...: How could Java classes direct program messages to the system console
 
New to Java?
Foundation Classes (JFC) - It is a part of Java class libraries based on the Java... Connectivity (JDBC) - JDBC API is a part of Java Standard Edition that helps in accessing....   Learn EJB - Enterprise Java Beans are a part of J2EE
 
New to Java?
Foundation Classes (JFC) - It is a part of Java class libraries based on the Java... Connectivity (JDBC) - JDBC API is a part of Java Standard Edition that helps in accessing....   Learn EJB - Enterprise Java Beans are a part of J2EE
 
WEBSERVICE USING APACHE AXIS - TUTORIAL-2 AXIS FOR EJB-WEBSERVICE (part-5)
had seen parts 1 to 4 of this tutorial on exposing an EJB as XML-Webservice using Axis. This is a 7 part article.?  part-1 : Overview part-2 : deploying...-bean itself) ??????????????? a) java:RPC ????????????? ??b) java:EJB ?part-6
 
Collections Exercise 1 - Unique Components
Java: Collections Exercise 1 - Unique Components Java: Collections Exercise 1 - Unique Components Name ____________________________________ For the purposes of this exercise, the only
 
Java Releases
Year Java 1.5.0_09 October 2006 Java 1.5.0_08... 1.5.0_05 October 2005 Java 1_5_0_04 July 2005..., Standard Edition 1.3 (J2SE 1.3) May 2000 Java 2 Platform
 
Wi-Fi as a part of LBS
Wi-Fi as a part of LBS Wi-Fi as a part of LBS                         
 
PaperClips
; A simple, light weight, extensible Java printing plug-in for SWT. PaperClips hides... toPaperClips for printing. PaperClips includes support for printing text, images...: Java 1.4 or later. SWT 3.2 or later. PaperClips can be used as an Eclipse
 
Summary - GUI Layouts 1 - FlowLayout, BorderLayout, GridLayout
Java: Summary - GUI Layouts 1 - FlowLayout, BorderLayout, GridLayout Java: Summary - GUI Layouts 1 - FlowLayout, BorderLayout, GridLayout Set the layout manager for a container p
 
Printing a Stack Trace to the Server Console
Printing a Stack Trace to the Server Console Printing a Stack Trace to the Server Console    ...): <HTML> <HEAD> <TITLE>Printing a Stack Trace
 
Read attachment message using Java Mail
api. Java Mail API provides classes to send multiple Mime body part with one... Read attachment message using Java Mail Read attachment message using Java Mail
 
Beginners Java Tutorial
java Beginner,java Beginners,Beginning java,Beginners Java Tutorial,Java for beginners Beginners Java Tutorial...; This tutorial will introduce you with the Java Programming
 
ASCII values table
was developed when non- printing characters were rarely used.  In the  example given below we have include the description of the some non- printing characters...="1" cellpadding="0" cellspacing="0" bgcolor="
 
Beginners Java Tutorial
java Beginner,java Beginners,Beginning java,Beginners Java Tutorial,Java for beginners Beginners Java Tutorial...; This tutorial will introduce you with the Java Programming
 
Creating Class in JRuby
;     In this part of JRuby tutorial you will come...;      puts "Deductions : 2000"   ...; # printing employee information      puts " Employee
 
Advanced Java Tutorials
will find the Java topics that should be part of Advanced Java study course. We have... Course Here the topics that you should master as part of your advance java study... Advanced Java Tutorials, Advanced Java Tutorial, Advance Java Tutorial
 
LG3D LiveCD 2.4 Test 1 has been relesed now
LG3D LiveCD 2.4 Test 1 has been relesed now LG3D LiveCD 2.4 Test 1 has been relesed now LG3D LiveCD 2.4-test1 available... is a bootable Linux CD incorporating the Project Looking Glass, a Java-based technology
 
RR64 Linux 3.0 Beta 1 has been released
RR64 Linux 3.0 Beta 1 has been released RR64 Linux 3.0 Beta 1 has been released RR64 Linux 3.0b1 (Xgl Beauty). RR64... Java and Firefox available out of the box; complete multimedia and DVD support
 
"JSONArray" example in Java
in Java          ...;    In this part of JSON tutorial you will study how to use JSONArray in Java. JSONArray is a sequential and ordered way of collection
 
'if' Statement - Braces
Java: 'if' Statement - Braces Java Notes'if' Statement - Braces Braces { } not required for one statement If the true or false part of and if statement has
 
SEE with Java
SEE with Java SEE with Java  ...; Position Vacant: SEE with Java Job Description  Java... project full life cycle project. Candidate with Java, EJB, JPA , Hibernate
 
Java Programming Books
, and the accompanying Java Pet Store sample application, are part of the successful Java BluePrints..., to run Java programs. However the substantive part of the environment consists... known as the Java Application Programmer Interface (API). The most important part
 
Understanding Hello World Java Program
programming in Java, its necessary to understand each and every part... Hello World Program Java,Hello World First Program,Hello World Java,Online Hello World Java Program Understanding Hello World
 
Creating Website with the use of template in Wicket
; In the previous section of Wicket tutorial you have studied about "printing... WebsiteApplication.java and according to links we have created four java files
 
Java API
. Official Java Core API The official core API is part of JDK... Java API, what is java api? Java API...;  What is Java API? Java API is not but a set of classes and interfaces
 
Example - Display Extension
Java: Example - Display Extension Java: Example - Display Extension This program reads in a file name and displays the extension (that last part of the name after the final dot
 
Navigable Map Example
value pair like navMap.put(1, "January"); whereas in NavigableSet we...;String>();     navMap.put(1, "January");  ...;);     navMap.put(10, "October");   
 
Prime Number in Java
Java Prime Number,Java Prime Number Program,Prime Number Example in Java Prime Number in Java    ...;          This Java
 
Product Components of JDBC
.  JDBC application programmming interface is a  part of the Java...;     JDBC has four Components: 1...-ODBC Bridge. 1. The JDBC API. The JDBC application
 
Unzip File Using Java
Java Unzip,Java Unzip Example,Java Unzip File,Free Unzip Program in Java...; Lets learn how to unzip a file using java... block. We are printing the whole exception using printStrackTrace() method. Here
 
Literals in Jsp
, that are written literally as part of a program code. A literal is a expression... are using a literals. We are printing the literals by using the out implicit object
 
Other Java Resources
are amazingly good! Internationalization Internationalization, Part 1... Java: Other Java Resources Java NotesOther Java Resources Online books, notes, and reviews Java
 
Difficult Interview Questions Page -1
Difficult Interview Questions Page -1 Difficult Interview Questions Page -1       ...;       Question 1: Tell me about yourself
 
Java Programmers with Financial Application
; Experience: 1- 6 Years  Keywords: Java, EJB, ERP, Finance, Struts 2, JSF... Java Programmers with Financial Application Java...;         Position Vacant: Java
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.