java.awt.print.PrinterJob的监听器

时间:2010-11-01 15:27:59

标签: java printing awt

java.awt.print.PrinterJob是否有听众?我只能在PrintJobListener中找到javax.print.DocPrintJob。我在java.awt.print.PrinterJob中寻找它的等价物,以便我可以跟踪打印是否存在问题。

3 个答案:

答案 0 :(得分:3)

这就像是一个古老的问题,但对那些正在寻找像我这样的答案的人来说,我希望它会有所帮助。

通过将printable转换为javax.print,只能使用java.awt事件的事件。以下是如何在不破坏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);

对于单个可打印也可以这样做,但是目前我找不到改变PageFormat的方法,所以我跳过了它。通过这种方式,您可以使用Pageable或Printable,只需使用printJob.addPrintJobListener()

添加它们即可使用这些事件。

这不是一个完整的代码,但它可以让你知道你是否参与其中。

答案 1 :(得分:1)

通过致电getPrintService上的PrinterJob,您可以获得javax.print.PrintService addPrintServiceAttributeListener方法PrintServiceAttributeEvents,这样您就可以收听{{1}}。

答案 2 :(得分:1)

@cappytoi answer的启发,我创建了ListenablePrinterJob类,允许您打印PrintablePageableDoc的实例。

使用示例

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);

完整源代码

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;
    }
}