java 爪哇。获取系统默认打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14501170/
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 16:28:42  来源:igfitidea点击:

Java. Get system default printer

javaprinting

提问by MyTitle

It is possible to get default system printer using Java Print Service API?

是否可以使用 Java Print Service API 获取默认系统打印机?

I can get list of all printers using

我可以使用所有打印机的列表

PrintServiceLookup.lookupPrintServices(null, null)

but how to get printer choosed as defaultin system? (In screen-shot below, the default printer is checked (HP Laser Jet)).

但是如何在系统中将打印机选为默认值?(在下面的屏幕截图中,选中了默认打印机(HP Laser Jet))。

choosed default printer

选择的默认打印机

回答by Azodious

You should use PrintServiceLookup

你应该使用 PrintServiceLookup

import javax.print.PrintServiceLookup;
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); 

Acc. to Javadocs:

累积 到 Javadocs:

lookupDefaultPrintServiceLocates the default print service for this environment. This may return null. If multiple lookup services each specify a default, the chosen service is not precisely defined, but a platform native service, rather than an installed service, is usually returned as the default. If there is no clearly identifiable platform native default print service, the default is the first to be located in an implementation-dependent manner.

lookupDefaultPrintService定位此环境的默认打印服务。这可能会返回 null。如果多个查找服务各自指定一个默认值,则不会精确定义所选服务,但通常会返回平台本机服务而不是已安装的服务作为默认值。如果没有明确可识别的平台本机默认打印服务,则以依赖于实现的方式首先定位默认打印服务。

回答by Reimeus

You could use PrintServiceLookup.lookupDefaultPrintService

您可以使用PrintServiceLookup.lookupDefaultPrintService

PrintService service = 
    PrintServiceLookup.lookupDefaultPrintService();
if (service != null) {
    String printServiceName = service.getName();
    System.out.println("Print Service Name is " + printServiceName);
} else {
    System.out.println("No default print service found");
}

回答by stacker

 PrintService service = 
                PrintServiceLookup.lookupDefaultPrintService();