javax.print

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » print » javax.print 
javax.print
javax.print package Provides the principal classes and interfaces for the JavaTM Print Service API. The Java Print Service API enables client and server applications to:
  • Discover and select print services based on their capabilities
  • Specify the format of print data
  • Submit print jobs to services that support the document type to be printed.

Print Service Discovery

An application invokes the static methods of the abstract class {@link javax.print.PrintServiceLookup PrintServiceLookup} 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.

The JDK includes PrintServiceLookup implementations that can locate the standard platform printers. To locate other types of printers, such as IPP printers or JINI printers, a print-service provider can write implementations of PrintServiceLookup. The print-service provider can dynamically install these PrintServiceLookup implementations using the SPI JAR file specification.

Attribute Definitions

The {@link javax.print.attribute} and {@link javax.print.attribute.standard} packages define print attributes, which describe the capabilities of a print service, specify the requirements of a print job, and track the progress of a print job.

The javax.print.attribute package describes the types of attributes and how they can be collected into sets. The javax.print.attribute.standard package enumerates all of the standard attributes supported by the API, most of which are implementations of attributes specified in the IETF Specification, RFC 2911 Internet Printing Protocol, 1.1: Model and Semantics, dated September 2000. The attributes specified in javax.print.attribute.standard include common capabilites, such as: resolution, copies, media sizes, job priority, and page ranges.

Document Type Specification

The {@link javax.print.DocFlavor DocFlavor} class represents the print data format, such as JPEG or PostScript. A DocFlavor object consists of a MIME type, which describes the format, and a document representation class name that indicates how the document is delivered to the printer or output stream. An application uses the DocFlavor and an attribute set to find printers that can print the document type specified by the DocFlavor and have the capabilities specified by the attribute set.

Using the API

A typical application using the Java Print Service API performs these steps to process a print request:
  1. Chooses a DocFlavor.
  2. Creates a set of attributes.
  3. Locates a print service that can handle the print request as specified by the DocFlavor and the attribute set.
  4. Creates a {@link javax.print.Doc Doc} object encapsulating the DocFlavor and the actual print data, which can take many forms including: a Postscript file, a JPEG image, a URL, or plain text.
  5. Gets a print job, represented by {@link javax.print.DocPrintJob DocPrintJob}, from the print service.
  6. Calls the print method of the print job.
The following code sample demonstrates a typical use of the Java Print Service API: locating printers that can print five double-sided copies of a Postscript document on size A4 paper, creating a print job from one of the returned print services, and calling print.

FileInputStream psStream; try { psStream = new FileInputStream("file.ps"); } catch (FileNotFoundException ffne) { } if (psStream == null) { return; } DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT; Doc myDoc = new SimpleDoc(psStream, psInFormat, null); PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(new Copies(5)); aset.add(MediaSize.A4); aset.add(Sides.DUPLEX); PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset); if (services.length > 0) { DocPrintJob job = services[0].createPrintJob(); try { job.print(myDoc, aset); } catch (PrintException pe) {} }

Please note: In the javax.print APIs, a null reference parameter to methods is incorrect unless explicitly documented on the method as having a meaningful interpretation. Usage to the contrary is incorrect coding and may result in a run time exception either immediately or at some later time. IllegalArgumentException and NullPointerException are examples of typical and acceptable run time exceptions for such cases.

@since 1.4

Java Source File NameTypeComment
AttributeException.javaInterface Interface AttributeException is a mixin interface which a subclass of PrintException PrintException can implement to report an error condition involving one or more printing attributes that a particular Print Service instance does not support.
CancelablePrintJob.javaInterface This interface is used by a printing application to cancel a print job.
Doc.javaInterface Interface Doc specifies the interface for an object that supplies one piece of print data for a Print Job.
DocFlavor.javaClass Class DocFlavor encapsulates an object that specifies the format in which print data is supplied to a DocPrintJob . "Doc" is a short, easy-to-pronounce term that means "a piece of print data." The print data format, or "doc flavor", consists of two things:
  • MIME type. This is a Multipurpose Internet Mail Extensions (MIME) media type (as defined in RFC 2045 and RFC 2046) that specifies how the print data is to be interpreted. The charset of text data should be the IANA MIME-preferred name, or its canonical name if no preferred name is specified.
DocPrintJob.javaInterface This interface represents a print job that can print a specified document with a set of job attributes.
FlavorException.javaInterface Interface FlavorException is a mixin interface which a subclass of PrintException PrintException can implement to report an error condition involving a doc flavor or flavors (class javax.print.DocFlavor DocFlavor ).
MimeType.javaClass Class MimeType encapsulates a Multipurpose Internet Mail Extensions (MIME) media type as defined in RFC 2045 and RFC 2046.
MultiDoc.javaInterface Interface MultiDoc specifies the interface for an object that supplies more than one piece of print data for a Print Job.
MultiDocPrintJob.javaInterface Obtained from a MultiDocPrintService, a MultiDocPrintJob can print a specified collection of documents as a single print job with a set of job attributes.
MultiDocPrintService.javaInterface Interface MultiPrintService is the factory for a MultiDocPrintJob.
PrintException.javaClass Class PrintException encapsulates a printing-related error condition that occurred while using a Print Service instance.
PrintService.javaInterface Interface PrintService is the factory for a DocPrintJob.
PrintServiceLookup.javaClass Implementations of this class provide lookup services for print services (typically equivalent to printers) of a particular type.

Multiple implementations may be installed concurrently. All implementations must be able to describe the located printers as instances of a PrintService.

ServiceUI.javaClass This class is a collection of UI convenience methods which provide a graphical user dialog for browsing print services looked up through the Java Print Service API.

The dialogs follow a standard pattern of acting as a continue/cancel option for a user as well as allowing the user to select the print service to use and specify choices such as paper size and number of copies.

The dialogs are designed to work with pluggable print services though the public APIs of those print services.

If a print service provides any vendor extensions these may be made accessible to the user through a vendor supplied tab panel Component. Such a vendor extension is encouraged to use Swing! and to support its accessibility APIs. The vendor extensions should return the settings as part of the AttributeSet. Applications which want to preserve the user settings should use those settings to specify the print job.

ServiceUIFactory.javaClass Services may optionally provide UIs which allow different styles of interaction in different roles. One role may be end-user browsing and setting of print options. Another role may be administering the print service.
SimpleDoc.javaClass This class is an implementation of interface Doc that can be used in many common printing requests.
StreamPrintService.javaClass This class extends PrintService and represents a print service that prints data in different formats to a client-provided output stream. This is principally intended for services where the output format is a document type suitable for viewing or archiving. The output format must be declared as a mime type. This is equivalent to an output document flavor where the representation class is always "java.io.OutputStream" An instance of the StreamPrintService class is obtained from a StreamPrintServiceFactory instance.

Note that a StreamPrintService is different from a PrintService, which supports a javax.print.attribute.standard.Destination Destination attribute.

StreamPrintServiceFactory.javaClass A StreamPrintServiceFactory is the factory for StreamPrintService instances, which can print to an output stream in a particular document format described as a mime type. A typical output document format may be Postscript(TM).
URIException.javaInterface Interface URIException is a mixin interface which a subclass of PrintException PrintException can implement to report an error condition involving a URI address.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.