查找打印机是否在线并准备好打印 [英] Finding if printer is online and ready to print
问题描述
以下4个问题没有帮助,因此不重复:
在图片中,打印机THERMAL可以打印,但是HPRT PPTII-A(USB)i无法打印。系统告诉我,通过使不可用的打印机着色
使用以下代码,我能够找到系统中的所有打印机
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());
}
}
返回名称;
}
输出:
[HPRT PPTII-A(USB),THERMAL]
问题是:此代码显示系统曾安装的所有打印机。
我需要的是:此列表应仅包含要打印的真正可用的打印机。在这个例子中,它应该只显示THERMAL,而不显示HPRT PPTII-A(USB)
如何实现这一目标?
如果解决方案是特定于Windows的,请尝试
如您所见,我的默认打印机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)
);
}
}
控制台日志如下所示:
名称:WEB.DE Club SmartFax
WorkOffline:False
名称:发送到OneNote 2016
WorkOffline:False
名称:Microsoft XPS文档编写器
WorkOffline:False
名称:Microsoft打印到PDF
WorkOffline:False
名称:Kyocera Mita FS-1010 KX
WorkOffline:True
名称:FreePDF
WorkOffline:False
名称:FinePrint
WorkOffline:False
名称:传真
WorkOffline:False
请注意,Kyocera打印机的 WorkOffline
为 True
。可能这是你想要找到的。
现在进行一些修改以过滤打印机列表以便仅显示活动打印机:
WMI4Java
.get()
.properties(Arrays.asList(Name, WorkOffline))
.filters(Arrays.asList($ _.WorkOffline -eq 0))
.getRawWMIObjectOutput(WMIClass.WIN32_PRINTER)
更新:我被问到如何获取活动打印机名称列表。好吧,由于 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;
公共类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(打印机);
}
}
控制台输出如下所示:
[传真,FinePrint,FreePDF,Microsoft打印到PDF,Microsoft XPS文档编写器,发送到OneNote 2016,WEB .DE Club SmartFax]
The following 4 questions didn't help, therefore this isn't a duplicate:
I need to find a way to discover if the Printer that my system reports is available to print or not.
Printer page:
In the picture, the printer "THERMAL" is available to print, but "HPRT PPTII-A(USB)" isn't available to print. The System shows me that, by making the non-available printer shaded
Using the following code, I'm able to find all the printers in the system
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;
}
output:
[HPRT PPTII-A(USB), THERMAL]
The problem is: This code shows all the printers that the system have ever installed.
What I need: This list should contain only the really available printers to print. In this example, it should only show "THERMAL", and not show "HPRT PPTII-A(USB)"
How can this be achieved?
If it is okay that the solution is Windows-specific, try WMI4Java. Here is my situation:
As you can see, my default printer "Kyocera Mita FS-1010" is inactive (greyed out) because I simply switched it off.
Now add this to your Maven POM:
<dependency>
<groupId>com.profesorfalken</groupId>
<artifactId>WMI4Java</artifactId>
<version>1.4.2</version>
</dependency>
Then it is as easy as this to list all printers with their respective status:
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)
);
}
}
The console log looks as follows:
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
Please note that WorkOffline
is True
for the Kyocera printer. Probably this is what you wanted to find out.
And now a little modification in order to filter the printers list so as to only show active printers:
WMI4Java
.get()
.properties(Arrays.asList("Name", "WorkOffline"))
.filters(Arrays.asList("$_.WorkOffline -eq 0"))
.getRawWMIObjectOutput(WMIClass.WIN32_PRINTER)
Update: I was asked how to get a list of active printer names. Well, this is not so easy due to a shortcoming in WMI4Java for which I have just filed a pull request. It causes us to parse and filter the raw WMI output, but the code is still pretty straightforward:
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);
}
}
The console output looks like this:
[Fax, FinePrint, FreePDF, Microsoft Print to PDF, Microsoft XPS Document Writer, Send To OneNote 2016, WEB.DE Club SmartFax]
这篇关于查找打印机是否在线并准备好打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!