11

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 trays.
  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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user789122
  • 480
  • 5
  • 12
  • 25
  • First idea is to go for something like cups4j. Not sure if basic printing api in java could handle this. – Wivani Jun 08 '11 at 12:09
  • http://stackoverflow.com/questions/9887713/how-to-print-out-java-jvm-memory-use-for-debugging-purposes – Anas Amosh Apr 29 '14 at 11:42

4 Answers4

9

Some quick hints:

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.

Francis Bartkowiak
  • 1,374
  • 2
  • 11
  • 28
MarcoS
  • 13,386
  • 7
  • 42
  • 63
4

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?

Community
  • 1
  • 1
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
0

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
-3

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

}

Nupur
  • 571
  • 5
  • 3