Listener for java.awt.print.PrinterJob

Is a listener available for java.awt.print.PrinterJob ? I could find PrintJobListener in javax.print.DocPrintJob . I am looking for its equivalent in java.awt.print.PrinterJob so that I can track if there are printing problems. A.

+4
source share
3 answers

This will sound like a jagged old question, but for those looking for an answer like me, hopefully this helps.

Only java.awt events can be used to convert your print to javax.print. Here's how you can do it right without breaking pageFormat.

  // get default service PrintService service = PrintServiceLookup.lookupDefaultPrintService(); // this is your old awt job, which you can use to get default pageFormat PrinterJob job = PrinterJob.getPrinterJob(); // set your printer service to old awt so that you can get the default paper job.setPrintService(service); // get the default page format from the printer you selected PageFormat pageFormat = job.getPageFormat(null); // do some paper stuff etc. here // get your doc DocPrintJob printJob = service.createPrintJob(); Book book = new Book(); // Print interface implements printable book.append(new PrintInterface(), pageFormat); // make your attr here PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(PrintQuality.HIGH); // Create your doc for pageable SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); /// and voila, print it printJob.print(doc, aset); 

The same thing can be done for one print, but at the moment I could not find a way to change PageFormat for this, so I just skipped it. That way you can use Printable or Printable, and you can still use these events by simply adding them using printJob.addPrintJobListener ()

This is not a complete code in any way, but it gives you an idea if you are for it.

+3
source

By calling getPrintService on the PrinterJob , you can get javax.print.PrintService , which has an addPrintServiceAttributeListener method that allows you to listen to PrintServiceAttributeEvents .

+1
source

Inspired by the @cappytoi answer , I created the ListenablePrinterJob class, which allows you to print instances of Printable , Pageable, and Doc .

Usage example

 PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService(); ListenablePrinterJob job = new ListenablePrinterJob(defaultPrintService); job.setPrintJobListener(new PrintJobAdapter() { // implement listeners for events that you want }); // Print Printable with default page format of PrintService job.print(printable); // Print Printable with given page format job.print(printable, pageFormat); // Print Pageable job.print(pageable); // Print Doc job.print(doc); 

Full source code

 import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.PrintService; import javax.print.SimpleDoc; import javax.print.event.PrintJobListener; import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Pageable; import java.awt.print.Printable; import java.awt.print.PrinterJob; public class ListenablePrinterJob { private final PrintService printService; private PrintJobListener printJobListener; public ListenablePrinterJob(PrintService printService) { this.printService = printService; } private static PageFormat getDefaultPageFormat() { return PrinterJob.getPrinterJob().defaultPage(); } private static Pageable toPageable(Printable printable, PageFormat page, int numPages) { Book book = new Book(); book.append(printable, page, numPages); return book; } /** * Prints single-page {@link Printable}. */ public void print(Printable printable) throws PrintException { print(printable, getDefaultPageFormat()); } /** * Prints {@link Printable} that has {@code numPages} pages. */ public void print(Printable printable, int numPages) throws PrintException { print(printable, numPages, getDefaultPageFormat()); } /** * Prints single-page {@link Printable}. */ public void print(Printable printable, PageFormat pageFormat) throws PrintException { print(toPageable(printable, pageFormat, 1)); } /** * Prints {@link Printable} that has {@code numPages} pages. */ public void print(Printable printable, int numPages, PageFormat pageFormat) throws PrintException { print(toPageable(printable, pageFormat, numPages)); } public void print(Pageable pageable) throws PrintException { print(new SimpleDoc(pageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null)); } public void print(Doc doc) throws PrintException { DocPrintJob printJob = printService.createPrintJob(); printJob.addPrintJobListener(printJobListener); printJob.print(doc, null); } public void setPrintJobListener(PrintJobListener printJobListener) { this.printJobListener = printJobListener; } } 
+1
source

All Articles