Bonjour, après avoir pataugé pas mal pour créer un bout de code permettant d'imprimer en JAVA, j'en viens a un problème qui me bloque : le retour des environnements du processus d'impression, je m'explique.
Je crée un Thread qui lance plusieurs impressions, avec un listener qui écoute (le spooler ?) me permettant de savoir quand l'impression s'est terminée ou pas. Le problème c'est que les seuls évènements que j'arrive à récupérer depuis ce listener sont le NO_MORE_EVENTS et DATA_TRANSFER_COMPLETE. Impossible par contre de récuperer les events JOB_COMPLETE / JOB_FAILED ou encore JOB_CANCELLED, du coup mon thread reste en wait() indéfiniment.
Voici mon code (l'impression est lancée dans une boucle for() pour facilité de test)
La classe d'impression
Le listener
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
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 package print; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.SimpleDoc; public class MyPrintService extends Thread { String name; public MyPrintService(String name) { super(name); this.name = name; System.out.println("MyPrintService("+this.name+")"); this.start(); } public void print() { System.out.println("print()"); try { for (int i = 1; i < 3; i++) { // Open the image file InputStream is = new BufferedInputStream(new FileInputStream("D://giulio.provasi//Desktop//"+i+".txt")); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); // Create the print job DocPrintJob job = service.createPrintJob(); Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null); // Monitor print job events PrintJobWatcher pjDone = new PrintJobWatcher(); job.addPrintJobListener(pjDone); // Print it job.print(doc, null); // Wait for the print job to be done pjDone.waitForDone(); // It is now safe to close the input stream is.close(); } } catch (Exception e) { } } @Override public void run() { System.out.println("run( "+this.name+" )"); this.print(); } }
Le launcher
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
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 package print; import javax.print.event.PrintJobEvent; import javax.print.event.PrintJobListener; public class PrintJobWatcher implements PrintJobListener { Boolean done = false; Integer status = 0; PrintJobWatcher() { System.out.println("PrintJobWatcher()"); } @Override public void printDataTransferCompleted(PrintJobEvent pje) { this.done(PrintJobEvent.DATA_TRANSFER_COMPLETE); System.out.println("DATA_TRANSFER_COMPLETE"); } @Override public void printJobCompleted(PrintJobEvent pje) { this.done(PrintJobEvent.JOB_COMPLETE); System.out.println("JOB_COMPLETE"); } @Override public void printJobFailed(PrintJobEvent pje) { this.done(PrintJobEvent.JOB_FAILED); System.out.println("JOB_FAILED"); } @Override public void printJobCanceled(PrintJobEvent pje) { this.done(PrintJobEvent.JOB_CANCELED); System.out.println("JOB_CANCELED"); } @Override public void printJobNoMoreEvents(PrintJobEvent pje) { this.done(PrintJobEvent.NO_MORE_EVENTS); System.out.println("NO_MORE_EVENTS"); } @Override public void printJobRequiresAttention(PrintJobEvent pje) { this.done(PrintJobEvent.REQUIRES_ATTENTION); System.out.println("REQUIRES_ATTENTION"); } private synchronized void done(Integer status) { System.out.println("DONE !"); this.status = status; this.done = true; notifyAll(); } synchronized void waitForDone() throws InterruptedException { System.out.println("AVANT : IMPRESSION EN COURS..."); try { while (!this.done || ((this.status != PrintJobEvent.JOB_COMPLETE) || (this.status != PrintJobEvent.JOB_FAILED))) { System.out.println("IMPRESSION EN COURS..."); wait(); } } catch (InterruptedException e) {} } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 import print.MyPrintService; public class Main { public static void main(String[] args) { System.out.println("C'est parti !"); MyPrintService test1 = new MyPrintService("1"); } }
Merci d'avance pour tout aide![]()
Partager