Listening for Print Job Attribute Changes : PrintJob « 2D Graphics « Java Tutorial






import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintJobAttributeSet;
import javax.print.attribute.PrintJobAttributeSet;
import javax.print.attribute.standard.JobMediaSheetsCompleted;
import javax.print.event.PrintJobAttributeEvent;
import javax.print.event.PrintJobAttributeListener;

public class Main {
  public static void main(String[] argv) throws Exception {
    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
        .lookupStreamPrintServiceFactories(flavor, DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);

    DocPrintJob job = service.createPrintJob();
    PrintJobAttributeSet set = new HashPrintJobAttributeSet(job.getAttributes());
    set.add(new JobMediaSheetsCompleted(0));
    job.addPrintJobAttributeListener(new MyPrintJobAttributeListener(), set);
  }
}

class MyPrintJobAttributeListener implements PrintJobAttributeListener {
  public void attributeUpdate(PrintJobAttributeEvent pjae) {
    Attribute[] attrs = pjae.getAttributes().toArray();
    for (int i = 0; i < attrs.length; i++) {
      String attrName = attrs[i].getName();
      String attrValue = attrs[i].toString();
    }
  }
}








16.49.PrintJob
16.49.1.Print with java.awt.PrintJob
16.49.2.Print Sample with java.awt.GraphicsPrint Sample with java.awt.Graphics
16.49.3.Print configuration
16.49.4.Listening for Print Job Attribute Changes
16.49.5.Determining When a Print Job Has Finished
16.49.6.Cancelling a Print Job
16.49.7.Getting the Default Value of a Print Job Capability
16.49.8.Getting the Possible Values for a Print Job Capability
16.49.9.Setting the Orientation of a Print Job -- Portrait or Landscape
16.49.10.demonstrate the Java 1.4 printing API
16.49.11.Setting the Number of Copies of a Print Job
16.49.12.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
16.49.13.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
16.49.14.Setting the Orientation of a Printed Page