Friday, June 5, 2009

Listing available printers in system using java

The below code will list the default printer device and also list the available printers in the system

import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobEvent;

public class ListPrinters {
public static void main(String[] args) throws PrinterException{

PrintService defServ = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Default PrintService: "+defServ);

PrintService[] serv = PrintServiceLookup.lookupPrintServices(null, null);
if (serv.length==0) {
System.out.println("no PrintService found");
} else {
System.out.println("number of Services "+serv.length);
}

for (int i = 0; i
PrintServiceAttributeSet psa = serv[i].getAttributes();
System.out.println("printer name "+(i+1)+" "+psa.get(PrinterName.class));
System.out.println("accepting "+psa.get(PrinterIsAcceptingJobs.class));

}
}

2 comments:

  1. Nice demonstration. It helps me in setting my available printer options on a customized application for printing.

    ReplyDelete
  2. Thanks. Complete the line with the for loop with "< serv.length; i++) { .... }".

    ReplyDelete