Listener for java.awt.print.PrinterJob
Asked Answered
U

3

7

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

Unpeg answered 1/11, 2010 at 15:27 Comment(0)
K
2

This gonna be like necroing an old question but to those who are searching for an answer like me, I hope it will help.

It is only possible to use events for java.awt stuff by converting your printable to the javax.print. Here is how you can do it properly without breaking the 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 can be done for a single printable but currently I couldn't find a way to change PageFormat for that, so I just skipped it. This way you can use Pageable or Printable and you can still use those events by simply adding them with printJob.addPrintJobListener()

This is not a full code by any means but it gives you the idea if you are in for it.

Kurtkurth answered 28/6, 2012 at 20:59 Comment(0)
A
2

Inspired by @cappytoi answer, I created ListenablePrinterJob class, that allows you printing instances of Printable, Pageable and Doc.

Example usage

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;
    }
}
Assassinate answered 6/2, 2016 at 0:26 Comment(0)
H
1

By calling getPrintService on the PrinterJob you can get a javax.print.PrintService that has a method addPrintServiceAttributeListener which will allow you to listen for PrintServiceAttributeEvents.

Hiccup answered 2/11, 2010 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.