以下4个问题没有帮助,因此这不是重复:
我需要找到一种方法来发现我的系统报告的打印机是否可以打印。
打印页面:
在图片中,打印机" THERMAL"可以打印,但是" HPRT PPTII-A(USB)"无法打印。系统通过使不可用的打印机阴影
向我显示使用以下代码,我能够找到系统中的所有打印机
public static List<String> getAvailablePrinters() {
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
ArrayList<String> names = new ArrayList<String>();
for (PrintService p : services) {
Attribute at = p.getAttribute(PrinterIsAcceptingJobs.class);
if (at == PrinterIsAcceptingJobs.ACCEPTING_JOBS) {
names.add(p.getName());
}
}
return names;
}
输出:
[HPRT PPTII-A(USB), THERMAL]
问题是:此代码显示系统曾安装过的所有打印机。
我需要的是:此列表应仅包含要打印的真正可用的打印机。在这个例子中,它应该只显示&#34; THERMAL&#34;,而不显示&#34; HPRT PPTII-A(USB)&#34;
如何实现这一目标?
答案 0 :(得分:4)
如果该解决方案是特定于Windows的,请尝试WMI4Java。这是我的情况:
正如您所看到的,我的默认打印机“Kyocera Mita FS-1010”处于非活动状态(灰色),因为我只是将其关闭。
现在将其添加到您的Maven POM:
<dependency>
<groupId>com.profesorfalken</groupId>
<artifactId>WMI4Java</artifactId>
<version>1.4.2</version>
</dependency>
然后,就可以轻松列出所有具有各自状态的打印机:
package de.scrum_master.app;
import com.profesorfalken.wmi4java.WMI4Java;
import com.profesorfalken.wmi4java.WMIClass;
import java.util.Arrays;
public class Printer {
public static void main(String[] args) {
System.out.println(
WMI4Java
.get()
.properties(Arrays.asList("Name", "WorkOffline"))
.getRawWMIObjectOutput(WMIClass.WIN32_PRINTER)
);
}
}
控制台日志如下所示:
Name : WEB.DE Club SmartFax
WorkOffline : False
Name : Send To OneNote 2016
WorkOffline : False
Name : Microsoft XPS Document Writer
WorkOffline : False
Name : Microsoft Print to PDF
WorkOffline : False
Name : Kyocera Mita FS-1010 KX
WorkOffline : True
Name : FreePDF
WorkOffline : False
Name : FinePrint
WorkOffline : False
Name : Fax
WorkOffline : False
请注意,Kyocera打印机WorkOffline
为True
。可能这是你想要找到的。
现在进行一些修改,以便过滤打印机列表,以便只显示活动的打印机:
WMI4Java
.get()
.properties(Arrays.asList("Name", "WorkOffline"))
.filters(Arrays.asList("$_.WorkOffline -eq 0"))
.getRawWMIObjectOutput(WMIClass.WIN32_PRINTER)
更新:我被问到如何获取活动打印机名称列表。好吧,由于我刚刚提交了拉取请求的shortcoming in WMI4Java,这并不容易。它导致我们解析和过滤原始WMI输出,但代码仍然非常简单:
package de.scrum_master.app;
import com.profesorfalken.wmi4java.WMI4Java;
import com.profesorfalken.wmi4java.WMIClass;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Printer {
public static void main(String[] args) {
String rawOutput = WMI4Java
.get()
.properties(Arrays.asList("Name", "WorkOffline"))
.filters(Arrays.asList("$_.WorkOffline -eq 0"))
.getRawWMIObjectOutput(WMIClass.WIN32_PRINTER);
List<String> printers = Arrays.stream(rawOutput.split("(\r?\n)"))
.filter(line -> line.startsWith("Name"))
.map(line -> line.replaceFirst(".* : ", ""))
.sorted()
.collect(Collectors.toList());
System.out.println(printers);
}
}
控制台输出如下所示:
[Fax, FinePrint, FreePDF, Microsoft Print to PDF, Microsoft XPS Document Writer, Send To OneNote 2016, WEB.DE Club SmartFax]
答案 1 :(得分:2)
Windows解决方案,查询WMI&#34; win32_printer&#34;对象:
public static void main(String[] args) {
// select printer that have state = 0 and status = 3, which indicates that printer can print
ProcessBuilder builder = new ProcessBuilder("powershell.exe", "get-wmiobject -class win32_printer | Select-Object Name, PrinterState, PrinterStatus | where {$_.PrinterState -eq 0 -And $_.PrinterStatus -eq 3}");
String fullStatus = null;
Process reg;
builder.redirectErrorStream(true);
try {
reg = builder.start();
fullStatus = getStringFromInputStream(reg.getInputStream());
reg.destroy();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print(fullStatus);
}
要将InputStream转换为String,请在此处查看:comprehensive StackOverflow answer,或者您只需使用:
public static String getStringFromInputStream(InputStream is) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while ((length = is.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
} catch (IOException e1) {
e1.printStackTrace();
}
// StandardCharsets.UTF_8.name() > JDK 7
String finalResult = "";
try {
finalResult = result.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return finalResult;
}
输出:
Name PrinterState PrinterStatus
---- ------------ -------------
Foxit Reader PDF Printer 0 3
Send to OneNote 2010 0 3
Microsoft XPS Document Writer 0 3
Microsoft Print to PDF 0 3
Fax 0 3
\\192.168.50.192\POS_PRINTER 0 3
如您所见,您现在拥有字符串中处于工作状态的所有打印机。
您可以使用现有方法(getAvailablePrinters()),例如添加这样的东西:
ArrayList<String> workingPrinter = new ArrayList<String>();
System.out.println("Working printers:");
for(String printer : getAvailablePrinters()){
if(fullStatus.contains("\n" + printer + " ")){ // add a newline character before the printer name and space after so that it catches exact name
workingPrinter.add(printer);
System.out.println(printer);
}
}
现在你将有一个很好的工作打印机列表。
控制台输出:
Working printers:
Send to OneNote 2010
Foxit Reader PDF Printer
Microsoft XPS Document Writer
Microsoft Print to PDF
Fax
\\192.168.50.192\POS_PRINTER
当然,你必须小心这种方法的名字 - 例如如果&#34; POS_PRINTER&#34;在所有打印机中但不在工作打印机列表中,如果有一个名为&#34; POS_PRINTER 1&#34;的工作打印机,它仍然可以添加到workingPrinters列表中。因为该名称包含&#34; \ nPOS_PRINTER&#34;串...