DocFlavor, which is a class that defines the
format of the print data.AttributeSet, which encapsulates a set
of attributes that describe the desired print service capabilities,
such as the ability to print five copies, staple, and print
double-sided.DocFlavor and the attribute set.DocPrintJob or a
PrinterJob. If the print data is a document then a DocPrintJob must
be used.
The combinations of printing methods and print data formats yield a choice of six printing mechanisms:
DocPrintJob and an implementation of
PrintServiceDocPrintJob and a
StreamPrintServiceDocPrintJob and an implementation of
PrintServiceDocPrintJob and a
StreamPrintServicePrintService using java.awt.print.PrinterJobStreamPrintService using java.awt.print.PrinterJobDocPrintJob. The Printing and Streaming 2D
Graphics chapter explains printing and streaming 2D
graphics using both DocPrintJob and PrinterJob.// Input the file
FileInputStream textStream;
try {
textstream = new FileInputStream("file.TXT");
} catch (FileNotFoundException ffne) {
}
if (textstream == null) {
return;
}
// Set the document type
DocFlavor myFormat = DocFlavor.INPUT_STREAM.TEXT_PLAIN_ASCII;
// Create a Doc
Doc myDoc = new SimpleDoc(texttream, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
aset.add(MediaSize.ISO_A4);
aset.add(Sides.DUPLEX);
// discover the printers that can print the format according to the
// instructions in the attribute set
PrintService[] services =
PrintServiceLookup.lookupPrintServices(myFormat, aset);
// Create a print job from one of the print services
if (services.length > 0) {
DocPrintJob job = services[0].createPrintJob();
try {
job.print(myDoc, aset);
} catch (PrintException pe) {}
}
Although this sample
only demonstrates one of the six ways to print, the other printing
mechanisms work in a similar way. The rest of this guide discusses
each piece of the printing process and all the printing mechanisms
in more detail.