【问题标题】:Connecting and printing to a printer in Java在 Java 中连接并打印到打印机
【发布时间】:2011-06-08 12:04:43
【问题描述】:

在 Java 中是否有一种简单的方法来执行以下操作?

  1. 连接到打印机(将是本地打印机,并且是唯一连接到机器的打印机)。
  2. 在 2 个不同的打印机纸盘中打印 2 页的页面。
  3. 获取当前打印队列计数,即我有 100 个要打印的项目,当前已打印 34 个,打印机队列现在应为 66。

【问题讨论】:

标签: java printing


【解决方案1】:

一些快速提示:

这个监听器的实现 接口应该附加到一个 DocPrintJob 监控状态 打印机作业。这些回调 方法可以在线程上调用 处理打印作业或服务 创建通知线程。在任一 客户不应该执行的情况 在这些回调中进行冗长的处理。

【讨论】:

    【解决方案2】:

    一个很好的打印教程: http://download.oracle.com/javase/tutorial/2d/printing/index.html

    同时检查我关于打印机的问题的答案,打印机作业 API 是您正在寻找的,但检查这一点也将有所帮助:

    How to Send JTable data to Print Job from Java Application?

    【讨论】:

      【解决方案3】:

      您的要求非常具体,因此我不确定 Java 打印 API 是否满足您的所有需求。您可以使用JNA 直接访问您的本机操作系统的 API,这可能会为您提供打印队列信息。

      【讨论】:

        【解决方案4】:

        一个简单的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;
            }
        

        }

        【讨论】:

          猜你喜欢
          • 2020-06-22
          • 1970-01-01
          • 2013-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-03
          • 1970-01-01
          • 2010-12-11
          相关资源
          最近更新 更多