In Java how do I change or set a default printer

12,785

Solution 1

This program works in Eclipse.

import java.awt.print.PageFormat;

import java.awt.print.PrinterJob;

public class PrinterSetup 
{

    public static void main(String[] args) throws Exception
    {
        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat pf = pjob.defaultPage();
        pjob.setPrintable(null, pf);

        if (pjob.printDialog()) {
          pjob.print();
        }
    }
}

Solution 2

You know how to get list of all printers then you want set a default printer.

ok this code will help you can Pass Name of printer which you want to set as default printer where "MYPRINTER" .replace it with name of printer.

PrinterJob pj = PrinterJob.getPrinterJob();
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of printers configured: " + printServices.length);
    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        if (printer.getName().equals("***MYPRINTER***")) {
            try {
                pj.setPrintService(printer);
            } catch (PrinterException ex) {
            }
        }
    }

Solution 3

I made a workaround to set the OS default printer. This one works for windows which basically executes a cmd command that sets the default printer before executing the print code:

Desktop desktop = null
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop()
    desktop.print(file)
 }

Here's my function:

public static boolean setDefaultPrinter(String printerName) {
    String defaultPrinterSetter = "wmic printer where name='"+ printerName +"' call setdefaultprinter";
    try {
        setDefaultPrinterProcess = Runtime.getRuntime().exec("cmd /c start cmd.exe /C " + defaultPrinterSetter);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

All you need to do is pass the printer name to this function and it'll make it the default printer.

Here's a function that gets a list of all avaiable printer services:

public static ArrayList<PrintService> availablePrinters() {
    PrintService[] services = PrinterJob.lookupPrintServices();
    ArrayList<PrintService> allServices = new ArrayList<>();

    for (PrintService myService : services) {
        allServices.add(myService);
    }
    return allServices;
}

And I'll assume you'd want to add a list in maybe a combobox or something for user to choose from. It should go something like this

    ArrayList<PrintService> availableServices = availablePrinters();
    System.out.println("All printers list:");
    for (PrintService myService : availableServices) {
        myCombo.getItems().add(myService.getName());
        System.out.println(myService.getName());
    }
Share:
12,785
cloudsmurf
Author by

cloudsmurf

Updated on June 04, 2022

Comments

  • cloudsmurf
    cloudsmurf almost 2 years

    I know how to get the list of available printers, I want users to be able to select from a list and set that to the default for the session

    Using Windows 7

    I know that this is easily done I just want to create a simple java program a: To increase my knowledge b: Teachers here are very adverse to playing with printing properties

    Thanks for your help in advance

    • cloudsmurf
      cloudsmurf almost 12 years
      I couldn't find it in Google apart from a nonsensical manner. I thought that it would be easier to create and register on forum that i lurked for a year or more and ask for help from a programming user base.
  • cloudsmurf
    cloudsmurf almost 12 years
    Thanks, that is pretty much what i want i want to do. However was hoping to be able to set the default printer without starting a print job. Can Java do this?
  • Badar
    Badar almost 12 years
    The generated dialog box in this program will let you setup printer, for more information, check out this link: docs.oracle.com/javase/tutorial/2d/printing/index.html
  • cloudsmurf
    cloudsmurf almost 12 years
    Thanks mate, Was a little confused now I think I got the hang of it
  • gumuruh
    gumuruh over 2 years
    what about the 3-asteriks symbols ? Is that mandatory?