Foro de elhacker.net

Programación => Java => Mensaje iniciado por: mapers en 12 Febrero 2015, 05:28 am



Título: Imprimir desde Java
Publicado por: mapers en 12 Febrero 2015, 05:28 am
Buenas señores del foro necesito de su sabiduria; tengo una impresora epson tm h6000 qquisiera saber como poder mandar para que imprima los tickets.Si tuvieran un ejemplo de donde guiarme se lo agradeceria .


Título: Re: Imprimir desde Java
Publicado por: Usuario Invitado en 12 Febrero 2015, 06:10 am
Ejemplo:

Código
  1. import java.io.*;
  2. import javax.print.*;
  3.  
  4. public class PrintTest {
  5. public static void main(String[] args) throws IOException {
  6.  //we are going to print "printtest.txt" file which is inside working directory
  7.  File file = new File("printtest.txt");
  8.  
  9.  //Discover the default print service. If you call PrintServiceLookup.lookupPrintServices
  10.  //then it will return an array of print services available and you can choose a
  11.  //printer from them
  12.  PrintService service = PrintServiceLookup.lookupDefaultPrintService();
  13.  
  14.  //Doc flavor specifies the output format of the file (Mime type + Char encoding)
  15.  //You can retrieve doc flavors supported by the printer, like this
  16.  //DocFlavor [] supportedFlavors = service.getSupportedDocFlavors();
  17.  DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;  
  18.  
  19.  
  20.  // Create the print job
  21.  DocPrintJob job = service.createPrintJob();
  22.  //Create the Doc. You can pass set of attributes(type of PrintRequestAttributeSet) as the
  23.  //3rd parameter specifying the page setup, orientation, no. of copies, etc instead of null.
  24.  Doc doc = new SimpleDoc(is, flavor, null);
  25.  
  26.  //Order to print, (can pass attributes instead of null)
  27.  try {
  28.   job.print(doc, null);
  29.  } catch (PrintException e) {
  30.   e.printStackTrace();
  31.  }  
  32.  
  33.  //DocPrintJob.print() is not guaranteed to be synchronous. So it's better to wait on completion
  34.  //of the print job before closing the stream. (See the link below)
  35.  is.close();
  36.  System.out.println("Printing done....");
  37. }
  38.  
  39. }

Leer más: http://kalanir.blogspot.com/2010/10/how-to-print-from-java-jps-javaxprint.html