Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[JAVA] Imprimer un fichier texte

1 réponse
Avatar
Superman
Re bonjour,

Voici le code source que j'ai d=E9velopp=E9 mais il ne fonctionne pas...
j'arrive a obtenir la page d'impression pour choisir son imprimante
etc..
Mais une fois que je clique sur "IMPRIMER" il ne se passe rien..

Savez vous d'ou viens le probleme ?

Voici mon code :

[code]
String filename =3D ("C:\\test.txt");
PrintRequestAttributeSet pras =3D new
HashPrintRequestAttributeSet();
DocFlavor flavor =3D DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] =3D
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =3D
PrintServiceLookup.lookupDefaultPrintService();
PrintService service =3D ServiceUI.printDialog(null, 200,
200,printService, defaultService, flavor, pras);
if (service !=3D null) {
DocPrintJob job =3D service.createPrintJob();
FileInputStream fis;
try {
fis =3D new FileInputStream(filename);
DocAttributeSet das =3D new HashDocAttributeSet();
Doc doc =3D new SimpleDoc(fis, flavor, das);
try {
job.print(doc, pras);
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (PrintException ex) {
ex.printStackTrace();
}

} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
[/code]

1 réponse

Avatar
TestMan
Re bonjour,

Voici le code source que j'ai développé mais il ne fonctionne pas...
j'arrive a obtenir la page d'impression pour choisir son imprimante
etc..
Mais une fois que je clique sur "IMPRIMER" il ne se passe rien..

Savez vous d'ou viens le probleme ?

Voici mon code :

[code]
String filename = ("C:test.txt");
PrintRequestAttributeSet pras = new
HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] > PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService > PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200,
200,printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis;
try {
fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
try {
job.print(doc, pras);
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (PrintException ex) {
ex.printStackTrace();
}

} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
[/code]



Bonjour,

Avez-vous testé l'exemple ? :
http://java.sun.com/javase/6/docs/api/javax/print/PrintService.html

En remplaçant le .INPUT_STREAM.POSTSCRIPT par un .READER par exemple,
directement issus d'un BufferedReader avec le bon code de page de
positioné pour convertir le flux entrant du fichier ...

A++
TM