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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
public static void imprimerPdf2(String pdfFile, int copiesToPrint, String printerName)
throws PrintException {
FileInputStream fis = null;
boolean found = false;
DocPrintJob printJob = null;
int serv = -1;
boolean copy = false;
DocPrintJob[] tabPrintJob = null;
SimpleDoc[] docs = null;
FileInputStream[] tabFis = null;
SimpleDoc doc = null;
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocFlavor[] fla = null;
PrintService[] service = PrinterJob.lookupPrintServices();
for (int i = 0; (i < service.length) && !found; i++) {
if (service[i].getName().equals(printerName)) {
printJob = service[i].createPrintJob();
found = true;
serv = i;
}
}
if (found) {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
fla = service[serv].getSupportedDocFlavors();
for (int i = 0; i < fla.length; i++) {
log.info("-------------INFO FLAVOR " + i + "------------------");
log.info(fla[i].getMediaSubtype());
log.info(fla[i].getMediaType());
log.info(fla[i].getMimeType());
log.info(fla[i].getRepresentationClassName());
}
if ((!service[serv].isDocFlavorSupported(flavor))) {
log.severe("Flavor non supporte...");
} else {
JobName jobName = new JobName((new File(pdfFile).getName()), null);
if (service[serv].isAttributeValueSupported(jobName, flavor, aset)) {
aset.add(jobName);
}
PageRanges range = new PageRanges("1-10");
if (service[serv].isAttributeValueSupported(range, flavor, aset)) {
aset.add(range);
}
if (copiesToPrint > 1) {
Copies copie = new Copies(copiesToPrint);
SheetCollate collate = SheetCollate.COLLATED;
if (service[serv].isAttributeValueSupported(copie, flavor, aset)
&& service[serv].isAttributeValueSupported(collate, flavor, aset)) {
aset.add(copie);
aset.add(collate);
copy = true;
}
}
try {
if (!copy) {
tabPrintJob = new DocPrintJob[copiesToPrint];
docs = new SimpleDoc[copiesToPrint];
tabFis = new FileInputStream[copiesToPrint];
for (int currentCopy = 0; currentCopy < copiesToPrint; currentCopy++) {
tabFis[currentCopy] = new FileInputStream(pdfFile);
tabPrintJob[currentCopy] = service[serv].createPrintJob();
docs[currentCopy] = new SimpleDoc(tabFis[currentCopy], flavor, null);
}
for (int currentCopy = 0; currentCopy < copiesToPrint; currentCopy++) {
tabPrintJob[currentCopy].print(docs[currentCopy], aset);
}
} else {
fis = new FileInputStream(pdfFile);
doc = new SimpleDoc(fis, flavor, null);
printJob.print(doc, aset);
}
} catch (IOException ioe) {
log.severe("Ioexception PrintPDF.imprimePDF2...");
ioe.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
fis = null;
}
if (doc != null) {
doc = null;
}
if (tabFis != null) {
for (int i = 0; i < tabFis.length; i++) {
if (tabFis[i] != null) {
tabFis[i].close();
tabFis[i] = null;
}
}
}
if (tabPrintJob != null) {
tabPrintJob = null;
}
if (docs != null) {
docs = null;
}
} catch (IOException ioe) {
}
}
}
} else {
log.severe("Probleme de recuperation de l'imprimante " + printerName + " !");
}
} |
Partager