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