Java advanced programming: using the Print Service API

Source: Internet
Author: User

Java has developed rapidly in various aspects since its publication, but printing and output have always been the weakest aspect of java. In fact, Java does not support any printing function. Java1.1 contains a class called PrintJob in the java. awt package, but the printing function provided by this class is very rough and unreliable. When java1.2 came out, it designed a complete and independent printer System (called java2D printing API) Around PrinterJob, and defined some new classes and interfaces in the java. awt. print package. These make the PrintJob-based printer (AWT printing) basically obsolete, although PrintJob has never been attacked and at least in this article is still a class that provides technology.
In J2SE1.3, some additional changes occur when PrintJob functions are extended to setting project and page attributes by using the JobAttributes and PageAttributes classes in the java. awt package. With the release of J2SE1.3, the printing function has been improved. However, there are still some problems when using these two completely different printer systems. For example, the two mechanisms use an interface of the java. awt. Graphics class to display the printed content, which means that all the items to be printed must be represented by an image. In addition, the complete PrintJob provides a very limited set of engineering-related properties; neither of these mechanisms can be used to select the target printer through the program.
The biggest change in Java printing comes from the Java Printing Service API brought about by the release of J2SE. This third-generation Java printing support interface breaks through the limitations of the PrintService and DocPrintJob interfaces using the javax. print package previously mentioned. Because the new API is a parent set of the previously defined functions of the two old printers, it is a common method and the focus of this article.
Further, the following steps show how to use the new Java Printing Service API:
1. Define the printer and restrict the list of functions that are returned to provide the functions you want to implement. The Print Service implements the PrintService interface.
2. Create a print event by calling the createPrintJob () method defined in the interface as an instance of DocPrintJob.
3. Create a class that implements the Doc interface to describe the data you want to print. You can also create a PrintRequestAttributeSet instance to define the print options you want.
4. Use the printv () method defined by the DocPrintJob interface to initialize printing, specify the Doc you created earlier, specify PrintRequestAttributeSet, or set it to a null value.
Now you can check each step and try to complete them.
  Define Print Service
You can use one of the three static methods defined in the PrintServiceLookup class. The simplest one is lookupDefaultPrintService (). Just like its name, it returns a default printer:
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
Although this method is very simple and convenient, using it to select your printer means that your printer has always supported the data output that your program needs to accurately transmit. In fact, what you really want is the type that can process the data you want and support the features you want, such as color or printing on both sides. To return the printers supported by the special features you require from the list, you can use lookupPrintServices () or lookupMultiDocPrintServices () in the remaining two methods ().
The lookupPrintServices () method has two parameters: A DocFlavor instance and an instance that implements the AttributeSet interface.
You will immediately see that you can use either of the two to restrict the returned printer, but lookupPrintServices () allows you to specify the two parameters as null values. If you set both to null, the returned value will be any available printer. In this case, you do not need to view the methods defined in PrintService. One of the getName () Methods returns a string representing the name of the printer. You can compile the following code to list the existing printers in your system:
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < services.length; i++)
{
  System.out.println(services[i].getName());
}

For example, if your system name is PrintServer and there are Alpha, Beta, and Gamma printers, you can use the above code to get the following output:
\PrintServerAlpha
\PrintServerBeta
\PrintServerGamma

Check the parameters that you can pass to the lookupPrintServices () method to see how to return printers with special functions.
DocFlavor
The first parameter you can specify is a DocFlavor instance, which describes the type and storage of the data to be printed. In most cases, you do not need to create a new instance. Because Java contains many pre-defined instances, you can use them to pass to lookupPrintServices (). However, let's take a look at the DocFlavor structure and method to explore how the Print Service uses this instance.
The two parameters required to create a DocFlavor instance are strings. One is the MIME (Multipurpose Internet Mail Extensions) type and the other is the class name. The MIME type is used to describe the data type. For example, if you want to print a gif file, you need to use DocFlavor of the MIME type of image/gif. Similarly, if you want to print text information in an HTML file, use MIME-type text/plain or text/html.

Presentation class
The MIME type describes the type of the data to be printed, and the class represents how the Print Service gets the data. DocFlavor contains several static internal classes, each of which corresponds to a presentation class and how to load the data to be printed.
Table 1 lists the internal and performance classes mentioned above. Note that next to SERVICE_FORMATTED (which will be explained in more detail later), each corresponds to "binary" or "character. In fact, these differences are artificial, because the "character" data type itself is a special binary type. In this case, binary data includes characters that people can understand and formatted characters such as tabs and line breaks. Of course, these differences are very important, reflecting that the class of forward character is not suitable for storing binary data.
For example, you do not use character queues or strings to save a gif file, and you cannot access it through the Reader interface. On the other hand, because character is a special binary data, it is perfect for storing text information in byte arrays or accessing it through InputStream or a URL.


Table 1. DocFlavor representations
  

Any static internal class defined above corresponds to a presentation class, but remember that every DocFlavor instance confirms the type of data to be printed through a presentation class and a MIME.
To access such an instance, you need to use the internal classes listed in Table 1. For example, if you want to print a gif file accessed through a URL on the Internet, you can select javav.net. url as the performance class, and the corresponding static class in DocFlavor is the URL class. If you open the internal class document, you will find that it actually defines a series of static internal classes, each corresponding to the MIME type supported by a printer. Table 2 describes the internal classes and MIME in DocFlavor. URL.


Table 2. The DocFlavor. URL inner classes

  
Because you want to print GIF images through URLs, you can use the code to obtain the instance.
DocFlavor flavor = DocFlavor.URL.GIF;
This Code creates a DocFlavor instance, which indicates that the class is java.net. URL, And the MIME is image/gif.
Table 2 lists DocFlavor. URL classes. What about the other six internal classes? Let's wait and discuss SERVICE_FORMATTED. Before that, let's look at the internal classes related to all three types of binary data (BYTE_ARRAY, INPUT_STREAM, and URL. For example, if you store gif in a byte array, you can use the following code:
DocFlavor flavor = DocFlavor.BYTE_ARRAY.GIF;
Just as there are three internal classes associated with the binary type, the other three classes related to the character type are listed in table 3.


Table 3. CHAR_ARRAY, READER, and STRING


Therefore, if you want to print text data stored in strings, use the following code: DocFlavor flavor = DocFlavor. STRING. TEXT_PLAIN; similarly, if the text is from the HTML document on the webpage, use the following code:
DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
  Select the correct printer
Remember the assumption about the exact support for the data type you want to print for the printer before we started to discuss DocFlavor? This seems unnecessary. In fact, you will be surprised by the types of documents supported by your printer. For example, the text type seems to be the easiest to support, so if your program wants to print a common text or HTML text, you can select a print service and send it to the printer. However, most printers do not support text-based representations. If you try to send DocFlavor that is not supported by the printer, the following exception occurs:
Exception in thread "main"
sun.print.PrintJobFlavorException: invalid flavor at sun.print.Win32PrintJob.print(Win32PrintJob.java:290) at PrintTest.main(PrintTest.java:11)

Now you know how to get a DocFlavor reference and we also discuss the importance of choosing a printer to support this flavor. Next I will show you how to determine whether your printer supports it. Previously, I said that lookupPrintServices () allows you to specify a DocFlavor as the first parameter. If the parameter you specify is not empty, the method returns an instance that supports this printer. For example, the following code returns a list of printers that can print gif files through urls:
DocFlavor flavor = DocFlavor.URL.GIF;
PrintService[] services = PrintSer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.