Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Currently retrieving the default printer installed on my machine for printing. I want to be able to pick which printer the documents go to. What is the best method of doing this ?

Code:

 PrintService[] services =
                PrintServiceLookup.lookupPrintServices(psInFormat, null);
        System.out.println("Printer Selected " + services[Printerinx]);

        //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

        DocFlavor[] docFalvor = services[Printerinx].getSupportedDocFlavors();
        for (int i = 0; i < docFalvor.length; i++) {
            System.out.println(docFalvor[i].getMimeType());
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
633 views
Welcome To Ask or Share your Answers For Others

1 Answer

The PrintServiceLookup.lookupPrintService() method provides a way to lookup a printer by name. Prepare a HashAttributeSet with a PrinterName attribute object and pass it to the lookup method.

Use code like below:

AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName("MyPrinter", null));
PrintService[] pservices = 
    PrintServiceLookup.lookupPrintServices(null, aset);

Works on Linux with CUPS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...