Advanced Printing with Java 1.4

The Java 1.4 Printing API is substantially more complex than the Java 1.1 and Java 1.2 APIs. It is a command-line utility that can:

  • List available printers capable of handling specific printing requests (color printing, collation, stapling, etc.)

  • Query the status of a particular named printer

  • Spool text or image files directly to a printer

  • Convert GIF, JPEG, and PNG image files to PostScript files

Example 13-5 demonstrates some of these advanced features. The example does not create a GUI, and, unlike the other examples in this chapter, it prints without using a Graphics object. The code is well-commented and straightforward; the example is long only because it demonstrates several different features of the Java 1.4 API.

Example 13-5. Print.java

package je3.print; import javax.print.*; import javax.print.event.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; import java.io.*; /** * This utility program demonstrates the javax.print API and allows you to * list available printers, query a named printer, print text and image files * to a printer, and print to PostScript files. * * Usage: * java Print -i inputfile [-q] [-p printer] [-ps outputfile] [attributes] **/ public class Print { public static void main(String[ ] args) throws IOException { // These are values we'll set from the command-line arguments boolean query = false; String printerName = null; String inputFileName = null; String outputFileName = null; String outputFileType ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.