CONTENTS | PREV | NEXT | JavaTM Print Service API User Guide |
You can print or stream 2D graphics encapsulated in a Pageable or Printable object using a DocPrintJob and a service-formatted DocFlavor. A DocFlavor can represent any kind of data, including Java objects. An implementation of the Pageable or Printable interface is a Java object. As discussed in the Specifying Document Types chapter, the Java Print Service API includes pre-defined DocFlavor object constants for print data in the form of a Java object. An application can look up print services or stream print services supporting this type of data, encapsulate the object in a Doc implemetation and submit it to the service in a DocPrintJob. The Printing the Service-Formatted Data section demonstrates printing the data. The Streaming Service-Formatted Print Data section demonstrates streaming the data. Registering for events on 2D graphics printing applications using DocPrintJob is done the same way as for document printing applications using DocPrintJob. See Registering for Events for more information.
To locate print services that can handle the service-formatted data, pass the appropriate service-formatted DocFlavor object constant to the lookupPrintServices method:The printing application implements the Printable interface. To create the Doc, use SimpleDoc, passing this in for the printData, the service-formatted DocFlavor constant for the DocFlavor, and an optional attribute set:PrintService []services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);Create the DocPrintJob, and submit it to the service:Doc doc = new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);See Example: Print2DGraphics.java for the complete application.DocPrintJob pj = service[0].createPrintJob(); pj.print(doc);
A stream print service can be used to export 2D graphics encapsulated in a Java object to another format. This example exports graphics in a Printable to Postscript:See Example: Print2DtoStream.java for the complete applicationDocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE StreamPrintServiceFactory []factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, "application/postscript")); if (factories.length == 0) { System.err.println("No suitable factories"); System.exit(0); } try{ FileOutputStream fos = new FileOutputStream("out.ps"); StreamPrintService sps = factories[0].getPrintService(fos); } Doc doc = new SimpleDoc(this, flavor, null); sps.createPrintJob().print(doc);