我对Java的打印机Api没什么经验。现在我正在研究一个托管在linux服务器上的java entrprise应用程序,这个应用程序打印一些文档,实际上我正在使用
javax.print中
打包管理这种情况。这是我用来打印的代码:
FileInputStream in = new FileInputStream(fileToPrint);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob job = myService.getPrintService().createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job,daProcessare);
job.print(doc, pras);
pjw.waitForDone();
in.close();
public PrintJobWatcher(DocPrintJob job,FatturaCodaInvio fileToProcess) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
System.out.println("Print Canceled");
allDone(false,"Print Canceled");
}
public void printJobCompleted(PrintJobEvent pje) {
System.out.println("Print Completed");
allDone(true,"Print Completed");
}
public void printJobFailed(PrintJobEvent pje) {
System.out.println("Print failed");
allDone(false,"Print failed");
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
System.out.println("Print No More Events");
allDone(true,"Print No More Events");
}
void allDone(boolean esito, String info) {
synchronized (PrintJobWatcher.this) {
System.out.println("Printing done ...");
PrintJobWatcher.this.notify();
}
}
});
}
现在在某些情况下,我几乎无法控制打印机。例如,我无法承担如果我的打印机没有准备好或者有问题,那么我想在发送文件之前检查mi打印机的状态。要做到这一点,我谷歌搜索,我发现一些图书馆谎言cups4j可能可以帮助我。是否有人遇到过同样的问题?你能给我一些关于这个的建议吗?