【问题标题】:How to obtain the location of a printer如何获取打印机的位置
【发布时间】:2013-06-10 04:08:46
【问题描述】:

我正在尝试在对话框中显示打印机位置。但令我惊讶的是,似乎没有打印服务具有位置属性 - 尽管我验证了我的一些打印机确实在 Windows 打印机控制面板中显示了位置。

我使用此代码打印位置(它总是为位置打印“null”)。我的 Java 版本是 1.7.0_21:

public class PrintLocation {

public static void main(String[] argv) {
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    for (PrintService service : services) {
        Object location = service.getAttribute(PrinterLocation.class);
        System.out.println(service.getName() + " - " + location);
    }
}

}

这不是 JRE 支持/实现的,还是我在这里做错了什么? 如何获取打印机的位置?

编辑:我机器上的输出是:

\\srv51\SIR-2725-01_KX_color - null
\\srv51\SIR-2725-01_KX_sw - null
Microsoft XPS Document Writer - null
Microsoft Office Document Image Writer - null
FreePDF XP - null

EDIT2:按照建议,我打印出了所有属性:

PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService service : services) {
    PrintServiceAttributeSet attrs = service.getAttributes();
        System.out.println("Service: " + service.getName());
        int i = 1;
        for (Object attr : attrs.toArray()) {
        System.out.println("Attr #" + i + ": " + attr.getClass().getSimpleName()
            + ", " + attr);
        ++i;
    }
}

我得到了:

Service: \\srv51\SIR-2725-01_KX_color
Attr #1: ColorSupported, supported
Attr #2: PrinterName, \\srv51\SIR-2725-01_KX_color
Attr #3: QueuedJobCount, 0
Attr #4: PrinterIsAcceptingJobs, accepting-jobs
Service: \\srv51\SIR-2725-01_KX_sw
Attr #1: ColorSupported, supported
Attr #2: PrinterName, \\srv51\SIR-2725-01_KX_sw
Attr #3: QueuedJobCount, 0
Attr #4: PrinterIsAcceptingJobs, accepting-jobs
Service: Microsoft XPS Document Writer
Attr #1: ColorSupported, supported
Attr #2: PrinterName, Microsoft XPS Document Writer
Attr #3: QueuedJobCount, 0
Attr #4: PrinterIsAcceptingJobs, accepting-jobs
Service: Microsoft Office Document Image Writer
Attr #1: ColorSupported, not-supported
Attr #2: PrinterName, Microsoft Office Document Image Writer
Attr #3: QueuedJobCount, 0
Attr #4: PrinterIsAcceptingJobs, accepting-jobs
Service: FreePDF XP
Attr #1: ColorSupported, supported
Attr #2: PrinterName, FreePDF XP
Attr #3: QueuedJobCount, 0
Attr #4: PrinterIsAcceptingJobs, accepting-jobs

因此,我的机器上的任何打印机都不存在 PrinterLocation。

【问题讨论】:

  • 尝试使用getAttributes() 打印出属性,看看PrinterLocation 是否存在。
  • @selig 好主意,结果它甚至不存在。

标签: java printing


【解决方案1】:

来自http://download.java.net/jdk8/docs/api/javax/print/attribute/package-summary.html

一旦打印机开始处理打印作业,有关该作业的其他信息就会变得可用,其中可能包括:作业状态(例如已完成或排队)和到目前为止打印的页数。这些信息也是属性。属性还可以描述打印机本身,例如:打印机名称、打印机位置和排队的作业数。

不确定这是否意味着它在启动进程后变得可用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多