简体   繁体   English

连接并打印到 Java 中的打印机

[英]Connecting and printing to a printer in Java

Is there an easy way in Java to do the following? Java 中是否有一种简单的方法可以执行以下操作?

  1. Connect to a printer (will be a local printer and the only printer connected to the machine).连接到打印机(将是本地打印机和唯一连接到机器的打印机)。
  2. Print pages that are 2 pages in 2 different printer trays.在 2 个不同的打印机纸盘中打印 2 页的页面。
  3. Get the current print queue count, ie I have 100 items to print and 34 have been currently printed, the printer queue should now read 66.获取当前打印队列计数,即我有 100 个要打印的项目,当前已打印 34 个,打印机队列现在应该为 66。

Some quick hints:一些快速提示:

Implementations of this listener interface should be attached to a DocPrintJob to monitor the status of the printer job.此侦听器接口的实现应附加到 DocPrintJob 以监视打印机作业的状态。 These callback methods may be invoked on the thread processing the print job, or a service created notification thread.可以在处理打印作业的线程或服务创建的通知线程上调用这些回调方法。 In either case the client should not perform lengthy processing in these callbacks.在任何一种情况下,客户端都不应该在这些回调中执行冗长的处理。

A very good printing tutorial: http://download.oracle.com/javase/tutorial/2d/printing/index.html很不错的打印教程: http://download.oracle.com/javase/tutorial/2d/printing/index.html

Also check answers to my question about printers, the Printer Job API is what are you looking for, but checking this out will also help:还要检查我关于打印机的问题的答案,打印机作业 API 是你要找的,但检查一下也有帮助:

How to Send JTable data to Print Job from Java Application? 如何从 Java 应用程序将 JTable 数据发送到打印作业?

Your requirements are very specific, so I'm not sure the Java printing APIs meet all your needs.您的要求非常具体,因此我不确定 Java 打印 API 是否满足您的所有需求。 You could use JNA to access your native OS's APIs directly, and that would probably get you the print-queue information.您可以使用JNA直接访问本机操作系统的 API,这可能会为您提供打印队列信息。

A simple java program to get byte Array printed一个简单的 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;
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM