Exquisite-print printer

Below the UN-commented  code is the in which you just give the path of the directory which contains files …. now the code see the files and send all files to the printer.

“I make that code for bulk of files send to printing in one time”

 

 

 
package javaapplication1;/*

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

/**
*
* @author zeeshan
*/
public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, PrintException {
// TODO code application logic here which show u GUI of printer

//        FileInputStream textStream;
//        textStream = new FileInputStream(“C:\\\Users\\Zeeshan\\Desktop\\interestrates.xlsx”);
//        PrintRequestAttributeSet attributes =
//                new HashPrintRequestAttributeSet();
//        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//        Doc mydoc = new SimpleDoc(textStream, flavor, null);
//        PrintService[] services = PrintServiceLookup.lookupPrintServices(
//                flavor, attributes);
//        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//
//        if (services.length == 0) {
//            if (defaultService == null) {
//                //no printer found
//            } else {
//                //print using default
//                DocPrintJob job = defaultService.createPrintJob();
//                job.print(mydoc, attributes);
//
//            }
//
//        } else {
//
//            //built in UI for printing you may not use this
//            PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, attributes);
//
//
//            if (service != null) {
//                DocPrintJob job = service.createPrintJob();
//                job.print(mydoc, attributes);
//            }
//
//        }

try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

/*  Online Printer Lists and select which you like
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0) {
throw new RuntimeException(“No printer services available.”);
}
PrintService ps = pss[3];
*/
/* Default Printer */
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService ps = defaultService;
System.out.println(“Printing to ” + ps);
DocPrintJob job = ps.createPrintJob();
String files;
File folder = new File(“C:\\Users\\Zeeshan\\Desktop\\pics\\”);
File[] listOfFiles = folder.listFiles();

for (int i = 2; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
FileInputStream fin = new FileInputStream(folder.getPath()+”\\” + files);
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

job.print(doc, pras);
fin.close();
System.out.println(files);
}
}

} catch (Exception e) {
e.printStackTrace();
}

}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s