O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required