Java Reference
In-Depth Information
We will be adding an instance of the Copies class to our PrintAttr object to specify the number of
copies to be printed and this will be displayed by the print dialog. We can create an object specifying
the number of copies to be produced like this:
HashPrintRequestAttributeSet printAttr = new HashPrintRequestAttributeSet();
Copies twoCopies = new Copies(2);
if (printer.isAttributeCategorySupported(twoCopies.getCategory()))
printAttr.add(twoCopies);
if(printJob.printDialog(printAttr)) { // Display print dialog
The argument to the constructor specifies the number of copies to be produced. Our object specifies just
two copies but you can go for more if you have the paper, the time, and the inclination.
Before we add this object to the print request attribute set though, we verify that the printer does
actually support the production of multiple copies. Obviously it only makes sense to set an attribute for
a printer that has the appropriate capability - you won't be able to print in color on a monochrome
printer for instance. We can call the isAttributeCategorySupported() method for the
PrintService object that we obtained from the PrinterJob object to do this.
The isAttributeCategorySupported() method requires an argument of type Class to identify
the attribute category that we are querying, and we obtain this by calling the getCategory() method
for our Copies object. If the attribute is supported, we add the twoCopies object to the set
encapsulated by printAttr by calling its add() method.
You can add the three lines of code to the actionPerformed() method in the FileAction inner
class and add an import statement for the Copies class to SketchFrame.java :
import javax.print.attribute.standard.Copies;
If you recompile and run Sketcher once more, the print dialog should come up with two copies set initially.
Of course, setting thing like margin sizes and page orientation once and for all may not be satisfactory
in many cases. It is easy to envisage situations where you may want to print some pages in a document
in portrait orientation while others, perhaps containing illustrations, are printed landscape. Let's see
how we can handle that in Java.
Multipage Document Printing
If you need to print a document that contains multiple pages in a print job, you can do it in the
implementation of the print() method declared in the Printable interface. The PrinterJob
object will continue to call this method until the value NO _ SUCH _ PAGE is returned. However, this won't
be convenient in every case. In a more complicated application than Sketcher, as well as having
different page orientations, you may want to have different class objects printing different kinds of
pages - rendering the same data as graphical or textual output for instance. You can't do this
conveniently with just one class implementing the Printable interface. You also need something
more flexible than just passing a class object that does printing to the PrinterJob object by calling its
setPrintable() method.
Search WWH ::




Custom Search