开发者

Connecting and printing to a printer in Java

Is there an easy way in Java to do the following?

  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 t开发者_运维知识库rays.
  3. Get the current print queue count, i.e. I have 100 items to print and 34 have been currently printed, the printer queue should now read 66.


Some quick hints:

  • print from java: see A Basic Printing Program

  • status of printing job: you might be able to get something useful by using a PrintJobListener:

Implementations of this listener interface should be attached to a DocPrintJob to monitor the status of the printer job. 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

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:

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


Your requirements are very specific, so I'm not sure the Java printing APIs meet all your needs. You could use JNA to access your native OS's APIs directly, and that would probably get you the print-queue information.


A simple java program to get byte Array printed

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;
    }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜