Getting access to default printer

This post explains how to programmatically get access to a printer configured as default printer in a machine. Java provides PrintServiceLookup class, which provides static methods through which we can get a reference to the printer.

Each printer installed in a machine is represented by an instance of PrintService interface. The PrintService interface provides methods through which we can get details of a particular printer such as its capabilities, create a print job, etc.

In the below example we get a reference to default printer and display a list of document formats the printer can print.

Main Class


package Printing;

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

public class PrintingDemo1 {
    public static void main(String[] args) {
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        System.out.println(printService.getName());
        DocFlavor[] docFlavors = printService.getSupportedDocFlavors();
        for(DocFlavor docFlavor : docFlavors) {
            System.out.println("\t" + docFlavor.toString());
        }
    }
}

Leave a Reply