Friday, June 5, 2009

Printing .txt file directly to printer using java

The below code will sent a .txt file directly to printer.

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
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.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class Class3 {


public static void main(String[] args) {

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

/* locate a print service that can handle it */

PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

/* create a print job for the chosen service */

int printnbr = 0;
DocPrintJob pj = pservices[printnbr].createPrintJob();

try {
FileInputStream fis = new FileInputStream("C:\\tmp\\test.txt");
Doc doc = new SimpleDoc(fis, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(pj);

/* print the doc as specified */
pj.print(doc, aset);

}
catch (Exception ex){

ex.printStackTrace();
}
}
}


Some useful links which will discuss deeply about the problem with printing in java and printing jasper reports


http://www.coderanch.com/t/385989/Java-General-intermediate/java/send-raw-data-printer

http://forums.sun.com/thread.jspa?threadID=5343781

http://forums.sun.com/thread.jspa?threadID=613476

http://www.javaworld.com/javaworld/jw-02-2001/jw-0202-print.html

http://www.exampledepot.com/egs/javax.print.attribute.standard/Dest.html

http://www.daniweb.com/forums/thread116599.html#

http://www.java2s.com/Code/Java/Development-Class/PrintingaFile.htm

http://www.exampledepot.com/egs/javax.print.event/JobAttrEvents.html

http://www.java2s.com/Code/Java/2D-Graphics-
GUI/DeterminingWhenaPrintJobHasFinished.htm

Printing an Open office document
http://www.oooforum.org/forum/viewtopic.phtml?t=29466

Printing a Jasper Report
http://blog.marcnuri.com/blog/default/2007/03/22/Choosing-a-printer-programmatically-in-Jasper-Reports

Windows script dealing with printer devices
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/printing/servers/#MonPrintJobStatus.htm

7 comments:

  1. thank you. its very usefull for me. thank u very much

    ReplyDelete
  2. i want print reports from jsp. how can i do?. can u give suggestions?

    ReplyDelete
  3. what is the function of printJobWatcher() ?

    ReplyDelete
  4. Hi,
    I am trying to test your code it give me the below error.
    Error(38,1): cannot find class PrintJobWatcher
    wher I can find the that PrintJobWatcher.

    ReplyDelete
  5. Ill try to use some things and hope it will works, thank you anyway for this post.

    ReplyDelete
  6. Thank you for sharing, I will try it later.

    jasper reports 2d barcodes

    ReplyDelete
  7. hallo, im useing jasper to print

    JasperReport jasper_rep = JasperCompileManager.compileReport("your file .jrxml");
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasper_rep, maps, dataSource);

    PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
    //PrintService print = printerService.printerName(defaultPrinter);

    JRPrintServiceExporter exporter1 = new JRPrintServiceExporter();
    List jasperPrintList = new ArrayList<>();
    jasperPrintList.add(jasperPrint);
    System.out.println("Ini adalah printer "+defaultPrinter);
    exporter1.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); //tell jasper what reports to export
    exporter1.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE,
    defaultPrinter); //tell jasper to export the report to selected printService
    exporter1.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
    defaultPrinter.getAttributes());
    exporter1.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
    Boolean.FALSE); //who doesn't like self-explanatory attributes!
    exporter1.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
    Boolean.FALSE);
    exporter1.exportReport();

    it works to printer dot matrix.

    ReplyDelete