From the previous lessons you have learned that the Java 2D printing API supports page imaging, displays print and page setup dialogs, and specifies printing attributes. Printing services is another key component of any printing subsystem.
The Java Print Service (JPS) API extends the current Java 2D printing features to offer the following functionality:
The Java Print Service API consists of four packages:
The
javax.print
package provides the principal classes and interfaces for the Java Print Service API. It enables client and server applications to:
The
DocFlavor
class represents format of the print data, such as JPEG or PostScript. The DocFlavor
format consists of two parts: a MIME type and a representation class name. A MIME type describes the format, and a document representation class name indicates how the document is delivered to the printer or output stream. An application uses the DocFlavor
and an attribute set to find printers with the capabilities specified by the attribute set. This code sample demonstrates obtaining an array of StreamPrintServiceFactory
objects that can return StreamPrintService
objects able to convert a GIF image into PostScript:
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF; String psMimeType = DocFlavor.BYTE_ARRAY. POSTSCRIPT.getMimeType(); StreamPrintServiceFactory[] psfactories = StreamPrintServiceFactory. lookupStreamPrintServiceFactories( flavor, psMimeType);
The
javax.print.attribute
and
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 the print job.
For example, if you would like to use A4 paper format and print three copies of your document you will have to create a set of the following attributes implementing the PrintRequestAttributeSet
interface:
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(MediaSize.ISO_A4); attr_set.add(new Copies(3));
Then you must pass the attribute set to the print job's print
method, along with the DocFlavor
.
An application invokes the static methods of the abstract class PrintServiceLookup
to locate print services that have the capabilities to satisfy the application's print request. For example, in order to print two copies of a double-sided document, the application first needs to find printers that have double-sided printing capability:
DocFlavor doc_flavor = DocFlavor.INPUT_STREAM.PDF; PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(new Copies(2)); attr_set.add(Sides.DUPLEX); PrintService[] service = PrintServiceLookup. lookupPrintServices(doc_flavor, attr_set);
In conclusion, the Java Print Service API performs the following steps to process a print request:
DocFlavor
.DocFlavor
and the attribute set.Doc
object encapsulating the DocFlavor
and the actual print data.DocPrintJob
, from the print service.print
method of the print job.For more information about Java Print Service, see Java 2D Print Service API User Guide.