java中的PrintJobListener不会产生任何输出

时间:2016-01-19 18:43:33

标签: java printing

我的代码:

   PdfBook pdfBook = new PdfBook(decodePdf, printingDevice, attributeSet);
    SimpleDoc doc = new SimpleDoc(pdfBook, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
    DocPrintJob printJob = printingDevice.createPrintJob();
    PrintJobWatcher pjDone = new PrintJobWatcher(printJob);
    try {
      printJob.print(doc, attributeSet);
      System.out.println(pjDone);
    } catch (PrintException e) {
        LogWriter.writeLog("Exception " + e + " printing");
    }

                        try {
                    Thread.sleep(2000);                 //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
    decodePdf.closePdfFile();  

    System.out.println("Exiting printing process");      
    return null;

}
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;

PrintJobWatcher(DocPrintJob job) {
    // Add a listener to the print job
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCanceled(PrintJobEvent pje) {
            allDone();
        }
        public void printJobCompleted(PrintJobEvent pje) {
            allDone();
        }
        public void printJobFailed(PrintJobEvent pje) {
            allDone();
        }
        public void printJobNoMoreEvents(PrintJobEvent pje) {
            allDone();
        }
        void allDone() {
            synchronized (PrintJobWatcher.this) {
                done = true;
                PrintJobWatcher.this.notify();
            }
        }
    });
}
public synchronized void waitForDone() {
    try {
        while (!done) {
            wait();
        }
    } catch (InterruptedException e) {
    }
}

}

我的问题: 我想确定如果打印作业失败或完成,我如何获得任何值?我尝试了上面的代码,但似乎我无法从PrintJobListener中获取任何值。我的目标是在特定的打印机上打印文档。如果打印作业仍未完成,则不会返回主函数。某些打印作业需要2-3分钟才能完成。但是,即使打印作业尚未完成,它也会立即返回主函数。有没有办法在打印操作期间和之后从printJobLister获取值?

我搜索了有关此问题的相关问题,但这些问题都没有产生我正在寻找的答案。:(

0 个答案:

没有答案