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;
public class PrintExamples {
public static void main(String[] args) {
printAvailable();
printDefault();
printByName("MiImpresora");
}
public static void printAvailable() {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService : services) {
System.out.println(" ---- IMPRESORA: " + printService.getName());
PrintServiceAttributeSet printServiceAttributeSet = printService.getAttributes();
System.out.println("--- atributos");
Attribute[] a = printServiceAttributeSet.toArray();
for (Attribute unAtribute : a) {
System.out.println("atributo: " + unAtribute.getName());
}
System.out.println("--- viendo valores especificos de los atributos ");
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() {
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);
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName(printName, null));
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