java seleccionar impresora code example

Example: java seleccionar impresora

1import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.PrinterInfo;
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
import javax.print.attribute.standard.PrinterLocation;
import javax.print.attribute.standard.PrinterMakeAndModel;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.standard.PrinterState;

/**
 * Ejemplos para ver tus impresoras
 * @author Peiretti
 */
public class PrintExamples {

    /**
     * @param args
     */
    public static void main(String[] args) {

        printAvailable();
        printDefault();
        printByName("MiImpresora");
    }

    public static void printAvailable() {

        // busca los servicios de impresion...
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

        // -- ver los atributos de las impresoras...
        for (PrintService printService : services) {

            System.out.println(" ---- IMPRESORA: " + printService.getName());

            PrintServiceAttributeSet printServiceAttributeSet = printService.getAttributes();

            System.out.println("--- atributos");

            // todos los atributos de la impresora
            Attribute[] a = printServiceAttributeSet.toArray();
            for (Attribute unAtribute : a) {
                System.out.println("atributo: " + unAtribute.getName());
            }

            System.out.println("--- viendo valores especificos de los atributos ");

            // valor especifico de un determinado atributo de la impresora
            System.out.println("PrinterLocation: " + printServiceAttributeSet.get(PrinterLocation.class));
            System.out.println("PrinterInfo: " + printServiceAttributeSet.get(PrinterInfo.class));
            System.out.println("PrinterState: " + printServiceAttributeSet.get(PrinterState.class));
            System.out.println("Destination: " + printServiceAttributeSet.get(Destination.class));
            System.out.println("PrinterMakeAndModel: " + printServiceAttributeSet.get(PrinterMakeAndModel.class));
            System.out.println("PrinterIsAcceptingJobs: " + printServiceAttributeSet.get(PrinterIsAcceptingJobs.class));

        }

    }

    public static void printDefault() {

        // tu impresora por default
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        System.out.println("Tu impresora por default es: " + service.getName());

    }

    public static void printByName(String printName) {

        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

        // buscar por el nombre de la impresora (nombre que le diste en tu S.O.)
        // en "aset" puedes agregar mas atributos de busqueda
        AttributeSet aset = new HashAttributeSet();
        aset.add(new PrinterName(printName, null));
        //aset.add(ColorSupported.SUPPORTED); // si quisieras buscar ademas las que soporten color

        services = PrintServiceLookup.lookupPrintServices(null, aset);
        if(services.length == 0){
            System.out.println("No se encontro impresora con nombre " + printName);
        }
        for (PrintService printService : services) {
            System.out.println(printService.getName());
        }
    }
}
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

Tags:

Java Example