我对 Java 的 Printer Api 几乎没有经验。现在我正在开发一个托管在 linux 服务器上的 java 企业应用程序,这个应用程序打印一些文档,实际上我正在使用
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 中,可能对我有帮助。有没有人遇到过同样的问题?你能给我一些建议吗?