【发布时间】:2011-06-08 12:04:43
【问题描述】:
在 Java 中是否有一种简单的方法来执行以下操作?
- 连接到打印机(将是本地打印机,并且是唯一连接到机器的打印机)。
- 在 2 个不同的打印机纸盘中打印 2 页的页面。
- 获取当前打印队列计数,即我有 100 个要打印的项目,当前已打印 34 个,打印机队列现在应为 66。
【问题讨论】:
-
第一个想法是选择像 cups4j 这样的东西。不确定java中的基本打印api是否可以处理这个。
在 Java 中是否有一种简单的方法来执行以下操作?
【问题讨论】:
一些快速提示:
从 java 打印:见 A Basic Printing Program
打印作业的状态:您也许可以通过使用PrintJobListener获得一些有用的东西:
这个监听器的实现 接口应该附加到一个 DocPrintJob 监控状态 打印机作业。这些回调 方法可以在线程上调用 处理打印作业或服务 创建通知线程。在任一 客户不应该执行的情况 在这些回调中进行冗长的处理。
【讨论】:
一个很好的打印教程: http://download.oracle.com/javase/tutorial/2d/printing/index.html
同时检查我关于打印机的问题的答案,打印机作业 API 是您正在寻找的,但检查这一点也将有所帮助:
【讨论】:
您的要求非常具体,因此我不确定 Java 打印 API 是否满足您的所有需求。您可以使用JNA 直接访问您的本机操作系统的 API,这可能会为您提供打印队列信息。
【讨论】:
一个简单的java程序打印字节数组
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
String str = "ABCGVDVJHBDKDLNJOKBUHODVWFCVDHGSBDKS";
byte[] byteArr = str.getBytes();
ByteArrayInputStream fis = new ByteArrayInputStream(byteArr);
String printerID; // = give printer ID here
System.out.println("printerID"+printerID);
String command = "lp -d " + printerID;
Process child = Runtime.getRuntime().exec(command);
OutputStream childOut = child.getOutputStream();
byte[] buffer = new byte[100000000];
int bytesRead;
while ((bytesRead = fis.read(buffer)) > 0)
{
childOut.write(buffer, 0, bytesRead);
}
childOut.close();
int exitVal = child.waitFor();
InputStream childIn = child.getInputStream();
BufferedReader is = new BufferedReader(new InputStreamReader(childIn));
String line;
boolean retval;
while ((line = is.readLine()) != null)
{
String finalLine = line;
}
childIn.close();
if (exitVal == 0)
{
retval = true;
}
}
【讨论】: