Spec-Zone .ru
спецификации, руководства, описания, API
|
CONTENTS | PREV | NEXT | JavaTM Print Service API User Guide |
A typical application using the Java Print Service API performs these steps to process a print request:
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.
- Obtain a suitable DocFlavor, which is a class that defines the format of the print data.
- 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.
- Lookup a print service that can handle the print request as specified by the DocFlavor and the attribute set.
- Create a print job from the print service.
- Call the print job's print method.
The combinations of printing methods and print data formats yield a choice of six printing mechanisms:
- Print a document to a printer by using a DocPrintJob and an implementation of PrintService
- Stream a document to an output stream by using a DocPrintJob and a StreamPrintService
- Print 2D graphics to a printer by using a DocPrintJob and an implementation of PrintService
- Stream 2D graphics to an output stream by using a DocPrintJob and a StreamPrintService
- Print 2D graphics to a PrintService using java.awt.print.PrinterJob
- Stream 2D graphics to a StreamPrintService using java.awt.print.PrinterJob
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.
Most applications using the Java Print Service API will probably send a document directly to a printer, which the following code sample demonstrates: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// 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) {} }