In Java how do I change or set a default printer

£可爱£侵袭症+ 提交于 2020-01-11 07:15:12

问题


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


回答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();
        }
    }
}



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




回答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());
    }


来源:https://stackoverflow.com/questions/11787662/in-java-how-do-i-change-or-set-a-default-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!