Documentation Contents
CONTENTS | PREV | NEXT

Using Service-Formatted Data

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

Printing the Service-Formatted Data

To locate print services that can handle the service-formatted data, pass the appropriate service-formatted DocFlavor object constant to the lookupPrintServices method:

PrintService[] services = PrintServiceLookup
    .lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
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:

Doc doc = new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
Create the DocPrintJob, and submit it to the service:

DocPrintJob pj = service[0].createPrintJob();
pj.print(doc);
See Example: Print2DGraphics.java for the complete application.

Streaming Service-Formatted Print Data

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:

DocFlavor 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);
See Example: Print2DtoStream.java for the complete application

 



CONTENTS | PREV | NEXT

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