CONTENTS | PREV | NEXT

How Applications Use the Java Print Service

A typical application using the Java Print Service API performs these steps to process a print request:
  1. Obtain a suitable DocFlavor, which is a class that defines the format of the print data.
  2. Create and populate an AttributeSet, which encapsulates a set of attributes that describe the desired print service capabilities, such as the ability to print five copies, stapled, and double-sided.
  3. Lookup a print service that can handle the print request as specified by the DocFlavor and the attribute set.
  4. Create a print job from the print service.
  5. Call the print job's 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 Jav a 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</ a> chapter discusses printing and streaming documents using DocPrintJob. The Printing and Streaming 2D Graphics chapter explains printing and 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:

// Input the file
FileInputStream textStream; 
try { 
        textstream = new FileInputStream("file.TXT"); 
} catch (FileNotFoundException ffne) { 
} 
if (textstream == null) { 
        return; 
} 
// Set the document type
DocFlavor myFormat = DocFlavor.INPUT_STREAM.TEXT_PLAIN_ASCII;
// Create a Doc
Doc myDoc = new SimpleDoc(texttream, myFormat, null); 
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
aset.add(new Copies(5)); 
aset.add(MediaSize.ISO_A4);
aset.add(Sides.DUPLEX); 
// discover the printers that can print the format according to the
// instructions in the attribute set
PrintService[] services =
        PrintServiceLookup.lookupPrintServices(myFormat, aset);
// Create a print job from one of the print services
if (services.length > 0) { 
        DocPrintJob job = services[0].createPrintJob(); 
        try { 
                job.print(myDoc, aset); 
        } catch (PrintException pe) {} 
} 
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


CONTENTS | PREV | NEXT

Oracle and/or its affiliates
Java Technology

Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.

Contact Us