The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
From the previous lessons you have learned that the Java 2D printing API supports page imaging, displays print and page setup dialogs, and specifies printing attributes. Printing services is another key component of any printing subsystem.
The Java Print Service (JPS) API extends the current Java 2D printing features to offer the following functionality:
The Java Print Service API consists of four packages:
The javax.print
package provides the principal classes and interfaces for the Java Print Service API. It enables client and server applications to:
The DocFlavor
class represents format of the print data, such as JPEG or PostScript. The DocFlavor
format consists of two parts: a MIME type and a representation class name. A MIME type describes the format, and a document representation class name indicates how the document is delivered to the printer or output stream. An application uses the DocFlavor
and an attribute set to find printers with the capabilities specified by the attribute set. This code sample demonstrates obtaining an array of StreamPrintServiceFactory
objects that can return StreamPrintService
objects able to convert a GIF image into PostScript:
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF; String psMimeType = DocFlavor.BYTE_ARRAY. POSTSCRIPT.getMimeType(); StreamPrintServiceFactory[] psfactories = StreamPrintServiceFactory. lookupStreamPrintServiceFactories( flavor, psMimeType);
The javax.print.attribute
and javax.print.attribute.standard
packages define print attributes which describe the capabilities of a print service, specify the requirements of a print job, and track the progress of the print job.
For example, if you would like to use A4 paper format and print three copies of your document you will have to create a set of the following attributes implementing the PrintRequestAttributeSet
interface:
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(MediaSize.ISO_A4); attr_set.add(new Copies(3));
Then you must pass the attribute set to the print job's print
method, along with the DocFlavor
.
An application invokes the static methods of the abstract class PrintServiceLookup
to locate print services that have the capabilities to satisfy the application's print request. For example, in order to print two copies of a double-sided document, the application first needs to find printers that have double-sided printing capability:
DocFlavor doc_flavor = DocFlavor.INPUT_STREAM.PDF; PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(new Copies(2)); attr_set.add(Sides.DUPLEX); PrintService[] service = PrintServiceLookup. lookupPrintServices(doc_flavor, attr_set);
In conclusion, the Java Print Service API performs the following steps to process a print request:
DocFlavor
.DocFlavor
and the attribute set.Doc
object encapsulating the DocFlavor
and the actual print data.DocPrintJob
, from the print service.print
method of the print job.For more information about Java Print Service, see Java 2D Print Service API User Guide.