Package javax.print

Provides the basic interfaces and classes of the Java Print Service API.

See: Description

Package javax.print Description:

Provides the basic interfaces and classes of the Java Print Service API.

The Java Print Service API enables programmers to:

Print Service Discovery

Print service types in the JPS API:

PrintService, MultiDocPrintService

Discovery is done by the use of the static methods in the PrintServiceLookup class. The discovery process can be constrained by supplying the document formats and printing attributes that need to be supported by the returned print service. Furthermore the lookupDefaultPrintService() method enables to lookup the default print service of the platforms printing system.

StreamPrintService

StreamPrintService provides the same functionality as a print service for output to a supplied OutputStream. Available stream print services are discovered via the static methods in the StreamPrintServiceFactory factory. The query can be constrained by supplying the the requested document format support and the needed output format.

Document formats

The format of the printing documents are specified by the DocFlavor class in the JPS API. It provides the description of the format in which the print data will be supplied in a print job to the print service and consists of two parts: The Java Print Service API differentiates between two types of print data, client-formatted and service-formatted. Client-formatted print data is already provided in a formatted representation by the client e.g. in an image format or as postscript. For service-formatted print data, the Java Print Service implementation produces the formatted print data. Here the doc flavor's representation class name does specify an interface instead of the actual print data source. The print service will call the methods of the given implementation of this interface with a special Graphics object capable of producing formatted print data from the graphics routines inside the interface methods.

Printing attributes

Print services as well as print jobs report their state and capabilities by the way of supplying printing attributes. Also the behaviour of print jobs (like how many copies should be printed) is controlled via printing attributes. For these requirements the JPS API defines different roles of attributes and common syntax classes in the package javax.print.attribute. The actual available printing attributes are implemented in the javax.print.attribute.standard package.

Example of using the API

import java.io.*;

import javax.print.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; import javax.print.event.*;
public class Beispiel {   public static void main(String[] args)   {     // Using the predefined doc flavor for postscript mimetype     DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
    // Looking for printservice supporting this doc flavor     PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
    // Just take the first     PrintService service = services[0];     System.out.println("Name :" + service.getName());
    try       {         // Create a print job         DocPrintJob job = service.createPrintJob();
        // We want to print a file so we construct an inputstream         // on the file to supply the print data as given in the doc flavor         File file = new File("File.ps");         InputStream stream = new FileInputStream(file);
        // Build a attribute set with the wanted printing attributes         HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();         attr.add(new Copies(2)); // two copies         attr.add(new PageRanges(2, 7)); // only the 2-7 pages
        // Construct a doc object with the provided class SimpleDoc         SimpleDoc doc = new SimpleDoc(stream, flavor, null);
        // register us as the print - use the adapter class         // and override the interesing failure condition         job.addPrintJobListener(new PrintJobAdapter()         {           public void printJobFailed(PrintJobEvent arg0)           {             System.out.println("The PrintJob failed.");           }         });
        // start the printing process         job.print(doc, attr);
        // lets assume we want to cancel it         if (job instanceof CancelablePrintJob)           {             CancelablePrintJob cancelJob = (CancelablePrintJob) job;             cancelJob.cancel();           }
      }     catch (PrintException e)     {       e.printStackTrace();     }     catch (FileNotFoundException e)     {       e.printStackTrace();     }   } }

Since: 1.4