2013-10-03 27 views
2

我需要爲我的打印例程實現一個偵聽器,以便從打印機獲取狀態。我正在使用PrinterJob,但偵聽器PrintServiceAttributeListener對我來說是不夠的。我想實現DocPrintJob的監聽器。我如何轉換到DocPrintJob?PrinterJob與DocPrintJob

這裏是我的原代碼:

public void print(int bufWidth, int bufHeight, BufferedImage bufImage, String printerName, String doPrint) 
{ 
    try 
    { 
    image = bufImage; 
    if (doPrint.toUpperCase().equals("YES")) 
    { 
     PrinterJob printerJob = getPrinterJob(printerName); 
     PrintService printService = printerJob.getPrintService(); 
     //printService.addPrintServiceAttributeListener(this); 
     //printerJob.addPrintJobListener(new PrintJobMonitor()); 

     int width = bufImage.getWidth(); 
     int height = bufImage.getHeight(); 

     PageFormat pf = printerJob.defaultPage(); 
     Paper paper = pf.getPaper(); 
     paper.setSize(width, height); 

     double imageableX = fromCMToPPI(0.1); 
     double imageableY = fromCMToPPI(0.1); 
     double imageableWidth = width - fromCMToPPI(0.1); 
     double imageableHeight = height - fromCMToPPI(0.1); 
     paper.setImageableArea(imageableX, imageableY, imageableWidth, imageableHeight); 

     pf.setOrientation(PageFormat.LANDSCAPE); 
     pf.setPaper(paper); 
     PageFormat validatePage = printerJob.validatePage(pf); 

     printerJob.setPrintable(new MyPrintable(), validatePage);  
     printerJob.print(); 
    } 
    } 
    catch (Exception ex) 
    { 
    } 
} 
+1

如果您添加相應的編程語言標記(Java?),這將有所幫助。 – Filburt

+0

您是否找到解決方案? – whizzzkey

+0

嗨@whizzzkey。不,但是由於打印機有一個SNMP接口,我用它來代替。 –

回答

-1

通過調用的PrinterJob getPrintService你可以得到有一個方法addPrintServiceAttributeListener,這將讓你聽PrintServiceAttributeEvents一個javax.print.PrintService。

+0

正如我在開始寫的那樣,PrintServiceAttributeListener對我來說是不夠的。 PrintJobListener更易於使用並提供簡單的消息。所以我只是想從PrinterJob「轉換」到DocPrintJob,如果可能的話。 –

+0

你能解釋我怎麼能(如果我使用PrintServiceAttributeListener和PrintServiceAttributeEvents)確定printerJob是否完成或者出現錯誤?(就像它做了PrintJobListener) – whizzzkey

+0

你有沒有解決這個問題?我需要相同的,我目前正在使用PrinterJob,但想確定打印是否成功 – Gillardo

相關問題