In the JPS API, printable objects are implemented as the javax.print.Doc type. JPS defines a Doc as "a piece of print data." In order to create a Doc object, you have to have two things: a source of data to be printed and a description of that data.
In this case, the source of data will be a java.io.InputStream and, instead of specifying a description for the data like HTML, plain text, or .PDF, we'll set the description to AUTOSENSE and allow the print service to figure out what kind of data it is.
A javax.print.SimpleDoc will be the document that we'll print. The constructor for SimpleDoc takes three arguments: an object that is the source of data to be printed, a javax.print.DocFlavor object that describes the data, and a javax.print.attribute.DocAttributeSet object that describes additional properties of the Doc to be printed or null if there are no additional properties.
InputStream in = new FileInputStream("test.txt");
Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
The next step is to locate a printer or print service that is capable of printing the document. The javax.print.PrintServiceLookup type is provided for this purpose. There are two basic forms of lookup methods that you can use to do this. The first form takes no arguments and returns a default print service.
PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
The second form of the lookup method is more complex and allows you to look up print services by capability. This method takes two arguments: a DocFlavor object describing the data source and a javax.print.AttributeSet object containing additional print properties null. If there are none, it looks like this:
PrintService services[] =
PrintServiceLookup.lookupPrintServices(doc.getDocFlavor(), null);
The lookupDefaultPrintService() method returns a reference to the default print service or null if no print service can be found. The more specific lookup method returns an array of print services that are capable of handling the requested document or an empty array if no suitable services can be found. In either case, the goal is to attain a reference to a javax.print.PrintService object.
Once you have the PrintService object, you use it to create an instance of javax.print.DocPrintJob:
DocPrintJob pj = ps.createPrintJob();
which is used to print the document:
pj.print(doc, null);
The print method above takes two arguments: the Doc to be printed and a javax.print.attribute.PrintRequestAttributeSet object containing a set of attributes to be applied to all documents in a print job or null if default properties should be used.
The entire sequence looks like this:
InputStream in = new FileInputStream("test.txt"); Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintService psa[] = PrintServiceLookup.lookupPrintServices(doc.getDocFlavor(),
null);
PrintService ps = psa[0];
DocPrintJob pj = ps.createPrintJob();
try {
pj.print(doc, null);
}
catch (PrintException e) {
log.error("error trying to print", e);
}
Printing in Java 1.4 requires that you grasp some new concepts and types such as DocFlavor and a whole family of possible attribute sets.