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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s