在 Java 中如何更改或设置默认打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11787662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:21:28  来源:igfitidea点击:

In Java how do I change or set a default printer

javaprinting

提问by cloudsmurf

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

使用 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

我知道这很容易做到我只想创建一个简单的java程序a:增加我的知识b:这里的老师非常不喜欢玩打印属性

Thanks for your help in advance

提前感谢您的帮助

采纳答案by Badar

This program works in Eclipse.

这个程序在 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();
        }
    }
}

回答by Pankaj Bansal

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.

好的,此代码将帮助您传递要设置为默认打印机的打印机名称,其中“MYPRINTER”。将其替换为打印机名称。

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


回答by Mahmoud Ibrahim

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:

我做了一个解决方法来设置操作系统默认打印机。这个适用于 Windows,它基本上执行一个 cmd 命令,在执行打印代码之前设置默认打印机:

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