PrinterJob与DocPrintJob

时间:2013-10-03 08:43:03

标签: java printing

我需要为我的打印例程实现一个监听器,以便从打印机获取状态。我正在使用PrinterJob,但是侦听器PrintServiceAttributeListener对我来说还不够。我想实现DocPrintJob的监听器。如何转换为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 个答案:

答案 0 :(得分:-1)

通过在PrinterJob上调用getPrintService,您可以获得一个javax.print.PrintService,它有一个方法addPrintServiceAttributeListener,它允许您监听PrintServiceAttributeEvents。