Documentation Contents
CONTENTS | PREV | NEXT

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.

The javax.print.PrintServiceLookup class included in the Java Print Service API provides static methods that applications use to locate printers.

An application invokes the lookupPrintServices method of PrintServiceLookup with a DocFlavor and an AttrbuteSet:

DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(2));
PrintService[] service =
        PrintServiceLookup.lookupPrintServices(flavor,
                        aset);
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 DocFlavor. See the Attributes chapter and the Specifying Document Types chapter for more help in choosing a DocFlavor and creating an AttributeSet.

The base set of printers returned from the lookupPrintServices 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.

After obtaining a suitable PrintService, the application can access its many query methods to determine what values are supported for attribute categories. The Obtaining a Print Job section explains how to get a print job from the PrintService.


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 StreamPrintService.

The StreamPrintServiceFactory class has a lookupStreamPrintServiceFactories method for locating stream print services. This method, like the lookupPrintServices method in PrintServiceLookup, takes a DocFlavor that represents the type of the input document—in this case, DocFlavor.INPUT_STREAM.GIF. Unlike lookupPrintServices, this method also takes a 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 has the getMimeType method for returning the MIME type from a DocFlavor. The lookupStreamPrintServicesFactories method returns an array of StreamPrintServiceFactory objects, which are factories for StreamPrintService instances. 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 StreamPrintServiceFactory object has an instance method called getPrintService that takes an OutputStream parameter and creates a StreamPrintService instance that writes to that stream:

FileOutputStream fos = new FileOutputStream(filename);
StreamPrintService psService = psfactories[0].getPrintService(fos);
The JDK V1.4 includes one stream print service that can export Postscript from graphics calls, such as through the Pageable and Printable interfaces. To verify the availability of this service, use the StreamPrintServiceFactory class to try to locate it. The Printing and Streaming 2D Graphics chapter discusses streaming 2D graphics.

StreamPrintService implements PrintService, which means you can use a StreamPrintService wherever you can use a PrintService. The application is responsible for closing the output stream after a job has printed to the stream. Once the stream is closed, th e StreamPrintService instance can no longer be used.



CONTENTS | PREV | NEXT

Oracle and/or its affiliates Copyright © 1993, 2022, Oracle and/or its affiliates. All rights reserved.
Contact Us