gpt4 book ai didi

java - PrinterJob 与 DocPrintJob

转载 作者:行者123 更新时间:2023-11-29 09:30:23 24 4
gpt4 key购买 nike

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

最佳答案

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

关于java - PrinterJob 与 DocPrintJob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19154629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com