Follow by Email

Thursday, 5 January 2017

Get Installed Printer List using Java, Print a file with default Printer




This post shows that how can we get installed printers list using PrintServiceLookup class


Implementations of this class provide lookup services for print services







package client;

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

public class GetInstalledPrinters {

    public static void main(String[] args) {
        String defPrinter;

        PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, null);

        System.out.println("List of Installed Printers- ");
        for (int i = 0; i < printService.length; i++) {
            String printerName = printService[i].getName();
            System.out.println(i + 1 + ". " + printerName);
        }

        defPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
        System.out.println("\nDefault Printer Name  - " + defPrinter);
    }
}


Print File with Default Printer-



package client;

import java.awt.Desktop;

import java.io.File;
import java.io.IOException;

public class DesktopPrint {
    public DesktopPrint() {
        super();
    }
    public static void main(String[] args) {
       Desktop desktop = Desktop.getDesktop();
            try {    
             desktop.print(new File("D:/Export.txt"));  
           } catch (IOException e) {   
              e.printStackTrace();   
          }
   }
}


1 comment :

  1. The above mentioned code is awesome. i just executed now. opted perfect for my requirements. thank you for sharing useful content.
    we provide oracle fusion financials training in ameerpet

    ReplyDelete