3

我需要为我的打印例程实现一个监听器,以便从打印机获取状态。我正在使用 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)
   {
   }
}
4

1 回答 1

-1

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

于 2013-10-04T05:15:11.993 回答