Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the location of a printer

Tags:

java

printing

I'm attempting to show a printers location in a dialog. But to my surprise, no print service seems to have a location attribute - although I verified that some of my printers do display a location in the windows printer control panel.

I used this code to print the locations (it always prints "null" for the location). My Java version is 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);
    }
}

}

Is this not supported/implemented by the JRE or am I doing something wrong here? How can I get the location of the printer?

EDIT: Output on my machine is:

\\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: As suggested, I printed out all the attributes:

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;
    }
}

and I got:

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

So, there is no PrinterLocation present for any printer on my machine.

like image 370
Durandal Avatar asked Jun 13 '13 16:06

Durandal


1 Answers

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

Once the printer starts processing the print job, additional information about the job becomes available, which might include: the job state (such as completed or queued) and the number of pages printed so far. These pieces of information are also attributes. Attributes can also describe the printer itself, such as: the printer name, the printer location, and the number of jobs queued.

Not sure if that means it becomes available after it starts a process or not

like image 199
Taylor Tvrdy Avatar answered Oct 14 '22 01:10

Taylor Tvrdy