How do I get print service attribute set?

This example demonstrates how to get print service’s attribute set using the javax.print API. First we find the default printer for the current machine using the PrintServiceLookup class. This will give us a PrintService object, this object might be null if no print service found.

The final step is to get the print service attribute set by calling getAttributes() method of the PrintService. We can convert the returned AttributeSet into an array using the toArray() method and iterates it.

package org.kodejava.print;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;

public class PrinterAttribute {
    public static void main(String[] args) {
        // Locates the default print service for this environment.
        PrintService printer = PrintServiceLookup.lookupDefaultPrintService();

        if (printer != null) {
            // Getting print service's attribute set.
            AttributeSet attributes = printer.getAttributes();
            for (Attribute a : attributes.toArray()) {
                String name = a.getName();
                String value = attributes.get(a.getClass()).toString();
                System.out.println(name + " : " + value);
            }
        }
    }
}

An example result of this program is:

color-supported : supported
queued-job-count : 0
printer-name : HP LaserJet P1005
printer-is-accepting-jobs : accepting-jobs

How do I get printer or print service name?

This example show you how to get the printer or print service installed on your machine. To get the installed services we can use PrinterJob.lookupPrintServices() method call. This method return an array of PrintService objects. After that call PrintService.getName() method to get the print service name.

package org.kodejava.print;

import javax.print.PrintService;
import java.awt.print.PrinterJob;

public class PrinterName {
    public static void main(String[] args) {
        // Lookup for the available print services.
        PrintService[] printServices = PrinterJob.lookupPrintServices();

        // Iterates the print services and print out its name.
        for (PrintService printService : printServices) {
            String name = printService.getName();
            System.out.println("Name = " + name);
        }
    }
}

The program will print the installed print service on your machine.

Name = OneNote for Windows 10
Name = OneNote (Desktop)
Name = Microsoft XPS Document Writer
Name = Microsoft Print to PDF
Name = HP LaserJet P1005
Name = Fax

How do I get the default print service?

To look up the default print service we can use the javax.print.PrintServiceLookup class lookupDefaultPrintService() method.

package org.kodejava.print;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

public class GetPrinter {
    public static void main(String[] args) {
        // Gets the default print service for this environment. Null is returned when 
        // no default print service found.
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        if (service != null) {
            String printServiceName = service.getName();
            System.out.println("Print Service Name = " + printServiceName);
        } else {
            System.out.println("No default print service found.");
        }
    }
}

The result of the code snippet above:

Print Service Name = HP LaserJet P1005