如何检测打印机是否连接到您的计算机? [英] How to detect whether Printer is attached to your computer or not in Java?

查看:116
本文介绍了如何检测打印机是否连接到您的计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swing Java创建一个GUI。我必须使用一个按钮打印,它将直接开始打印我设置的文件而不打开默认的打印对话框。
我必须首先检查打印机是否连接到我的电脑?

I am creating one GUI in swing Java. I have to use one button " Print " which will directly start printing the file I have set without opening the default Print dialog box. I have to check first whether printner is attached to my computer or not ?

推荐答案

可能正在使用 PrintServiceLookup


此类的实现为特定类型的打印服务(通常相当于打印机)提供查找服务。

Implementations of this class provide lookup services for print services (typically equivalent to printers) of a particular type.



DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset = new HashPrintRequestHashAttributeSet();
aset.add(MediaSizeName.ISO_A4);
PrintService[] pservices =PrintServiceLookup.lookupPrintServices(flavor, aset);
if (pservices.length > 0) {
    DocPrintJob pj = pservices[0].createPrintJob();
    //....
}

注意:PrintService的数量如果有打印机,应该至少有一个。如果有实际打印机,则可能至少为2,因为您可以在计算机上安装纯软件打印机。另请参见此主题

Note: the number of PrintService should be at least one iff there is a printer. Potentially at least 2 if there is an actual printer, since you can have pure software printers installed on your computer. See also this thread.

根据平台和jdk,它可以一些错误,但除此之外,以下方法应该至少列出打印机:

Depending on the platform and jdk, it can have some bug, but otherwise, the following method is supposed to at least list the printers:

import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import java.text.*;
import javax.print.attribute.standard.*;

public class ShowPrinters {

    public ShowPrinters() {
    }

    public static void main(String[] args) {
        DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        PrintService[] services =PrintServiceLookup.lookupPrintServices(myFormat, aset);
        System.out.println("The following printers are available");
        for (int i=0;i<services.length;i++) {
            System.out.println("  service name: "+services[i].getName());
        }
    }
}






eclipse代码源,你看到使用PrinterState检查打印机是否实际连接:


In this eclipse code source, you have see the use of PrinterState to check if the printer is actually connected:

AttributeSet attributes = new HashPrintServiceAttributeSet(
    new PrinterName(printerName, Locale.getDefault()));
PrintService[] services = PrintServiceLookup.lookupPrintServices(
    DocFlavor.SERVICE_FORMATTED.PRINTABLE,
    attributes);
PrintService printService = services[0];
PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
PrinterState printerState = (PrinterState) printServiceAttributes.get(PrinterState.class);

检查printerState是否为空。注意:这可能并不总是足够(请参阅此主题)。

Check if printerState is not null. Note: this might not be always enough (see this thread).

这篇关于如何检测打印机是否连接到您的计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆