You are on page 1of 58

Java Print Service API User Guide

(Draft 0.1)

A
Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. 650-960-1300
Part No. 8xx-xxxx-xx Month 2001, Revision 01 Send comments about this document to: java-print@sun.com

ph l

D a

ft ra

1.

2.

Introduction...................................................................................5 History of Printing on the Java Platform ...................................5 What the Java Print Service API Offers.....................................6 Java Print Service Architecture..................................................6 The javax.print Package ........................................................6 Discover Print Services ....................................................7 Specify the Print Data Format ..........................................7 Create Print Jobs...............................................................7 The javax.print.event Package ..............................................8 How Applications Use the Java Print Service ...........................8 A Basic Example...................................................................9 Attributes .....................................................................................11 Attribute Categories and Values ..............................................11 Attribute Roles .........................................................................12 Attribute Sets............................................................................13 How to Specify Attributes........................................................14 Standard Attributes ..................................................................15 OrientationRequested ..........................................................15 Copies..................................................................................16 Media...................................................................................16 MediaSize .......................................................................17 MediaPrintableArea........................................................17

Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. 650-960-1300

Send comments about this document to: java-print@sun.com

3.

4.

Destination ..........................................................................18 SheetCollate ........................................................................18 Sides ....................................................................................19 Fidelity ................................................................................19 Uses of JPS Attributes..............................................................19 Specifying Document Types .......................................................21 Client-Formatted Print Data.....................................................22 MIME Types for Preformatted Data ...................................22 Text Data ........................................................................23 Page Description Language Documents.........................23 Image Data......................................................................23 Autosense Print Data ......................................................24 Representation Classes........................................................24 Importance of Character Encoding ..........................................25 Service-Formatted Print Data...................................................25 How to Use DocFlavor ............................................................26 Printing and Streaming Documents ..........................................28 StreamPrintService Versus PrintService..................................28 Locating Services .....................................................................29 Discovering Print Services ..................................................29 Discovering Stream Print Services......................................30 Obtaining a Print Job................................................................31

Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. 650-960-1300

Send comments about this document to: java-print@sun.com

5.

A. B. C. D. E.

Creating a Doc .........................................................................31 Registering for Events..............................................................32 Print Service Events ............................................................32 Print Job Events...................................................................33 PrintJobAttributeListener ...............................................33 PrintJobListener..............................................................33 Submitting the Print Job...........................................................34 Submitting the Print Job to the Printer ................................34 Submitting the Print Job to the Stream................................35 Print Service Providers.............................................................36 Printing and Streaming 2D Graphics .......................................37 Using PrinterJob to Print or Stream Graphics..........................37 Printing 2D Graphics...........................................................38 Streaming 2D Graphics .......................................................39 Using Service-Formatted Data.................................................40 Printing the Service-Formatted Data...................................40 Streaming Service-Formatted Print Data ............................40 Example: PrintPS.java ...............................................................42 Example: PrintGIFtoStream.java .............................................44 Example: Print2DPrinterJob.java ............................................46 Example: Print2DGraphics.java ...............................................48 Example: Print2DtoStream.java ...............................................51

Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. 650-960-1300

Send comments about this document to: java-print@sun.com

F. Example: PrintGIF.java.............................................................53 G. JavaTM Print Service Glossary.................................................56

Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, CA 94303 U.S.A. 650-960-1300

Send comments about this document to: java-print@sun.com

CHAPTER

Introduction
The JavaTM Print Service (JPS) is a new Java Print API that is designed to support printing on all Java platforms, including platforms requiring a small footprint, but also supports the current JavaTM 2 Print API. This unified Java Print API includes extensible print attributes based on the standard attributes specified in the Internet Printing Protocol (IPP) 1.1 from the IETF Specification, RFC 2911. With the attributes, client and server applications can discover and select printers that have the capabilities specified by the attributes. In addition , which allows applications to transcode print data to to the included different formats, a third party can dynamically install their own print services through the Service Provider Interface.

History of Printing on the Java Platform


Basic printing support for the Java platform was first introduced in the Java Development Kit, version 1.1 in 1997. The JDK 1.1 printing API provided developers with a basic framework for printing the user-interface content from client applications. JDK 1.1 printing, class, also called the AWT Printing API, was designed around the class creates a subclass of , which encapsulates a printing request. The which implements the rendering calls to image the page. In 1998, the SDK 1.2 advanced printing on the Java platform with the package by allowing applications to print all Java 2D graphics, which includes 2D graphics, text, and images. and classes were introduced For the SDK version 1.3, the to AWT printing so that client applications could specify the properties of a print job and the attributes of a page. The two APIs primarily support page imaging, which is the rendering and formatting of a page and is one component of a printing subsystem. Although a client can choose a printer from a print dialog and can specify printing attributes using JobAttributes and

scihparG boJtnirP.twa.avaj

tnirp.twa.avaj

setubirttAegaP

boJtnirP

setubirttAboJ

ecivreStnirPmaertS

PageAttributes, neither the AWT nor Java 2D printing APIs support discovery of printers based on their capabilities, which is another key component of any printing subsystem. For more details on the history of printing on the Java platform refer to Printing on the Java Platform (http://java.sun.com/printing/whitepaper.html).

What the Java Print Service API Offers


The Java Print Service API unifies and extends printing on the Java platform by addressing many of the common printing needs of both client and server applications that are not met by the current Java printing APIs. In addition to supporting the current Java 2D printing features, the Java Print Service offers many improvements, including: Both client and server applications can discover and select printers based on their capabilities and specify the properties of a print job. Thus, the JPS provides the missing component in a printing subsystem: programmatic printer discovery. Implementations of standard IPP attributes are included in the JPS API as first-class objects. Applications can extend the attributes included with the JPS API. Third parties can plug in their own print services with the Service Provider Interface.

Java Print Service Architecture


tneve.tnirp.xavaj
Introduction

Discover Print Services Specify the print data format Create print jobs from a print service Send the print data to a printer or a stream

tnirp.xavaj

The that you use to:

dradnats.etubirtta.tnirp.xavaj etubirtta.tnirp.xavaj tnirp.xavaj

The Java Print Service API consists of four packages: ,

, , and

The javax.print Package


package is the main package of the API. It contains classes and interfaces

Chapter 1

Discover Print Services


An application invokes the static methods of the abstract class to locate print services that have the capabilities to satisfy the application's print request. For example, to print a double-sided document, the application first needs to find printers that have the double-sided printing capability. interface represents an actual printer that might be An implementation of the . The can be returned from an implementation of queried to determine its capabilities by invoking one of the many methods that return the print services supported attributes. The Locating Services section of the Printing and and . Streaming Documents chapter discusses using The Java 2 SDK includes implementations that can locate the standard platform printers. To locate other types of printers, such as IPP printers or JINI . The printers, a print-service provider can write implementations of implementations print-service provider can dynamically install these using the SPI JAR file specification. Rather than sending data to a printer, an application can also print the data to an output , which allows the application to convert the data to stream using a , an application uses the lookup methods different formats. To obtain a abstract class. This class is similar to that of the of the class in that it also discovers print services. The chapter Printing and and Streaming Documents describes using .A can also be used to send 2D graphics to an output stream. See Printing and Streaming 2D Graphics for more details.

Specify the Print Data Format


rovalFcoD rovalFcoD
The class represents the print data format, such as JPEG or PostScript. A object consists of a MIME type describing the format, and a representation class name, such as java.io.InputStream, that indicates how the document will be delivered to the service. As a convenience, the JPS API also includes pre-defined DocFlavor objects representing common data formats. The Specifying Document Types chapter describes in objects. more detail how to use

Create Print Jobs


When an appropriate print service is found, an application creates a print job from it. The class, which provides the actual method. print job is represented by the , the application creates a object to encapsulate the print data and Before calling and the print request attributes, which are defined in the packages. The chapter Printing and Streaming and . An application that is using the Java Print Documents discusses using

Chapter 1

pukooLecivreStnirP

pukooLecivreStnirP tnirp ecivreStnirP

pukooLecivreStnirP pukooLecivreStnirP

etubirtta.tnirp.xavaj

ecivreStnirPmaertS ecivreStnirPmaertS yrotcaFecivreStnirPmaertS pukooLecivreStnirP yrotcaFecivreStnirPmaertS ecivreStnirPmaertS ecivreStnirPmaertS

ecivreStnirP coD

pukooLecivreStnirP ecivreStnirP boJtnirPcoD

boJtnirPcoD coD dradnats.etubirtta.tnirp.xavaj

pukooLecivreStnirP

rovalFcoD

tnirp

Introduction

Service to print 2D Graphics can use either the class or the class. The chapter Printing and Streaming 2D Graphics discusses using the Java Print Service to print 2D Graphics. The Attribute Packages and packages define The print attributes, which describe a capability of a print service, a charactaristic of a document, an instruction for processing a document or an entire print job, or the state of a print job or printer. package describes the types of attributes and how they can be interface is the superinterface for all attributes. The package includes classes and interfaces defining the five different kinds of attributes, each of which describes the capabilities of one piece of the printing process. For example, the interface defines attributes that clients use to describe the characteristics of a print job, which might include the number of copies to print. package enumerates all of the standard attributes The supported by the API, most of which are implementations of attributes specified in the IPP specification. The Attributes chapter discusses the attribute API in more detail.

The javax.print.event Package


The package contains classes that allow applications to register for events on print jobs and print services. Read the Registering for Events section for more information on registering for print job events.

How Applications Use the Java Print Service


A typical application using the Java Print Service API performs these steps to process a print request:

, which encapsulates a set of attributes that describe 2. Create and populate an the desired print service capabilities, such as the ability to print five copies, stapled, and double-sided.

4. Create a print job from the print service.


Chapter 1 Introduction 8

rovalFcoD

3. Lookup a print service that can handle the print request as specified by the and the attribute set.

teSetubirttA

rovalFcoD

1. Obtain a suitable

etubirttAtseuqeRtnirP

dradnats.etubirtta.tnirp.xavaj

etubirtta.tnirp.xavaj etubirttA etubirtta.tnirp.xavaj

The collected into sets. The

, which is a class that defines the format of the print data.

boJtnirPcoD

dradnats.etubirtta.tnirp.xavaj

boJretnirP

etubirtta.tnirp.xavaj tneve.tnirp.xavaj

5. Call the print jobs print method. The application performs these steps differently depending on what and how it intends to print. The application can either send print data to a printer or to an output stream. The print data can either be a document in the form of text or images, or a Java object encapsulating 2D Graphics. If the print data is 2D graphics , the print job can be represented by either a DocPrintJob or a PrinterJob. If the print data is a document then a DocPrintJob must be used. The combinations of printing methods and print data formats yield a choice of six printing mechanisms:

The Attributes chapter and the Specifying Document Types chapter describe how to create an attribute set and specify document types for use with any of the print mechanisms. The Printing and Streaming Documents chapter discusses printing and streaming documents . The Printing and Streaming 2D Graphics chapter explains printing and using streaming 2D graphics using both DocPrintJob and PrinterJob.

A Basic Example
Most applications using the Java Print Service API will probably send a document directly to a printer, which the following code sample demonstrates:

boJretnirP.tnirp.twa.avaj ecivreStnirPmaertS boJretnirP.tnirp.twa.avaj ecivreStnirP

Print 2D graphics to a Stream 2D graphics to a

using

using

coD a etaerC // ;IICSA_NIALP_TXET.MAERTS_TUPNI.rovalFcoD = tamroFym rovalFcoD epyt tnemucod eht teS // } ;nruter { )llun == maertstxet( fi } { )enff noitpecxEdnuoFtoNeliF( hctac } ;)"TXT.elif"(maertStupnIeliF wen = maertstxet { yrt ;maertStxet maertStupnIeliF elif eht tupnI //

boJtnirPcoD

Stream 2D graphics to an output stream by using a

boJtnirPcoD

Print 2D graphics to a printer by using a

boJtnirPcoD

Stream a document to an output stream by using a

boJtnirPcoD

Print a document to a printer by using a

and an implementation of and a

ecivreStnirPmaertS ecivreStnirPmaertS boJtnirPcoD ecivreStnirP

ecivreStnirP

and an implementation of and a

Chapter 1

Introduction

Although this sample only demonstrates one of the six ways to print, the other printing mechanisms work in a similar way. The rest of this guide discusses each piece of the printing process and all the printing mechanisms in more detail

} }{ )ep noitpecxEtnirP( hctac } ;)tesa ,coDym(tnirp.boj { yrt ;)(boJtnirPetaerc.]0[secivres = boj boJtnirPcoD { )0 > htgnel.secivres( fi secivres tnirp eht fo eno morf boj tnirp a etaerC // ;)tesa ,tamroFym(secivreStnirPpukool.pukooLecivreStnirP = secivres ][ecivreStnirP tes etubirtta eht ni snoitcurtsni // eht ot gnidrocca tamrof eht tnirp nac taht sretnirp eht revocsid // ;)XELPUD.sediS(dda.tesa ;)4A_OSI.eziSaideM(dda.tesa ;))5(seipoC wen(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP setubirtta fo tes a dliuB // ;)llun ,tamroFym ,maerttxet(coDelpmiS wen = coDym coD

Chapter 1

Introduction

10

CHAPTER

Attributes
When sending print data to a printer, a client also provides instructions on how to print the data, such as: what media (paper) to use and how many copies to print. The client specifies these processing instructions using the attribute definitions of the Java TM Print Service API. The Java Print Service API includes two packages that define attributes: and . The package contains the interfaces and classes that describe the types of attributes and how they are collected into sets. The package enumerates all of the standard attributes in the API. Most of the attribute definitions in the JPS API are implementations of the standard and extensible attributes defined by the Internet Printing Protocol (IPP) Specification from the IETF. This chapter describes the attribute definitions of the JPS API in more detail.The Attribute Categories and Values section explains what an attribute is according to the JPS. The Attribute Roles section describes the type of attributes. The Attribute Sets section describes how the attributes are collected into sets. The Standard Attributes section describes some of package. the more-commonly used attributes defined in the

Attribute Categories and Values


Every printer has a set of capabilities, such as the ability to print on different paper sizes or the ability to print more than one copy. Each of the capabilities has a range of values. For example, a printer's orientation capability might have this range of values: [landscape, portrait]. For each print request, the capability is set to one of these possible values. The Java Print Service API uses the term attribute category to refer to a printer capability, such as orientation, and the term attribute value to refer to the value of the capability, such as landscape.

dradnats.etubirtta.tnirp.xavaj

dradnats.etubirtta.xavaj

dradnats.etubirtta.tnirp.xavaj

etubirtta.tnirp.xavaj etubirtta.tnirp.xavaj

11

In the Java Print Service API, an attribute category is represented by a Java class interface, and attribute values are instances of such a class or implementing the one of its subclasses. For example, to print 5 copies of a job, an application constructs an class with the value of 5 and uses the instance to specify the instance of the class represents the attribute category, and the instance print job. The represents the attribute value. Because the attribute value is encapsulated as a Java object, the attribute value implies its attribute category, and so an attribute value is usually referred to simply as an attribute. The category and value constitute a key-value pair, which you can add to a set along with other attributes. See Attribute Sets for more information

To determine what attribute categories and values are supported by a printer, use the query interface. A print request does not need to specify methods defined in the attribute values for all of the target printers supported attribute categories: each supported attribute category has a default value. For example, a printer might support printing up to 999 copies of a job, but if a print request does not specify the number of copies, the printer almost always prints a default of just 1 copy.

Attribute Roles
When submitting a print job to a printer, the client provides the attributes describing the characteristics of the print data, such as the document name, and how the print data should be printed, such as double-sided, five copies. If a print job consists of multiple pieces of print data, different pieces might have different processing instructions, such as 8 x 11 inch media for the first document, and 11 x 17 inch media for another document. Once the printer starts processing the print job, additional information about the job becomes available, which might include: the job state (such as completed or queued) and the number of pages printed so far. These pieces of information are also attributes. Attributes can also describe the printer itself, such as: the printer name, the printer location, and the number of jobs queued. The Java Print Service API defines these different kinds of attribute roles with five : subinterfaces of

etubirttAtseuqeRtnirP

is used by an application to represents a setting applied to an entire print job and to specify how the entire print job should be printed. is used by an application to specify a characteristic of a single document and the print job settings to be applied to the document.

yrogetaCteg yrogetaCteg

emaNteg

ecivreStnirP

gnirtS

etubirttA

yrogetaCteg

etubirttAcoD

emaNteg etubirttA

The

interface defines just two methods: and . The method returns a name for the attribute category. The method returns the class that first implements that category. To ensure that all implementations of a particular category report the same name and class, subclasses should not override the or methods.

Chapter 2

seipoC

seipoC

etubirttA

seipoC seipoC

emaNteg

Attributes

12

Many attributes can be members of more than one role. For example, the Media attribute belongs to the doc, print job, and print request roles because the Media attribute can describe the paper size, the paper tray, or paper type. In fact, most doc attributes are also request attributes, and all request attributes are also job attributes. The next section describes how to collect attributes together into attribute sets, which also have roles.

Attribute Sets
A client usually needs to provide more than one processing instruction when submitting a print job. For example, the client might need to specify an A4 media and a landscape orientation. To send more than one processing instruction to the printer, the client collects the attributes representing the instructions into an attribute set, which the Java Print Service interface. API represents with the The interface is similar to the interface: it provides a map of keys to values, in which each key is unique and can contain no more than one value. interface is designed to specifically support the needs of the However, the requires that: Java Print Service API. An corresponds to a category, and the value of the key can Each key in an only be one of the attribute values that belong to the category represented by the key. , an restricts the possible values of a key: an attribute Thus, unlike a category cannot be set to an attribute value that does not belong to that category. Only attributes implementing the interface can be added to the attribute set. . For example, Only one attribute from a particular category can exist in an if an attribute set already contains a Media attribute, adding another Media attribute overwrites the first one.

Chapter 2

teSetubirttA

Like a single attribute, an attribute set has a role. The roles are defined by the subinterfaces, which parallel the subinterfaces mentioned in the Attribute Roles subinterfaces are: , section. The

teSetubirttA

teSetubirttAtseuqeRtnirP

paM.litu.avaj

etubirttA

etubirttA

teSetubirttA teSetubirttA

teSetubirttA

teSetubirttA

etubirttAseulaVdetroppuS detroppuSseipoC

teSetubirttA

etubirttAseulaVdetroppuS

etubirttAecivreStnirP

teSetubirttA

etubirttAboJtnirP paM teSetubirttA

is used by a print service to report how a job is being printed. These values will usually be identical the requested attribute values. However, if the printer does not support the value of a print request attribute then the corresponding print job attribute contains a different value that is supported by the printer is used by a print service to report the status of the print service. is used by a print service o indicate the range of values supported for a request attribute. For example, a printer might support only a certain range of copies. When a print service is queried for the supported range of copies, it returns this object, which implements information in a .

Attributes

13

, , and . The role of the attribute set determines what kind of attributes it contains: a print request attribute set can contain only print request attributes.

as a concrete implementation of the The Java Print Service API includes interface, but developers can provide their own implementation. Rather than using HashAttributeSet directly, applications will more often use one of the subclasses of , which are: , , , and . These subclasses implement the corresponding subinterfaces. For example extends , and is the concrete implementation of . An attribute set created with or any implementation of can contain only request attributes. Unlike its subclasses, a can contain any kind of attribute. An application directly when it needs a set to contain more than one kind of uses a method of attribute. One example is the set returned by the . The unsupported attributes returned might belong to more than one attribute role. As with single attributes, applications will more often create request attribute sets because specifying print requests is the printing applications primary function. To create a request attribute set, use one of the constructors from the HashPrintRequestAttributeSet class:
;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP

teSetubirttAtseuqeRtnirP teSetubirttAtseuqeRtnirPhsaH teSetubirttAtseuqeRtnirP teSetubirttAtseuqeRtnirPhsaH teSetubirttA teSetubirttAtseuqeRtnirP teSetubirttA teSetubirttAecivreStnirPhsaH teSetubirttAtnemucoDhsaH teSetubirttAboJtnirPhsaH teSetubirttAtseuqeRtnirPhsaH teSetubirttAhsaH

The next section describes some of the more commonly-used attributes that you can add to a set.

How to Specify Attributes


Whether you are printing or streaming data, you specify attributes in the same way. You will usually use PrintRequestAttribute more often than the other kinds of attributes because print request attributes specify the settings for an entire print job. Because you will most likely want to specify more than one attribute for a particular job, you need to create an attribute set. This sample demonstrates creating a PrintRequestAttributeSet that instructs the service to use A4 media to print five copies of the job using both sides of the paper:

teSetubirttAecivreStnirP
Chapter 2

setubirttAdetroppusnUteg

;)XELPUD.sediS(dda.tesa ;)4A_OSI.eziSaideM(dda.tesa ;))5(seipoC wen(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP

teSetubirttAhsaH

teSetubirttAcoD teSetubirttAboJtnirP teSetubirttAhsaH teSetubirttAhsaH teSetubirttA ecivreStnirP

Attributes

14

After creating an attribute set, you will pass it to the print jobs print method, along with the . The Specifying Document Types discusses using . The Printing and Streaming Documents chapter discusses how to print. The next section explains some of the more commonly-used attributes in more detail.
rovalFcoD rovalFcoD

Standard Attributes
The package enumerates all of the standard attributes in the Java Print Service API. Most of these standard attributes are taken from the attributes defined in the IETF Internet Printing Protocol (IPP) 1.1 specification. This means that each IPPcompliant attribute class(category) defined in package corresponds to an IPP attribute category, and the name (as returned by ) is the actual IPP name for the category. The class names also usually reflect the IPP name as closely as the coding conventions of the Java programming language permit. Furthermore the values defined for a category are the same as the IPP values. IPP compatibility of each attribute category is documented in the API specification. This section describes the attributes that developers will probably use most frequently. The more commonly-used attributes, including the ones listed here, implement because printing applications will usually specify how an entire print . job should be printed, which is the role of a
etubirttAtseuqeRtnirP etubirttAtseuqeRtnirP emaNteg dradnats.etubirtta.tnirp.xavaj dradnats.etubirtta.tnirp.xavaj detseuqeRnoitatneirO

OrientationRequested
The attribute category allows you to specify the orientation of the , , imaging on the paper. The possible attribute values are: , and . is usually the default value. This code snippet demonstrates adding an OrientationRequested attribute to a set: The object is a type-safe enumeration encapsulating values, which correspond to the possible orientations. These values are the actual IPP keywords. Some pre-formatted document types, such as "Postscript", might not be able to support this attribute category because pre-formatted document types embed printer language commands that are interpreted by the printer, and these commands take precedence over a client request. Clients can discover the supported orientation values for a particular print service by calling: This method returns an array of type values.
.)... ,ssalc.detseuqeRnoitatneirO(seulaVetubirttAdetroppuSteg.ecivreStnirP gnirtS TIARTROP.detseuqeRnoitatneirO EPACSDNAL_ESREVER TIARTROP_ESREVER EPACSDNAL TIARTROP detseuqeRnoitatneirO detseuqeRnoitatneirO ;)TIARTROP_ESREVER.detseuqeRnoitatneirO(dda.tesa
Chapter 2

enumerating the supported

Attributes

15

Copies
seipoC llun
16

The attribute category allows you to specify the number of copies to print. The class encapsulates an integer representing the number of copies requested. This code snippet demonstrates adding a Copies attribute, set to five copies, to an attribute set: Clients can discover the range of copies that a print service supports by calling :
;))5(seipoC(dda.tesa seipoC

This method returns a object, which encapsulates a range of integer values representing the range of copies that the service can handle. Calling with instead of always returns because the object does not implement the interface, attribute in a print request. and so a client cannot specify a This code sample demonstrates discovering if a print service supports printing 5 copies and adding a Copies attribute with a value of 5 copies to an attribute set:
etubirttAtseuqeRtnirP seipoC detroppuSseipoC detroppuSseipoC detroppuSseipoC detroppuSseipoC seulaVetubirttAdetroppuSteg

Media
Media is the IPP attribute that identifies the medium on which to print. The Media attribute is an important attribute to understand, butis relatively complex. to reflect the The Java Print Service API defines three subclasses of the abstract class , and . overloaded Media attribute in the IPP specification: subclasses have the Media category, for which each subclass defines different All the standard attribute values. For example, a MediaTray object can specify a value of MANUAL for the Media attribute to indicate that the document must be printed on paper from the printers manual tray. This code snippet demonstrates adding a Media attribute to a set:
yarTaideM emaNaideM emaNeziSaideM aideM ;)LAUNAM.yarTaideM(dda.tesa aideM

} ... { esle } ;))5(seipoC wen(dda.teSrttAtseuqer { ))5(sniatnoc.ppuSpoc && llun =! ppuSpoc( fi ;)llun ,llun ,ssalc.seipoC(seulaVetubirttAdetroppuSteg.ecivres )detroppuSseipoC( = ppuSpoc detroppuSseipoC

)... ,ssalc.seipoC(seulaVetubirttAdetroppuSteg.ecivreStnirP
Chapter 2 Attributes

The value of the Media attribute is always a String, but because the attribute is overloaded, its value determines the type of media to which the attribute refers. For example, the IPP predefined set of attribute values include the values a4 and top-tray. If Media is set to the value a4 then the Media attribute refers to the size of paper, but if Media is set to toptray then the Media attribute refers to the paper source. Because the String attribute value can refer to such diverse types of media, an application can extend the attribute set to include values such as company-letterhead or yellow letter paper. Of course, to extend the Media attribute in this way, an application must discover a print service that is configured to print with this media. or . The In most cases, applications will use either class enumerates the media by size. The class enumerates the paper trays on a printer, which usually include a main tray and a manual feed tray. The IPP 1.1 specification does not provide for specifying both the media size and the media tray at the same time, which means, for example, that an application cannot request size A4 paper from the manual tray. A future revision of the IPP specification might provide for a way to request more than one type of media at a time, in which case the JPS API will most likely be enhanced to implement this change. The JPS API also includes two additional media-related Attribute classes that are not IPP and . attributes:
aerAelbatnirPaideM eziSaideM emaNeziSaideM
Attributes

MediaSize
is not a request attribute; it is an enumeration of paper dimensions and a mapping instances. Each instance usually has a object to associated with it so that clients can obtain the dimensions of the paper that the instance defines. To determine the dimensions of the instance, call:
emaNeziSaideM eziSaideM ;)repap(emaNroFeziSaideMteg.emaNeziSaideM = ezis eziSaideM emaNeziSaideM emaNeziSaideM eziSaideM

MediaPrintableArea
is used in a print request in conjunction with a compatible Media to specify the area of the paper on which to print. Printer hardware usually defines the printable area of a page, which is rarely the entire page. For this reason, an application needs to determine what printable area a printer defines for a particular size media to ensure that the print data can fit within this area. For example, to determine the supported printable area for 5" x 7" paper, the application needs to choose a media size attribute value that corresponds to this size paper and then query the print service with the media size:
aerAelbatnirPaideM

;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP

yarTaideM

yarTaideM emaNeziSaideM

emaNeziSaideM

Chapter 2

17

The returned value indicates the largest printable area that can be supported by that printer for that paper size.

Destination
The Destination attribute allows you to redirect your print data to a file rather than sending it to a printer device. The print-to-file option is very common in user dialogs, but the spooled data is not always usable because it might be a device-specific raster that can only be interpreted by the device from which it was redirected. For this reason, the Java Print Service API requires that the client query the print service to determine if it can redirect the output to a file. The service might not support the category at all, or it might support only particular values. For example, since the JPS API can be used in an network environment, in which the formatting of print data does not occur on the host computer, specifying a local file for output might not be possible because the service formatting the data might not have access to the local filesystems of the client. The Destination attribute takes a URL as the value of the destination, which allows a network printer to use a protocol such as ftp to upload formatted print data. However, most printers that support this attribute will run as part of a local environment and can accept the file: protocol URL. This code snippet redirects the output to a file called out.prn on the c: drive:

SheetCollate
The SheetCollate attribute allows you to specify whether or not your print job is collated when you are printing more than one copy of a multi-page document. For example, the pages of a 3-page, 2-copy collated job will print as (1,2,3,1,2,3), but the pages of the same document submitted in a 2-copy uncollated job will be printed as (1,1,2,2,3,3). This attribute is not in version 1.1 of the IPP specification, but it is very useful and most printers support it. This code snippet demonstrates specifying a collated job:
;)DETALLOC.etalloCteehS(dda.tesa

;)tesa ,llun ,ssalc.aerAelbatnirPaideM(seulaVetubirttAdetroppuSteg .ecivres)aerAelbatnirPaideM( = aerAelbatnirp aerAelbatnirPaideM ;)7X5_AN.emaNeziSaideM(dda.tesa ;))nrp.tuo\:c:elif(noitanitseD wen(dda.tesa


Chapter 2 Attributes 18

Sides
Some printers, particularly high-end printers, can print on both sides of the paper. The Sides attribute allows applications to specify two-sided printing instead of the usual default of single-sided printing. Two-sided printing is sometimes referred to as "duplex" or "tumble" printing. These two values are differentiated by the orientation of the output. The Java Print Service API refers to duplex as "two sided long edge" and tumble as "two sided short edge". Read the API specification for Sides for further explanation. This code snippet specifiesthat a job print the documents two-sided:
;)XELPUD.sediS(dda.tesa

Fidelity
The Fidelity attribute is an IPP boolean attribute that represents whether or not a print job should be rejected if the print service does not support any attribute specified in the print request. Fidelity is not an attribute many developers will need to consider, but it is an important attribute in the context of the JPS API. The default value is FIDELITY_FALSE, which indicates that a print job should not be rejected if the print service does not support an attribute specified in the print request. For example, if an application specifies an orientation of reverse landscape, but the printer does not support reverse landscape, the job is rejected if fidelity is true, but if fidelity is false then the printer might substitute a reasonable alternative, such as the landscape orientation. The Fidelity attribute allows applications to decide whether to only print the document exactly as specified or to print it even though the printer might not support all attributes. This code snippet specifies that a job should be rejected if the printer does not support the requested attributes: For the cases in which fidelity is important, the Java Print Service API provides many tools for applications to query exactly what can be supported for a particular print request. See the various query methods on the PrintService interface.
;)EURT_YTILEDIF.ytilediF(dda.tesa

Uses of JPS Attributes


Unlike attributes of previous Java printing APIs, the Java Print Service attributes can be used in both the program and from the user dialog. Applications can specify as little as needed about how a job should be printed and can inspect the selections made by end users in user dialogs.

Chapter 2

Attributes

19

The JPS attributes are designed to be extensible. An AttributeSet can contain anything that properly implements the Attribute interfaces, which means that the set of standard attributes can evolve and still be compatible with future SDK versions, and implementors of services can extend attributes in two ways: Using their own subclasses of a standard attribute category to support additional values of that category. Creating new attribute categories. Demonstrating attribute usage from the user dialog requires providing a user interface, which is beyond the scope of this user guide. Likewise, extending JPS attributes is a task for service providers, and is not included as an example in this version of the user guide. However, the Printing a Document chapter explains a simple printing application, which covers the basics of using the JPS attributes as described in this chapter.

Chapter 2

Attributes

20

CHAPTER

Specifying Document Types


From the users perspective, a document can take many different forms, including: a PDF file, an image from a digital camera, an email, a word processor document, or a web page. Before printing a document of a particular format, the client needs to ensure that the printer can understand the format. Sometimes a printer can directly print documents of a given format: photo printers can directly print images of various formats, and PostScriptTM printers can directly print a PostScriptTM document. However, there are few printers that can directly print a wide range of formats; most printers require some higher-level software support to translate the source document into a format they can print. A printing API needs to provide a way to describe document types so that: The printer can report what formats it can print. The client can describe the format of the data it wants to print. The client can describe the encoding of text data.

A MIME type that tells the printer how to interpret the data. A representation class name indicating the Java class that describes how the data is sent to the printer.

with To describe an HTML page to a print service, a client might choose to use a a MIME type string of text/html; charset=utf-16 and a representation class name of in one of two ways: java.io.InputStream. The client can obtain this

Use the pre-defined instance that represents this type of : . The Java Print Service API provides a set of pre-defined instances for common doc flavors as a convenience. Because the HTML page contains text data, the MIME type String includes the text encoding, which is charset=utf-16 in this example. The client is responsible for accurately describing the print data to the print service. The section, Client-Formatted Print Data,

61_FTU_LMTH_TXET MAERTS_TUPNI.rovalFcoD rovalFcoD ;)"maertStupnI.oi.avaj" ,"61 ftu=tesrahc ;lmth/txet"(rovalFcoD wen = rovalFmaertSlmth rovalFcoD rovalFcoD

Construct a

rovalFcoD

rovalFcoD

The JavaTM Print Service API describes document types using the is comprised of:

class. A

rovalFcoD

rovalFcoD

21

explains how to properly construct a DocFlavor to accomplish this. If the text encoding is not included in the MIME type, unexpected results can occur, as explained in the section, Importance of Character Encoding. The client can allow the service to determine the format of data that the client supplies as a Java object. The Service-Formatted Print Data section describes using a DocFlavor to represent service-formatted print data. API has a pre-declared doc flavor, this Keep in mind that, just because the doesnt mean that an implementation of the particular flavor is available. For example, even if you use the pre-defined DocFlavor representing HTML text in UTF-16, you wont be able to print the HTML unless you have a print service that supports printing HTML. Again, it is the users responsibility to ensure that a printer supports a particular format.

Client-Formatted Print Data


The client uses the to describe the print data format and to indicate how the data will be delivered to the print service. The MIME type specifies the data format. The representation class name specifies how the data will be delivered to the printer. class has an inner class for each of the common representation class names. The object constants representing formats that can Each inner class contains a set of be delivered using the representation class. For example, an input stream can stream many different kinds of print data formats, including GIF, represented by , and PostScriptTM, represented by . If one of the object constants accurately describes the format of a given piece of print data, the client can use this constant . instead of constructing a If the appropriate is not already defined in the JPS API, a client can create it with constructor. The client can use the MIME types and representation class the . names explained in the next two sections to construct the

MIME Types for Preformatted Data


The four most common types of preformatted data are: text data, page description language documents, image data, and autosense print data. This section describes each one of these data types and lists their corresponding MIME types.

Chapter 3

rovalFcoD

rovalFcoD

r o va l F co D

rovalFcoD

TPIRCSTSOP.MAERTS_TUPNI.rovalFcoD FIG.MAERTS_TUPNI.rovalFcoD

rovalFcoD

rovalFcoD

rovalFcoD

rovalFcoD

rovalFcoD

Specifying Document Types

22

Text Data
Preformatted text data is usually provided in a character-oriented representation class, such as a character array, String, or Reader, or in a byte-oriented representation class, such as a byte array, input stream, or URL. Plain text and HTML are two common forms of preformatted text data. You can use these MIME type strings to represent the format of the : data when constructing a

"text/plain" "text/plain;charset=xxx" "text/html" "text/html;charset=xxx"

Page Description Language Documents


Preformatted page description language (PDL) documents are usually provided in a byteoriented representation class, such as byte array, InputStream, or URL. You can use these MIME type strings to represent the format of the data when constructing a DocFlavor:

"application/pdf" "application/postscript" "application/vnd.hp-PCL"

Image Data
Preformatted image data is provided in a byte-oriented representation class: byte array, InputStream, or URL. You can use these MIME type strings to represent the format of the : data when constructing a

"image/gif" "image/jpeg" "image/png"

rovalFcoD rovalFcoD

Plain text in the default character set US-ASCII Plain text in character set xxx HyperText Markup Languge in the default character set (US-ASCII) HyperText Markup Languge in the character set xxx

Portable Document Format document PostScriptTM document Printer Control Language document

Graphics Interchange Format image Joint Photographic Experts Group image Portable Network Graphics image

Chapter 3

Specifying Document Types

23

Autosense Print Data


Preformatted autosense print data allows the printer to decide how to interpret the print data. This type of data is usually provided in a byte-oriented representation class. You can use this : MIME type string to represent the format of the data when constructing a

"application/octet-stream"

Representation Classes
For client-formatted print data, the print data representation class is usually one of the following:

Character array (char[] )

The print data consists of the Unicode characters in the array. This representation class can provide text data and PDL data. The print data consists of the Unicode characters in the string. This representation class can provide text data. The print data consists of the Unicode characters read from the stream up to the end-of-stream. This representation class can provide text data. The print data consists of the bytes in the array. The bytes are encoded in the character set specified by the doc flavor's MIME type. If the MIME type does not specify a character set, the default character set is US-ASCII. This representation class can provide text data, PDL documents, and image data. The print data consists of the bytes read from the stream up to the end-of-stream. The bytes are encoded in the character set specified by the doc flavor's MIME type. If the MIME type does not specify a character set, the default character set is US-ASCII. This representation class can provide text data, PDL documents, and image data. The print data consists of the bytes read from the URL location. The bytes are encoded in the character set specified by the doc flavor's MIME type. If the MIME type does not specify a character set, the default character set is US-ASCII. This representation class can provide text data, PDL documents, and image data. To print documents to a network print service that might not have access to the URL, open an input stream on the URL and use an input stream data flavor instead.

A Character stream represented by

Byte array (byte[])

A Byte stream represented by

Uniform Resource Locator,

Chapter 3

Specifying Document Types

rovalFcoD

maertStupnI.oi.avaj

gnirtS.gnal.avaj redaeR.oi.avaj LRU.ten.avaj

24

Importance of Character Encoding


If a MIME type for byte print data does not include a charset parameter, indicating the encoding, the Java Print Service assumes a character set of US-ASCII. This behavior is different than that of the Java runtime, which always assumes the default encoding for the users locale on the underlying operating system, which might be different than US-ASCII. Every instance of the JavaTM Virtual Machine1 has a default character encoding determined during virtual-machine startup that usually depends upon the locale and charset used by the underlying operating system. In a distributed environment, two JVM's might not share the same default encoding. Thus clients who want to stream platform-encoded text data from the host platform to a Java Print Service instance must explicitly declare the charset and not rely on defaults. For these reasons, applications that stream text data should always specify the charset in the MIME type. To specify a MIME type, an application needs to determine the host platforms method. The MIME type encoding, which it does by calling the returned from this method is guaranteed to be understood by the current JVM. See character encodings for more information on the character encodings supported on the Java platform.

Service-Formatted Print Data


Rather than the client describing the print data format with a MIME type, the client can supply a Java object from which a print service determines the print data format. For example, the Java object can encapsulate a PostScriptTM document. Instead of the client explicitly describing the document as PostScriptTM with a MIME type, the client can wrap a reference to the document in a Java object, which the print service inspects to determine that the format is PostScript TM. Because the print data is delivered to the print service through the Java object, the class that the Java object implements is the representation class. The class has an inner class called that object constants representing service-formatted print data. Each of these contains , which constants has a MIME type of indicates that the client will supply a reference to a Java object implementing the interface named as the representation class. object constants contained in each The three represent a common representation class name used with service-formatted print data:
1. As used in this guide, the terms Java Virtual Machine or JVM mean a virtual machine for the Java platform

Chapter 3

DETTAMROF_ECIVRES.rovalFcoD

DETTAMROF_ECIVRES.rovalFcoD

fertcejbo lacol mvj avaj x/noitacilppa

gnidocnEtsoh.rovalFcoD

rovalFcoD

rovalFcoD rovalFcoD

Specifying Document Types

25

As stated in the Introduction, applications that wrap 2D graphics into a Java object can use constants. See Printing and Streaming 2D the service-formatted print data Graphics for more information.
rovalFcoD

1. A client determines the format of the print data and decides how to present the data to the representing the format: printer. The client then creates or obtains a

2. The client uses the to find printers that can understand the format specified by and has the capabilities specified in the clients attribute set: the

4. The client creates a Doc representing the document to be printed:

Chapter 3

Specifying Document Types

boJtnirPcoD

3. From a print service, the client creates a print job, represented by a

;)tesa ,rovalf(secivreStnirPpukool.pukooLecivreStnirP = secivres ][ecivreStnirP

;)(boJtnirPetaerc.]0[secivres = boJtnirp boJtnirPcoD

;)rovalf ,fig.ekud(coDmaertStupnI wen = cod coD

;FIG.MAERTS_TUPNI.rovalFcoD = rovalf rovalFcoD

rovalFcoD

rovalFcoD

These steps illustrate the role of the

rovalFcoD

How to Use

in a typical application:

elbatnirP.tnirp.twa.avaj

elbaegaP.tnirp.twa.avaj

EGAMI_ELBAREDNER.DETTAMROF_ECIVRES.rovalFcoD

egamIelbaredneR.elbaredner.egami.twa.avaj

ELBATNIRP.DETTAMROF_ECIVRES.rovalFcoD

ELBAEGAP.DETTAMROF_ECIVRES.rovalFcoD rovalFcoD rovalFcoD

: interface, The client supplies an object that implements the which represents a set of pages to be printed. The printer calls methods in that interface to obtain the pages to be printed, one by one. For each page, the printer supplies a graphics context and prints whatever the client draws in that graphics context. : interface, The client supplies an object that implements the which is responsible for drawing the contents of each page. The printer calls methods in that interface to obtain the pages to be printed, one by one. For each page, the printer supplies a graphics context and prints whatever the client draws in that graphics context. : The client supplies an object that implements interface , which represents an image that can be manipulated in a rendering-independent manner and can be rendered to various contexts, such as a printer, without compromising quality. The printer calls methods in the interface to obtain the image to be printed.

object:

26

When the client passes the object to the print method, the object object and determines the doc flavor that the client can obtains the print data from the to obtain a supply. The doc flavor's representation class is a conduit for the sequence of characters or bytes from the client. The Printing and Streaming Documents chapter demonstrates a complete printing application.

Chapter 3

Specifying Document Types

boJtnirPcoD

5. The client prints the document by calling the print method of the

object:

boJtnirPcoD

boJtnirPcoD

coD coD

;)tesa ,cod(tnirp.boJtnirp
27

CHAPTER

Printing and Streaming Documents


Before submitting a print job, an application needs to locate print services that have the capability to handle the print data. Once the appropriate service is located, the application obtains a print job from it and submits the print job to the service. This chapter demonstrates how to print and stream documents by: Locating appropriate services using the elements presented in the two previous chapters: Attributes and Document Flavors Creating a print job Submitting the print job to the printer or service Registering for events on the print job or service.

e ci v re S t ni r Pm a er t S e ci v r eS t ni r Pm a e rt S ecivreStnirP e c iv r eS t n ir P ma e r tS e c i vr e St n i rP ec i vr e S tn i rP e ci v r eS t ni r P ma e rt S ec i vr e S tn i rP e c i vr e St n ir P m ae r tS e ci v r eS t ni r Pm a e rt S ecivreStnirP
After locating a service, you obtain a print job and submit the print job to the service in the or a StreamPrintServie. The major same way whether you are using a and are in the way they are differences between located. The next section discusses locating both print services and stream print services.

The Java TM Print Service API includes a class and a class. A extends , and so a can be used anywhere a can be used. However, and are used for different purposes. A is used to direct is used to export formatted print data to a output to a printer; a , you specify stream, usually to a different format. When locating a the required output format in the form of a MIME type argument and provide an to receive the data. You do not provide a representation class when locating , as you do when locating a , because the output is a . always delivered to an

e ci v r e S tn i r P

Versus

ec i vr e S tn i rP

e c iv r eS t n ir P ec i vr e S tn i rP m a er t S e c iv r e St n ir P

e c iv r e S t ni r P m a e rt S
m a e rt S tu p t uO e c iv r eS t n ir P ma e rt S m a e rt S tu p t uO

28

Locating Services
An application locates a print service in a slightly differently way from locating a stream print service. The Discovering Print Services section explains locating print services. The Discovering Stream Print Services section explains locating stream print services.

Discovering Print Services


Before sending a print job to a printer, the application needs to find printers that have the capabilities to handle the print job. To print a double-sided document, the application first needs to find printers that have the double-sided printing capability. class included in the Java Print Service API The provides static methods that applications use to locate printers. :

This method returns an array of print services representing printers that have the capabilities specified in the attribute set and can print the data format specified in the doc flavor. See the Attributes chapter and the Specifying Document Types chapter for more help in choosing a and creating an . The base set of printers returned from the method are the same as the set of printers returned by the platform. For example, when using Windows NT, the set of returned printers is the same as the set of printers visible in the Windows Printer Control Panel. Likewise, when using Solaris, the returned printers are the same as those enumerated by the System V Unix "lpstat" command. However, since third parties can augment these sets, additional printers, such as JINI printers, can be returned. , the application can access its many query methods After obtaining a suitable to determine what values are supported for attribute categories. The Obtaining a Print Job . section explains how to get a print job from the

Chapter 4

Printing and Streaming Documents

pukooLecivreStnirP

;)tesa ,rovalf(secivreStnirPpukool.pukooLecivreStnirP = ecivres ][ecivreStnirP ;))2(seipoC wen(dda.tesa ;)4A_SOI.emaNeziSaideM(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP ;TPIRCSTSOP.MAERTS_TUPNI.rovalFcoD = rovalf rovalFcoD

secivreStnirPpukool

ecivreStnirP

teSetubrttA secivreStnirPpukool teSetubirttA ecivreStnirP

An application invokes the and an

pukooLecivreStnirP.tnirp.xavaj

method of

with a

rovalFcoD rovalFcoD

29

Discovering Stream Print Services


An application uses stream print services to convert print data to different formats. This is useful, for example, if you do not have a printer that can print the format of a particular piece of print data. This section demonstrates how to convert a GIF document into Postscript using . a The class has a method for method in locating stream print services. This method, like the , takes a that represents the type of the input document--in this . Unlike , this method also takes a case, MIME type. This MIME type represents the format of the output. Since this example converts GIF to postscript, the MIME type is application/postscript. The DocFlavor class method for returning the MIME type from a . The has the method returns an array of objects, which are factories for instances. This code sample objects that can return demonstrates obtaining an array of objects able to convert a GIF image into PostScript:
;)epyTemiMsp ,rovalf (seirotcaFecivreStnirPmaertSpukool.yrotcaFecivreStnirPmaertS = seirotcafsp ][yrotcaFecivreStnirPmaertS ;)(epyTemiMteg.TPIRCSTSOP.YARRA_ETYB.rovalFcoD = epyTemiMsp gnirtS ;FIG.MAERTS_TUPNI.rovalFcoD = rovalf rovalFcoD secivreStnirPpukool seirotcaFecivreStnirPmaertSpukool ecivreStnirPmaertS secivreStnirPpukool ecivreStnirPmaertS yrotcaFecivreStnirPmaertS ecivreStnirPmaertS seirotcaFsecivreStnirPmaertSpukool epyTemiMteg FIG.MAERTS_TUPNI.rovalFcoD rovalFcoD pukooLecivreStnirP yrotcaFecivreStnirPmaertS maertStuptuO yrotcaFecivreStnirPmaertS ecivreStnirPmaertS

The Java 2 SE SDK V1.4 includes one stream print service that can export Postscript from and interfaces. To verify the graphics calls, such as through the class to try to locate it. The availability of this service, use the Printing and Streaming 2D Graphics chapter discusses streaming 2D graphics. implements , which means you can use a wherever you can use a . The application is responsible for closing the output stream after a job has printed to the stream. Once the stream is closed, the instance can no longer be used.
ecivreStnirP ecivreStnirP ecivreStnirPmaertS ecivreStnirPmaertS ecivreStnirPmaertS yrotcaFecivreStnirPmaertS elbatnirP elbaegaP

Chapter 4

Printing and Streaming Documents

ecivreStnirPteg

The takes an that stream:

object has an instance method called that parameter and creates a instance that writes to

yrotcaFecivreStnirPmaertS rovalFcoD
30

;)sof(ecivreStnirPteg.]0[seirotcafsp = ecivreSsp ecivreStnirPmaertS ;)emanelif(maertStuptuOeliF wen = sof maertStuptuOeliF

Obtaining a Print Job


A print job is a submitted print request and includes one or more pieces of print data and a set of processing instructions. The Java Print Service represents a print job with the object. Whether you are sending print data to a stream or to a printer, you create on the service: a print job in the same way: by calling An application obtains a print job from a service because the service only creates print jobs that are capable of handling data that the particular service can accept.
teSetubirttAtseuqeRtnirP boJtnirPcoD ;)(boJtnirPetaerc.]0[secivresp = jp boJtnirPcoD boJtnirPetaerc boJtnirPcoD

The

interface provides the print method, which takes a parameter and a encapsulating the print data and the doc
coD ;)tesa ,cod(tnirp.jp

flavor:

Creating a Doc
coD

To create a you must provide an implementation of the interface. The Java Print Service API provides a convenient implementation of called . An application is implementation, but to ensure compliance with , any not required to use the implementation must observe the same required semantics that implements, which are: implemenation must implement all five methods of the interface. Every implementation must allow multiple threads to access the object. The method is called, it returns the same object. This means you do not Every time a return a new stream. Since there is only one input stream there can only be one consumer . of the returns a stream for the service if requested The checks if the data type matches the doc flavor The always override those passed in the print The attributes returned from method.
coD coDelpmiS coD coD coDelpmiS coD coD coDelpmiS coD coD coD

Before creating a , you need to load your document from the file. The representation class determines how you load the document from the file. In this case, the of the : representation class is an
;)fig.ogold2avaj(maertStupnIeliF wen = sif maertStupnIeliF maertStupnI coD rovalFcoD

setubirttAteg

coD

The next section discusses creating a

coD

coD coD coD

Chapter 4

Printing and Streaming Documents

31

The next section demonstrates how to register for events on your print job or service.

Registering for Events


The Java Print Service API allows services to report two types of events to applications: printer status updates and print job progress updates. The events API, which includes the package and methods to register listeners on a service and , follows the familiar listener model used in AWT.
boJtnirPcoD tneve.tnirp.xavaj

Print Service Events


Print Service event listeners monitor a services changes in status and report events as changes in the values of print service attributes. An application can monitor events on a print interface service by implementing the as shown in this example: and installing itself as a listener on a
... } detadpu si etubirtta na fi gnihtemos oD // {)e tnevEetubirttAecivreStnirP(etadpUetubirtta diov cilbup ... ;)siht(renetsiLetubirttAecivreStnirPdda.]0[secivresp ... { renetsiLetubirttAecivreStnirP stnemelpmi SPtnirP ssalc cilbup ecivreStnirP renetsiLetubirttAecivreStnirP.tneve.tnirp.xavaj

An application can discover which print service attributes a service supports by using the same query methods it uses to discover which request attributes a service supports. For example, to discover if the service supports the QueuedJobCount attribute an application calls:
;)ssalc.tnuoCboJdeueuQ(detroppuSyrogetaCetubirttAsi.ecivres

renetsiLetubirttAecivreStnirP )(etadpUetubirtta.renetsiLetubirttAecivreStnirP

The attributes change. The service uses the which events it supports.

method is called when print service interface to decide

Chapter 4

Printing and Streaming Documents

coD

See Example: PrintGIF.java for an example of a custom

implementation.

,teSetubirttAcoD
32

;)llun ,rovalf ,sif(coDelpmiS wen = cod coD llun teSetubirttAcoD rovalFcoD coDelpmiS

Once you have the stream, pass it to ifi you have one. If you dont have a

with the , pass in

and a instead:

The service determines how frequently it reports updates on attributes. If many attributes are supported, the service might batch events, which means an application isnt guaranteed to receive a particular event. The delivered event contains only attributes that have changed in value, which means that static service attributes, such as the printer model, will never be reported in an event.

Print Job Events


Most printing clients are more interested in monitoring a print job than monitoring a : services status. A client can install two different kinds of listeners on a and .
boJtnirPcoD renetsiLboJtnirP renetsiLetubirttAboJtnirP renetsiLetubirttAboJtnirP

PrintJobAttributeListener
is similar to the service attribute listener: the job reports changes interface. Usually these attributes are also in attributes that implement the print request attributes and are fixed over the lifetime of a print job. Only a few attributes, , are likely to change. Since few clients are interested in such as this granularity of detail, and even fewer services support this capability, clients will most to monitor a jobs progress. likely use
etubirttAboJtnirP retpadAboJtnirP detelpmoCsteehSaideMboJ renetsiLboJtnirP

PrintJobListener
is easier to use than because it delivers simple messages, such as or . The interface has only six methods, each of which reports a significant but simple piece of information as an event. As a , provides default implementations of these convenience, the adapter class, six methods. is unusual but useful. A client is often One message in particular: interested in knowing if a job has finished or failed. If possible, a service should deliver such "terminal" events, but sometimes the service cannot be sure if the job finished or failed, and a "completed" message is misleading in this case. For example, a job might be spooled to a network print service that has a queue thats not visible. In this case, the no more events message is not sufficient to say that the job has succeeded, but the client can at least infer that it is not known to have failed. The following example demonstrates adding a listener that messages: monitors
{)e tnevEboJtnirP(stnevEeroMoNboJtnirp diov cilbup ... ;)siht(renetsiLboJtnirPdda.jp ... {retpadAboJtnirP sdnetxe SPtnirP ssalc cilbup stnevEeroMoNboJtnirp stnevEeroMoNboJtnirp deliaFboJtnirp detelpmoCboJtnirp renetsiLetubirttAboJtnirP renetsiLboJtnirP
Chapter 4

Printing and Streaming Documents

33

} } ;)e(nltnirp.rre.metsyS { )e noitpecxEtnirP( hctac } ;)e(nltnirp.rre.metsyS { )e noitpecxEOI( hctac } ;)tesa ,cod(tnirp.jp ;)llun ,rovalFsp ,sif(coDelpmiS wen = cod coD ;)sp.elpmaxe(maertStupnIeliF wen = sif maertStupnIeliF { yrt ;)(boJtnirPetaerc.]0[secivresp = jp boJtnirPcoD { )0 > htgnel.secivres( fi ;)tesa ,rovalFsp(secivreStnirPpukool.pukooLecivreStnirP = secivresp ][ecivreStnirP ;)ELPATS.sgnihsiniF(dda.tesa ;)EGDE_GNOL_DEDIS_OWT.sediS(dda.tesa ;)4A_OSI.emaNeziSaideM(dda.tesa ;))2(seipoC wen(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP ;TPIRCSTSOP.MAERTS_TUPNI.rovalFcoD = rovalFsp rovalFcoD

Once you have the and , you can call the objects print method to submit the document to the service. The Submitting the Print Job to the Printer section completes the printing example. The Submitting the Print Job to the Stream section completes the streaming example.

boJtnirPcoD

See Example: PrintPS.java for the complete application.

This section completes the printing application explained in this chapter. This example prints five copies of a PostScript document, double-sided on A4 paper, and stapled.

Submitting the Print Job to the Printer

ereh gnihtemos oD //

Submitting the Print Job

Chapter 4

Printing and Streaming Documents

34

boJtnirPcoD

coD

... }

} ;)e(nltnirp.rre.metsyS { )e noitpecxEtnirP( hctac } ;)e(nltnirp.rre.metsyS { )e noitpecxEOI( hctac } ;)tesa ,cod(tnirp.jp ;)tesa ,rovalFsp ,sof(coDelpmiS wen = cod coD ;)(boJtnirPetaerc.sps = jp boJtnirPcoD ;)sof(ecivreStnirPteg.]0[seirotcaf =sps ecivreStnirPmaertS ;)emanelif(maetStuptuOeliF wen = sof maertStuptuOeliF ;sp.elifwen = emanelif gnirtS ;)fig.ogold2avaj(maertStupnIeliF wen = sif maertStupnIeliF { yrt } ;)0(tixe.metsyS ;)seirotcaf elbatius oN(nltnirp.rre.metsyS { )0==htgnel.seirotcaf(fi ;)epyTemiMsp ,rovalf (seirotcaFecivreStnirPmaertSpukool.yrotcaFecivreStnirPmaertS = seirotcaf ][yrotcaFecivreStnirPmaertS ;)ELPATS.sgnihsiniF(dda.tesa ;)EGDE_GNOL_DEDIS_OWT.sediS(dda.tesa ;)4A.emaNeziSaideM(dda.tesa ;))2(seipoC wen(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP ;)(epyTemiMteg.TPIRCSTSOP.YARRA_ETYB.rovalFcoD = epyTemiMsp gnirtS ;FIG.MAERTS_TUPNI.rovalFcoD = rovalf rovalFcoD

See Example: PrintGIFtoStream.java for the complete application.

This section completes the streaming example explained in this chapter. This example converts a GIF document to PostScript and embeds the specified printing attributes into the PostScript document.

Submitting the Print Job to the Stream

Chapter 4

Printing and Streaming Documents

35

Print Service Providers


Print Service Providers are third parties that write an implementation of the abstract class and install it according to the SPI JAR file specification.

The API is reasonably simple and generally requires implementation of the class, and the interface, and . The should also support the events defined by the Java Print Service API.

How much more code there is depends on the document types that are supported. If a printer wants to be able to print a JPEG image and the printer already has such capability in hardware, its little more than a case of properly spooling the data. However, to print 2D graphics, a class that can turn the graphics calls into a printer-formatted raster is a significant body of work.

Chapter 4

Printing and Streaming Documents

e ci v r eS t ni r P

b oJ t n ir P co D

e c iv r e St n ir P

p u ko o Le c i vr e St n i rP e c i vr e St n i rP p u ko o Le c i vr e St n i rP

This class provides a standardized API for looking up instances of print services. For example an IPP print service provider, a JINI lookup service, or a JNDI-based directory s. service may each be implementations of this class which can return

36

CHAPTER

Printing and Streaming 2D Graphics


The Java 2DTM printing API is the package that is part of the JavaTM 2 SE, version 1.2 and later. The Java 2D printing API provides for creating a , displaying a printer dialog to the user, and printing paginated graphics using the same and classes that are used to draw to the screen. Many of the features that are new in the Java Print Service, such as printer discovery and specification of printing attributes, are also very important to users of the Java 2D printing API. To make these features available to users of Java 2D printing, the package has been updated for version 1.4 of the JavaTM 2 SE to allow access to the Java TM Print Service from the Java 2D printing API. Developers of Java 2D printing applications have four ways of using the Java Print Service with the Java 2D API:
boJtnirPcoD boJtnirPcoD boJretnirP boJretnirP tnirp.twa.avaj boJretnirP tnirp.twa.avaj D2scihparG.twa.avaj scihparG.twa.avaj

Print 2D graphics using Stream 2D graphics using Print 2D graphics using using Stream 2D graphics using

and a service-formatted DocFlavor and a service-formatted DocFlavor

The Using PrinterJob to Print or Stream Graphics section explains the first two methods. The Using Service-Formatted Data section explains the other two methods.

Using PrinterJob to Print or Stream Graphics


Static convenience methods to look up print services that can image 2D graphics, which or objects depending are returned as an array of on the method. on a . Methods to set and get a method that takes a parameter. A method that takes a parameter. A
yrotcaFecivreStnirPmaertS teSetubirttAtseuqeRtnirP teSetubirttAtseuqeRtnirP boJretnirP ecivreStnirP ecivreStnirP golaiDtnirp golaiDegap
37

boJretnirP

tnirp.twa.avaj

The new API in

consists of these new

methods:

Because the new printDialog and pageDialog methods take an attribute set, users can edit the initail attribute settings from the dialogs. to print 2D graphics to a printer or to an output stream. The method returns an array of objects, each of which method represents a printer that can print 2D graphics. The objects, each of which can return a returns an array of . An application uses the to send print data to an to output stream. As with printing documents, applications can use a to submit transcode 2D graphics to other formats. This section discusses using 2D graphics to a printer and to an output stream.
boJretnirP ecivreStnirPmaertS secivreStnirPmaertSpukool ecivreStnirP ecivreStnirPmaertS ecivreStnirPmaertS yrotcaFecivreStnirPmaertS boJretnirP

Applications can use

Printing 2D Graphics
The new , , and methods allow an application to initialize print settings and pass these settings to a dialog so that a user can update the settings before submitting the print request, as demonstrated by this code sample:
} } :)ep(nltnirp.rre.metsyS { ))ep(noitpecxEretnirP( hctac } } ;)tesa(tnirp.jp .tseuqer tnirp eht otni sgnittes lanif eht ssaP :5 petS // .sgolaid eht ni resu eht yb edam sgnittes eht etadpU :4 petS // { ))tesa(golaiDtnirp.jp( fi ;)tesa(golaiDegap.jp .golaid tnirp dna golaid egap a ot sgnittes eht ssaP :2 petS // ;)]0[ecivres(ecivreStnirPtes.jp { yrt ;)]0[secivres + :retnirp detceles(nltnirp.tuo.metsyS { )0 > htgnel.secivres( fi ;)(secivreStnirPpukool.boJretnirP = secivres][ ecivreStnirP .secivres tnirp dniF :3 petS // ;)(boJretnirPteg.boJretnirP = jp boJretnirP .boj tnirp a niatbO :2 petS // ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP .sgnittes tnirp laitini pu teS :1 petS // tnirp golaiDtnirp golaiDegap

See Example: Print2DPrinterJob.java for the complete application.

teSetubirttAtseuqeRtnirP
Chapter 5

secivreStnirPpukool

tnirp

method that takes a

parameter.

Printing and Streaming 2D Graphics

38

Note that Step 4 in this code sample does not seem to correspond to any particular line of code. In fact, the user updates the print settings in the dialogs, and the updated settings are , aset. saved in the One problem with using Java 2D and the Java Print Service together is that some attributes, such as number of copies, are defined in both APIs. If such an attribute is specified in a , it takes precedence over the same attribute specified in the . Note that if a user updates the number of copies in a print dialog, the is automatically updated to reflect that, which reconfirms the existing behavior. specification also overlaps with the Java Print Service , , and attributes. If an application uses the interface and the method, the media, orientation, and imageable area attributes contained in the attribute set are added to a new , which is passed to the print method of the object. If an application interface, the does not change. uses the
aideM elbatnirP )teSetubirttAtseuqeRtnirP(tnirp detseuqeRnoitatneirO tamroFegaP elbatnirP aerAelbatnirPaideM tamroFegaP elbaegaP tamroFegaP boJretnirP teSetubirttAtseuqeRtnirP teSetubirttAtseuqeRtnirP

The

Streaming 2D Graphics
An application can also use a and a to send print data to an output stream. This example is similar to the example in the previous section, except a is used in place of a :
ecivreStnirPmaertS ecivreStnirP boJretnirP ecivreStnirPmaertS

Chapter 5

Printing and Streaming 2D Graphics

boJretnirP
39

} ;)tesa(tnirp.boj ;))2(seipoC wen(dda.tesa ;)(teSetubirttAtseuqeRtnirPhsaH wen = tesa teSetubirttAtseuqeRtnirP .retnirp siht yficeps ot stnaw ppa fi // ;)]0[ecivres(ecivreStnirPtes.boj } } { )e noitpecxEdnuoFtoNeliF( hctac } boJretnirP a no ecivres eht sa tes eb won nac retnirPsp // ;)sof(ecivreStnirPteg.]0[seirotcaf = retnirPsp ;)"sp.tuo"(eliF wen = maertstuo { yrt { )0 > htgnel.seirotcaf( fi ;)epyTemiMsp(secivreStnirPmaertSpukool.boJretnirP = seirotcaFsps][ yrotcaFecivreStnirPmaertS ;retnirPsp ecivreStnirPmaertS ;maertstuo maertStuptuOeliF ;"tpircstsop/noitacilppa" = epyTemiMsp gnirtS ;)(boJretnirPteg.boJretnirP = boj boJretnirP

Using Service-Formatted Data


You can print or stream 2D graphics encapsulated in a or object using a and a service-formatted .A can represent any kind of data, or interface is a Java including Java objects. An implementation of the object. As discussed in the Specifying Document Types chapter, the Java Print Service API object constants for print data in the form of a Java object. includes pre-defined An application can look up print services or stream print services supporting this type of implemetation and submit it to the service in a data, encapsulate the object in a DocPrintJob. The Printing the Service-Formatted Data section demonstrates printing the data. The Streaming Service-Formatted Print Data section demonstrates streaming the data. Registering for events on 2D graphics printing applications using DocPrintJob is done the same way as for document printing applications using DocPrintJob. See Registering for Events for more information.
elbatnirP elbatnirP elbaegaP rovalFcoD elbaegaP elbatnirP rovalFcoD coD rovalFcoD boJtnirPcoD

Printing the Service-Formatted Data


To locate print services that can handle the service-formatted data, pass the appropriate object constant to the lookupPrintServices method: service-formatted
rovalFcoD siht

;)llun ,ELBATNIRP.DETTAMROF_ECIVRES.rovalFcoD ,siht(coDelpmiS wen = cod coD rovalFcoD coDelpmiS

SeeExample: Print2DGraphics.java for the complete application.

Streaming Service-Formatted Print Data


A stream print service can be used to export 2D graphics encapsulated in a Java object to to Postscript: another format. This example exports graphics in a

;)cod(tnirp.jp ;)(boJtnirPetaerc.]0[ecivres = jp boJtnirPcoD boJtnirPcoD

Create the

, and submit it to the service:

Chapter 5

Printing and Streaming 2D Graphics

rovalFcoD coD

The printing application implements the interface. To create the , passing in for the , the service-formatted , and an optional attribute set: the
ataDtnirp elbatnirP

;)llun ,ELBATNIRP.DETTAMROF_ECIVRES.rovalFcoD(secivreStnirPpukool.pukooLecivreStnirP = secivres][ ecivreStnirP

, use constant for

40

} ;)sof(ecivreStnirPteg.]0[seirotcaf = sps ecivreStnirPmaertS ;)sp.tuo(maertStuptuOeliF wen = sof maertStuptuOeliF {yrt } ;)0(tixe.metsyS ;)seirotcaf elbatius oN(nltnirp.rre.metsyS { )0 == htgnel.seirotcaf( fi ;))"tpircstsop/noitacilppa" ,rovalf(seirotcaFecivreStnirPmaertSpukool.yrotcaFecivreStnirPmaertS = seirotcaf][ yrotcaFecivreStnirPmaertS ELBATNIRP.DETTAMROF_ECIVRES.rovalFcoD = rovalf rovalFcoD

See Example: Print2DtoStream.java for the complete application

;)cod(tnirp.)(boJtnirPetaerc.sps ;)llun ,rovalf ,siht(coDelpmiS wen = cod coD

Chapter 5

Printing and Streaming 2D Graphics

41

APPENDIX

Example: PrintPS.java

42

; ) ) (e m aN t eg . ] 0[ s ec i v re s p + r et n i rp d et c e le s ( n l tn i rp . t uo . me t s yS { ) 0 > h t g ne l .s e ci v r es p ( fi ; )t e s a , r ov a l f( s ec i v re S tn i rP p u ko o l. p u ko o Le c i vr e St n i rP = se c iv r e sp ] [e c i vr e St n i rP /* t i e l d na h na c t ah t e ci v re s t ni r p a e t ac o l */ ; )E L P AT S .s g n ih s in i F (d d a. t e sa ; ) E GD E _G N OL _ D ED I S_ O W T. s ed i S (d d a. t e sa ;) ) 2( s e ip o C w e n (d d a. t e sa ; )4 A _O S I .e m aN e z iS a id e M (d d a. t e sa ; )( t e Se t ub i r tt A ts e uq e R tn i rP h s aH w en = t es a t eS e tu b i rt t At s e uq e Rt n i rP ; T PI R C ST S OP . M AE R TS _ T UP N I. r ov a l Fc o D = r o va l f ro v al F c oD /* d e t ni r p e b o t e r a se i po c 2 dn a , 4A s i d e r iu q er * ez i s a i d em e hT . m ae r t s a s a d e i lp p us * e b l l i w h c ih w t pi r cs t s oP s i at a d t n i rp e hT * . n oi t a ci f ic e p s t s eu q e r t n ir p e ht t cu r t sn o C */ { ) ( SP t n ir P ci l b up } ; ) ( SP t ni r P we n = s p S Pt n i rP { )] [ sg r a gn i rt S ( ni a m d i o v c i t at s ci l b up { S Pt n i rP s s al c ci l b up ; * .d r ad n a ts . et u b ir t ta . t ni r p. x a va j tr o p mi ;* . et u b ir t ta . t ni r p. x a va j tr o p mi ;* . t ni r p. x a va j tr o p mi ;* . oi . a va j tr o p mi /* * .s m r et e s ne c il o t tc e jb u s si e sU * . cn I ,s m e ts y so r c iM n uS * fo n o it a mr o f ni y ra t e ir p or p e ht s i er a wt f o s s i hT * * .d e vr e s eR s th g i R l l A .c n I , s m et s ys o r ci M nu S 1 00 2 th g i ry p oC * */

} ; )e ( nl t ni r p .r r e. m e ts y S { ) e n oi t pe c x Et n ir P ( hc t ac } ; ) ei ( nl t ni r p .r r e. m e ts y S { ) ei n oi t p ec x EO I ( hc t ac } ; )t e sa , c od ( tn i r p. j p /* d e if i ce p s sa c od e h t t n i rp * / ; )l l un , r ov a lf , s if ( co D e lp m iS w e n = c o d c o D ; ) s p. e l pm a xe ( ma e rt S tu p n Ie l iF w en = s if ma e r tS t up n I el i F /* . de t el p m oc si g ni t ni r p n e hw ma e r ts eh t e s o lc de t se u q er * f i ll i w ta h t n o i ta t ne m e lp m i l u f es u a si c oD c i sa B * d e ni a tb o e b o t sd e e n m a er t s tu p ni n a * , el i f k si d a n i d et a co l tp i rc s ts o p s i a t ad eh t ec n iS * . at a d tn i rp e h t d l oh o t tc e j bo c oD a e ta e rC * * / { y rt ;) ( bo J t ni r Pe t a er c .] 0 [s e c iv r es p = jp b o Jt n ir P c oD /* e ci v r es n es o h c e h t r o f bo j t ni r p a e t ae r c */

Appendix A

Example: PrintPS.java

43

} }

APPENDIX

Example: PrintGIFtoStream.java

44

; ) 0( t i xe . me t s yS ; )" s e ir o tc a f el b at i u s o N "( n l tn i rp . r re . me t s yS { ) 0 = = h t g ne l .s e ir o t ca f ( fi ;) e p yT e mi M sp , r ov a l f ( s e ir o tc a Fe c i vr e St n i rP m ae r t Sp u ko o l. y r ot c aF e c iv r eS t n ir P ma e r tS = s e ir o t ca f ][ y r ot c aF e c iv r eS t n ir P ma e r tS / * t pi r cs t s oP s a m a er t s eg a mi F I G a t r op x e n a c hc i hw y r ot c af e t ac o L */ ; ) (e p y Te m iM t e g. T PI R CS T S OP . YA R R A_ E TY B . ro v al F c oD = ep y Te m iM s p gn i r tS /* m a er t s t u p tu o eh t f o e p y t e h t y f i ce p S */ ; F IG . M AE R TS _ T UP N I. r ov a l Fc o D = r o va l f ro v al F c oD / * m ae r t St u pn I n a m or f F IG a r o f r ov a lf d e n if e d e r p eh t e sU */ { )] [ sg r a gn i rt S ( ni a m d i o v c i t at s ci l b up { ma e r tS o tF I Gt n i rP s s al c ci l b up /* . r ew e i v t p ir c s ts o p a n i de s u ro , re t n ir p t pi r cs t so P a ot d e lo o ps * e b y am si h T . t pi r cs t so P sa ma e rt s a ot e ga m i d ed o c ne FI G a tr o p xe * n a c hc i h w e c i vr e s a e t a co l o t I P A ec i v re S t ni r P )M T (a v a J e h t es U * */ ; * .d r ad n a ts . et u b ir t ta . t ni r p. x a va j ;* . et u b ir t ta . t ni r p. x a va j ;* . t ni r p. x a va j ;* . oi . a va j tr o p mi tr o p mi tr o p mi tr o p mi

/* .s m r et e sn e c il o t tc e jb u s si e sU * . cn I ,s m e ts y so r c iM n uS * fo n o it a mr o f ni y ra t e ir p or p e ht s i er a wt f o s s i hT * * .d e vr e s eR s th g i R l l A .c n I , s m et s ys o r ci M nu S 1 00 2 th g i ry p oC * */

; ) l lu n ,r o v al f ,s i f (c o De l p mi S we n = co d c oD ; )E L P AT S .s g n ih s in i F (d d a. t e sa ; ) E GD E _G N OL _ D ED I S_ O W T. s ed i S (d d a. t e sa ; )4 A _O S I .e m aN e z iS a id e M (d d a. t e sa ;) ) 2( s e ip o C w e n (d d a. t e sa ; ) ( te S et u b ir t tA t se u q eR t ni r P hs a H w e n = t es a t eS e tu b i rt t At s e uq e Rt n i rP ; )( b o Jt n ir P et a e rc . sp s = jp b o Jt n ir P c oD / * e g a mi F IG e h t r o f bo J tn i r P a l l ac d na e t ae r C */ ; )s o f (e c iv r e St n ir P t eg . ]0 [ s ei r ot c af = s ps e c iv r eS t n ir P ma e r tS / * t pi r cs t so P r of r e tn i rp m a er t S a e t ae r C */ ; ) em a n el i f( m ae r t St u pt u O el i F w e n = s o f ma e rt S t up t uO e l iF ; "s p .e l i fw e n" = e ma n el i f gn i r tS / * t p ir c s ts o p d e t ro p xe e h t r o f el i f a e t ae r C */ ; ) "f i g. o g ol d 2a v aj " ( ma e rt S t up n Ie l i F w e n = s i f m a er t S tu p nI e l iF / * el i f e h t da o L */ { y rt }

Appendix B

Example: PrintGIFtoStream.java

45

} ;) e i( n l tn i rp . r re . me t s yS { ) ei n oi t p ec x EO I ( hc t ac } ;) e p( n l tn i rp . r re . me t s yS { )e p n oi t pe c x Et n ir P ( hc t ac } ; ) ( es o lc . s of ; ) t es a ,c o d (t n ir p . jp

APPENDIX

Example: Print2DPrinterJob.java

46

= s e c iv r es ] [ ec i vr e St n i rP / * t s e uq e r e h t el d na h n ac t ah t e ci v re s t ni r p a e t ac o l */ ; ) s ih t (e l b at n ir P te s . jp ;) ( bo J re t n ir P te g . bo J re t n ir P = j p b oJ r et n i rP / * bo j t ni r p a e t ae r C */ ; )) l l un , " bo j yM " ( em a Nb o J we n (d d a. t e sa ;) ) 2 (s e ip o C we n (d d a. t e sa ; )E P A CS D NA L .d e t se u qe R n oi t at n e ir O (d d a. t e sa ; )( t e Se t ub i r tt A ts e u qe R tn i rP h s aH w e n = t e sa t eS e t ub i rt t A ts e uq e Rt n i rP /* . a id e m e h t fo n oi t a tn e ir o e pa c sd n a l * d n a ,s e ip o c 2 , e ma n b oj a s ei f ic e p s y l la n o ti d da t s eu q er e h t * . tc e j bo e lb a t ni r P a s i at a d tn i rp e h T * . n o it a ci f i ce p s t s e uq e r t n i rp e ht t c ur t sn o C */ { ) ( bo J r et n ir P D2 t n ir P ci l b up { el b at n ir P s tn e me l p mi b o Jr e tn i rP D 2 tn i rP s s al c c il b up ; * .d r a dn a ts . e tu b ir t t a. t ni r p. x a va j t ro p mi ;* . e tu b ir t t a. t ni r p. x a va j t ro p mi ; *. t ni r p. x a va j t ro p mi ;* . t ni r p. t wa . a va j t ro p mi ;* . e ga m i. t wa . a va j t ro p mi ; *. t en . a va j t ro p mi ; *. t wa . a va j t ro p mi ;* . oi . a va j t ro p mi /* * . sm r et e s ne c il o t tc e j bu s si e s U * . cn I , sm e ts y s or c iM n u S * f o n o it a m ro f ni y r at e ir p o rp e ht s i er a w tf o s s i h T * * .d e v re s eR s t hg i R l l A .c n I ,s m et s ys o r ci M n uS 1 00 2 t hg i ry p o C * */

; ) (b o Jr e t ni r PD 2 t ni r P w e n = p s b oJ r et n i rP D 2t n i rP

{ ) ] [g r a gn i rt S ( ni a m d i o v c i t at s ci l b up } ; E GA P _ HC U S_ O N .e l ba t ni r P nr u t er { e s le } ; ST S I XE _ EG A P .e l ba t ni r P nr u t er ; ) 00 2 , 00 2 , 0 , 0 (t c e Rl l if . d 2g ; ) 0 52 , 05 2 , "g n ir t s el p ma x e "( g ni r t Sw a rd . d 2g ; ) k ca l b. r o lo C (r o l oC t es . d 2g ; ) )( Y e lb a eg a m It e g. f p ,) ( Xe l ba e g am I te g . fp ( et a l sn a rt . d 2g ; g) D 2 sc i hp a rG ( = d2 g D 2s c ih p a rG { ) 0 = = x e dn I e ga p ( fi }

{ )x e dn I eg a p tn i ,f p t am r o Fe g aP , g sc i hp a rG ( t ni r p tn i ci l b up } } ; ) ep ( n lt n ir p .r r e .m e ts y S { ) e p no i tp e cx E r et n ir P ( hc t ac } } ;) t es a ( tn i rp . j p { )) t es a ( go l ai D tn i r p. j p( f i ; ) t es a (g o la i D eg a p. j p ; )] 0 [s e c iv r es ( e ci v re S tn i r Pt e s. j p { y rt ; ) )( e ma N te g . ]0 [ se c i vr e s + " r et n i rp d et c e le s "( n l tn i rp . t uo . me t s yS { ) 0 > h t gn e l. s ec i v re s ( fi ;) ( se c iv r e St n ir P p uk o ol . b oJ r et n i rP }

Appendix C

Example: Print2DPrinterJob.java

47

APPENDIX

Example: Print2DGraphics.java

48

; )t e s a , r ov a l f( s ec i v re S tn i rP p u ko o l. p u ko o Le c i vr e St n i rP = s ec i vr e s ][ e ci v r e S t ni r P / * t se u qe r e ht e l dn a h n a c ta h t e c i v r e s tn i rp a e ta c ol * /

. a i de m eh t f o n o it a t n e i ro e p ac s dn a l dn a ,s e i po c 2 , e m an b oj a s ei f i ce p s y l l an o ti d d a t s eu q e r e h t . t c ej b o el b at n ir P a si a t a d t ni r p eh T . no i t ac i fi c ep s t se u qe r t ni r p eh t tc u r t s n oC

;) ) ll u n , b oj y M ( e ma N b oJ w en ( d d a . te s a ; ) )2 ( se i p oC w en ( d d a . te s a ;) E PA C SD N A L. d et s e uq e Rn o i ta t ne i rO ( d d a . te s a ; )( t eS e t ub i rt t A ts e uq e R tn i rP h s aH we n = te s a te S et u b ir t tA t se u q e R t ni r P ; E LB A TN I R P. D ET T AM R O F_ E CI V R ES . ro v a lF c oD = r ov a lf r o v a l Fc o D /* * * * */ { ) ( co D s ci h pa r GD 2 t ni r P c i l b u p { el b a tn i rP s t ne m el p m i c o Ds c i hp a rG D 2t n i rP s s al c ci l b up ; * .d r ad n a ts . et u b ir t ta . t ni r p. x a va j tr o p mi ;* . et u b ir t ta . t ni r p. x a va j tr o p mi ;* . t ni r p. x a va j tr o p mi ;* . tn i r p. t wa . a va j tr o p mi ;* . eg a m i. t wa . a va j tr o p mi ; *. t en . a va j tr o p mi ; *. t wa . a va j tr o p mi ;* . oi . a va j tr o p mi /* * .s m r et e s ne c il o t tc e jb u s si e sU * . cn I ,s m e ts y so r c iM n uS * fo n o it a mr o f ni y ra t e ir p or p e ht s i er a wt f o s s i hT * * .d e vr e s eR s th g i R l l A .c n I , s m et s ys o r ci M nu S 1 00 2 th g i ry p oC * */

{ co D st n e me l pm i c oD e lb a tn i r P s s a lc } ; )( c oD s ci h p ar G D2 t n ir P we n = ps c o Ds c ih p a rG D 2t n i rP { ) ] [g r a gn i rt S ( ni a m d i o v c i ta t s ci l b up } ; EG A P_ H CU S _ ON . el b a tn i rP n r ut e r { e s le } ;S T SI X E_ E G AP . el b a tn i rP n r ut e r ; )0 0 2 , 0 0 2 , 0 ,0 ( t ce R ll i f .d 2 g ; )0 5 2 ,0 5 2 , g ni r ts e l pm a xe (g n i rt S wa r d .d 2 g ; )k c a lb . ro l oC ( r ol o Ct e s .d 2 g ; )) ( Ye l ba e g am I te g . fp , )( X e lb a eg a m It e g. f p( e t al s na r t .d 2 g ; g ) D2 s ci h p ar G ( = d 2 g D 2 sc i h pa r G { ) 0 = = x e dn I e ga p ( fi }

{ )x e dn I eg a p tn i ,f p t am r o Fe g aP , g sc i hp a rG ( t ni r p t n i ci l b up } }

/* . o s o d ot ef a s s i t i ne h w d e if i to n e b o t r en e t si l * b oj t ni r p * a e s u , M V e h t ti x e yl t ic i lp x e ot t n aw u oy f I * .d a e rh t et a r ap e s a n i * g ni t uc e xe eb ya m o s s u o no r hc n y sa eb na c g n it n ir P * . sn r ut e r * t n ir p n eh w )( t i xe . me t s yS l la c y lt i ci l p xe t on o D * */ ; )t e sa , c od ( tn i r p. j p /* d e if i ce p s sa c od e h t t n i rp * / ;) s ih t ( co D el b a tn i rP w e n = c o d c o D /* . a t ad t ni r p eh t dl o h ot t c ej b o c o D a e t ae r C * */ { yrt ;) ( b oJ t ni r P et a er c . ]0 [ se c iv r e s = j p bo J tn i r P c o D / * e c i vr e s n e s oh c eh t r of b o j t n ir p a et a er c * / ;) ) (e m a Nt e g. ] 0 [s e ci v r es + re t ni r p de t ce l es ( nl t ni r p .t u o. m e t s y S { )0 > ht g n el . se c i v r e s( f i

Appendix D

Example: Print2DGraphics.java

49

} ; )e ( nl t ni r p .r r e. m e ts y S { ) e n o i tp e cx E t ni r P( h c ta c }

} ;l l un n r ut e r { n o i tp e cx E O I sw o r ht ) ( s et y Br o F ma e rt S te g m a e rt S tu p nI ci l b up } ;l l un n r ut e r { n o i tp e cx E OI s w or h t )( t xe T r oF r ed a eR t e g r e da e R ci l b up } ; el b a tn i rp n r ut e r { n o it p ec x E OI s w or h t ) ( a ta D tn i rP t e g t c ej b O ci l b up } ;l l un n r ut e r { ) ( se t u bi r tt A t eg t eS e t ub i rt t Ac o D ci l b up } ; E LB A TN I R P. D ET T A MR O F_ E CI V R ES . ro v a lF c oD n r ut e r { ) ( ro v a lF c oD t eg r o va l Fc o D ci l b up } ;e l ba t n ir p = e l b at n ir p . si h t { ) el b at n i rp e lb a t ni r P( c o De l ba t ni r P ci l b up ; e l ba t ni r p el b at n ir P e ta v i rp

Appendix D

Example: Print2DGraphics.java

50

} }

APPENDIX

Example: Print2DtoStream.java

51

( s e ir o tc a Fe c i vr e St n i rP m ae r t Sp u ko o l. y r ot c aF e c iv r eS t n ir P ma e r tS = s e ir o t ca f ][ y r ot c aF e c iv r eS t n ir P ma e r tS / * t pi r cs t s oP s a m a er t s eg a mi F I G a t r op x e n a c hc i hw y r ot c af e t ac o L */ ; ) (e p y Te m iM t e g. T PI R CS T S OP . YA R R A_ E TY B . ro v al F c oD = ep y Te m iM s p gn i r tS /* m a er t s t u p tu o eh t f o e p y t e h t y f i ce p S */ ; E L BA T NI R P .D E TT A M RO F _E C I VR E S. r ov a l Fc o D = r o va l f ro v al F c oD / * m a er t S tu p nI * n a mo r f el b at n i rP a r of r ov a l f d e ni f e d e rp e h t e s U */ { )( m ae r t So t D2 t n ir P ci l b up { e lb a tn i r P s t ne m e lp m i m a e rt S ot D 2t n i rP s s al c ci l b up /* . r ew e i v t p ir c s ts o p a n i de s u ro , r et n ir p t pi r cs t so P a ot * d el o op s e b y a m s i hT .t p i rc s ts o P s a m a er t s a o t s c i hp a rg D2 tr o p xe * n a c hc i h w e c i vr e s a e t a co l o t I P A ec i v re S t ni r P )M T (a v a J e h t es U * */ ; * .d r ad n a ts . et u b ir t ta . t ni r p. x a va j ;* . et u b ir t ta . t ni r p. x a va j ;* . t ni r p. x a va j ;* . tn i r p. t wa . a va j ; *. t wa . a va j ;* . oi . a va j tr o p mi tr o p mi tr o p mi tr o p mi tr o p mi tr o p mi

/* * .s m r et e s ne c il o t tc e jb u s si e sU * . cn I ,s m e ts y so r c iM n uS * fo n o it a mr o f ni y ra t e ir p or p e ht s i er a wt f o s s i hT * * .d e vr e s eR s th g i R l l A .c n I , s m et s ys o r ci M nu S 1 00 2 th g i ry p oC * */

} ; )( m a er t So t D 2t n ir P w en = p s m a er t S ot D 2t n i rP { )] [ sg r a gn i rt S ( ni a m d i o v c i t at s ci l b up } ; E GA P _ HC U S_ O N .e l ba t ni r P nr u t er { e s le } ; ST S I XE _ EG A P .e l ba t ni r P nr u t er ; ) 00 2 , 00 2 , 0 , 0 (t c e Rl l if . d 2g ; ) 0 52 , 05 2 , g n ir t s el p ma x e ( g ni r t Sw a rd . d 2g ; ) k ca l b. r o lo C (r o l oC t es . d 2g ; ) )( Y e lb a eg a m It e g. f p ,) ( Xe l ba e g am I te g . fp ( et a l sn a rt . d 2g ; g) D 2 sc i hp a rG ( = d2 g D 2s c ih p a rG { ) 0 = = x e dn I e ga p ( fi }

{ )x e dn I eg a p tn i ,f p t am r o Fe g aP , g sc i hp a rG ( t ni r p tn i ci l b up } ;) e i( n l tn i rp . r re . me t s yS { ) ei n oi t p ec x EO I ( hc t ac } ;) e p( n l tn i rp . r re . me t s yS { )e p n oi t pe c x Et n ir P ( hc t ac } ; ) ( es o lc . s of ; ) t es a ,c o d (t n ir p . jp ;) l l un , ro v a lf , si h t (c o De l p mi S we n = co d c oD ;) ( te S e tu b ir t t At s eu q e Rt n ir P hs a H we n = te s a t eS e tu b i rt t At s e uq e Rt n i rP ; )( b o Jt n ir P et a e rc . sp s = jp b o Jt n ir P c oD / * bo J tn i r P a l l ac d na e t ae r C */ ; )s o f (e c iv r e St n ir P t eg . ]0 [ s ei r ot c af = s ps e c iv r eS t n ir P ma e r tS / * t pi r cs t so P r of r e tn i rp m a er t S a e t ae r C */ ; ) s p . tu o ( m ae r t St u pt u O el i F w e n = s o f ma e rt S t up t uO e l iF / * t p ir c s ts o p d e t ro p xe e h t r o f el i f a e t ae r C */ { y rt } ; ) 0( t i xe . me t s yS ; ) s e ir o tc a f el b at i u s o N ( n l tn i rp . r re . me t s yS { ) 0 = = h t g ne l .s e ir o t ca f ( fi ;) e p yT e mi M sp , r ov a l f }

Appendix E

Example: Print2DtoStream.java

52

APPENDIX

Example: PrintGIF.java

53

; ) te s a , r o va l f( s e ci v re S tn i r Pp u ko o l .p u ko o L ec i vr e St n i rP = se c i vr e sp ] [ ec i vr e St n i rP / * de i fi c e ps r e nn a m e h t n i FI G a t n i rp n ac h ci h w s e c iv r es t ni r p e t a co L */ ;) ) 1 (s e ip o C we n (d d a. t e sa ; )R E TT E L _A N .e m a Ne z iS a i de M (d d a. t e sa ; )( t e Se t ub i r tt A ts e u qe R tn i rP h s aH w e n = t e sa t eS e t ub i rt t A ts e uq e Rt n i rP /* d et n ir p e b o t s i b o j e h t w oh se i fi c e ps hc i hw te s a et a e rC */ ; F I G. M AE R TS _ T UP N I. r o va l Fc o D = r o va l f ro v al F c oD / * m ae r t St u pn I n a m or f F IG a ro f r ov a l f de n if e d e r p eh t e s U */ { )] [ s gr a gn i r tS ( ni a m di o v c i t at s ci l b up

.s e t ub i rt t a gn i tn i r p f o te s a sa d e if i ce p s et a lp m e t b o j a o t g n i dr o cc a de t ni r p s i eg a mi F IG A . e ga m i d e do c n e F IG a t n ir p na c h c ih w ec i vr e s t n ir p a e t ac o l o t I P A e c iv r eS t ni r P ) M T( a v aJ eh t es U ; * .d r a dn a ts . e tu b ir t t a. t ni r p. x a va j ;* . e tu b ir t t a. t ni r p. x a va j ; *. t ni r p. x a va j ;* . oi . a va j

{ F I G tn i rP s s al c c il b up /* * * * */ t ro p mi t ro p mi t ro p mi t ro p mi

/* * . sm r et e s ne c il o t tc e j bu s si e s U * . c nI , s m et s ys o r ci M nu S f o n o i ta m ro f ni y r at e ir p o rp e h t s i er a w tf o s s i h T * * .d e v re s eR s t hg i R ll A .c n I ,s m et s y so r ci M n uS 1 00 2 t hg i ry p o C * */

{ n o it p ec x E OI , no i t pe c xE g n id o cn E d et r op p us n U sw o r ht )( t xe T r oF r ed a eR t e g r e d ae R ci l b up / * tx e t s a F IG a n ru t er o t el b is s o p t o N */ } ; ) ( se t yB r o Fm a er t St e g nr u t er { n o it p ec x E OI s w or h t ) ( a ta D tn i rP t e g t c e jb O ci l b up /* . )( s e ty B ro F m ae r tS t eg * o t et a ge l e d m a er t St u p nI n a s a de i lp p u s e b ot s i a t ad e ht e cn i S */

co D it l u M ro f l uf e s u yl n ia m

} ;r o v al f = ro v al F c od ; em a n = e m an e l if { )r o v al f ro v a lF c oD , e ma n gn i r tS ( co D m ae r tS t u pn I ci l b up ; m ae r ts m a er t St u p nI e ta v i rp ; r o va l Fc o d ro v al F c oD e ta v i rp ; e ma n el i f gn i r tS e ta v i rp { c o D s t ne m e lp m i c o D ma e rt S t up n I s s a lc } }

} ;) " s re t ni r p el b at i u s o N "( n l tn i rp . r re . me t s yS { e s le } } ; )e ( n lt n ir p .r r e .m e ts y S { ) e n oi t pe c x Et n ir P ( hc t ac } ; )t e sa , c od ( tn i rp . b oJ t ni r p { y rt / * de i fi c e ps s a co d eh t t ni r P */ ; ) ro v al f , "f i g. o g ol d 2a v a j" ( co D m ae r tS t u pn I we n = co d c oD / * a t ad t n ir p eh t s sa p o t n o it a t ne m el p m i c o D a e t ae r C */ ; ) (b o J tn i rP e t ae r c. ] 0 [s e ci v re s p = b o J tn i rp b o Jt n ir P c oD / * bo J t ni r P a e t ae r C */ { ) 0 > h t g ne l .s e ci v r es p ( fi

Appendix F

Example: PrintGIF.java

54

} ; ll u n nr u t er { ) ( se t u bi r tt A t eg t eS e t ub i rt t A co D ci l b up /* c o D si h t ot d e h ca t ta s e t ub i rt t a oN */ } ;r o va l Fc o d nr u t er { ) ( ro v a lF c oD t eg r o va l F co D ci l b up

} } ;m a er t s nr u t er } ; )e m an e l if ( ma e r tS t up n Ie l i F w e n = m a er t s { ) l l un = = m a e rt s ( fi { ) s ih t (d e z in o rh c n ys { n o it p ec x E OI s wo r h t ) ( se t y Br o Fm a e rt S te g m ae r tS t u pn I ci l b up /* . e cn a ts n i em a s e h t nr u te r s ya w lA * . ma e r tS t up n I na s a at a d t n i rp e ht n r ut e R */ ; ll u n nr u t er }

Appendix F

Example: PrintGIF.java

55

APPENDIX

JavaTM Print Service Glossary


attribute attribute category a description of: a capability of a print service, a charactaristic of a doc, an instruction for processing a doc or an entire print job, or the state of a print job or printer. the name of the attribute without its range of values. The Java Print Service represents the attribute category with a class, such as the class. An instance of an attribute class, such as an object, is an attribute value. an attribute collection that usually is restricted to contain only a certain kind of attribute. The Java Print Service defines a superinterface for all attribute sets called and an interface for each kind of attribute set. The kinds of attribute sets are: doc attribute set, print request attribute set, print job attribute set, and print service attribute set. one of the range of values that an attribute supports. The Java Print Service represents each supported attribute with one of the attribute classes, such as the class. The class itself is the attribute category. An instance of the class, such as an object, is the attribute value. The term attribute often refers to the attribute value because the value of an attribute implies the attribute itself.

attribute set

attribute value

doc attribute doc attribute set doc flavor

an attribute that specifies a characteristic of an individual document and the print job settings to be applied to an individual document. a collection of attributes that is restricted to only contain doc attributes. a document type. The Java Print Service uses the class to represent a document type. A object encapsulates a representing a MIME type, which indicates the format of the document, and a representing a Java class, such as java.io.InputStream, that indicates how the document is sent to the printer. an attribute set that can contain different kinds of attributes. The Java Print Service represents this attribute set with the class, which implements .

hash attribute set

gn i r t S

rovalFcoD

g ni r t S

t e S e tu b i r t t A h s a H

coD

doc

a piece of print data. the Java Print Service uses the of print data.

d e t s e u q e R n o it a t n e i r O d e t s eu q e R n o i t a t n e i r O

d et s e u q e R n o it at n e ir O d et s e u q e R n oi t at n ei r O

r o v a lF c o D

t e S e tu b i r t t A t e S e tu b i r t t A

interface to represent a piece

56

print job

a submitted print request, which includes one or more pieces of print data and a set of processing instructions. The Java Print Service represents a print job with the object, which is obtained from a print service. an attribute that reports the status of a print job. A client usually does not specify these attributes since these attributes describe the print job after the client has submitted it. To specify settings for a whole print job, the client instead uses the print request attributes. a collection of attributes that is restricted to only contain print job attributes. an unsubmitted print job. an attribute that specifies a setting applied to a whole print job and to all the documents in the print job. a collection of attributes that is restricted to only contain print request attributes. an interface that represents a particular printer and is a factory for print jobs. an attribute that describes a printer. An example of such an attribute is one that describes the printers location. a collection of attributes that is restricted to only contain print service attributes. the action of attempting to locate a set of print services representing printers that can print a given set of print data according to a given set of processing instructions. A client uses the class to perform this action. a print service that prints data to a client-provided output stream.

print job attribute

print job attribute set print request print request attribute print request attribute set print service print service attribute print service attribute set print-service lookup

stream print service

p uk o o L e c i v r e S t n i r P

b o J tn i r P c o D

Appendix G

JavaTM Print Service Glossary

57

You might also like