طباعة ملف بأوامر الجافا Java
صفحة 1 من اصل 1
طباعة ملف بأوامر الجافا Java
طباعة ملف بأوامر الجافا Java
Impressions de fichier en Java
- الكود:
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// lister les imprimantes qui supportent ce flavor
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// choix de l'imprimante
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
try {
FileInputStream fis = new FileInputStream(nom);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
// lancement de l'impression
job.print(doc, pras);
} catch (PrintException ex) {
...
} catch (FileNotFoundException ex) {
...
}
}
}
Ce code ne fonctionne pas pour des fichiers PDF.
2Impression d'un fichier vers un fichier
DocFlavor type( String nom ){
int i = nom.lastIndexOf('.');
String t = nom.substring(i+1).toLowerCase();
if(t.equals("gif"))return DocFlavor.INPUT_STREAM.GIF;
if(t.equals("jpeg"))return DocFlavor.INPUT_STREAM.JPEG;
if(t.equals("jpg"))return DocFlavor.INPUT_STREAM.JPEG;
if(t.equals("png"))return DocFlavor.INPUT_STREAM.PNG;
return null;
}
public void imprimerFichierFichier(String nom, String nomD) {
setCursor(attendre);
DocFlavor flavor = type(nom);
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.LANDSCAPE);
StreamPrintServiceFactory[] factories =
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, psMimeType);
if(factories.length==0) {
JOptionPane.showMessageDialog(this, "pas possible",
"ERREUR", JOptionPane.ERROR_MESSAGE);
return;
}
try {
FileInputStream fis = new FileInputStream(nom);
FileOutputStream fos = new FileOutputStream(nomD);
StreamPrintService sps = factories[0].getPrintService(fos);
DocPrintJob pj = sps.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
pj.print(doc, aset);
} catch (IOException e) {
System.err.println(e);
} catch (PrintException e) {
System.err.println(e);
}
setCursor(normal);
}
مــنـــقــول مــن الــرابــط
imss - www . upmf-grenoble.fr/prevert/Prog/Java/swing/impressionFichier.html#imprimante
دروس في الجافا
imss - www . upmf-grenoble.fr/prevert/Prog
.
sdk11- مبرمج مجتهد
- عدد المساهمات : 108
تاريخ التسجيل : 19/09/2014
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى