Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how do I change or set a default printer

Tags:

java

printing

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

like image 927
cloudsmurf Avatar asked Jan 28 '26 18:01

cloudsmurf


2 Answers

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) {
            }
        }
    }

like image 176
Pankaj Bansal Avatar answered Jan 30 '26 08:01

Pankaj Bansal


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();
        }
    }
}
like image 36
Badar Avatar answered Jan 30 '26 10:01

Badar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!