Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/02/2011, 08:29
Avatar de valdo_kof
valdo_kof
 
Fecha de Ingreso: noviembre-2009
Ubicación: San Juan del Rio, Qro
Mensajes: 192
Antigüedad: 14 años, 5 meses
Puntos: 16
imprimir con java y printjob

tengo el siguiente codigo q nme imprime un diseño de una etiqueta para productos con codigos de barras

Código Java:
Ver original
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.geom.Rectangle2D;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.print.*;
  9. import java.util.Date;
  10. import java.util.logging.*;
  11. import net.sourceforge.barbecue.*;
  12. import net.sourceforge.barbecue.output.OutputException;
  13.  
  14. public class imprimir_individual {
  15.     String item_no,ident,country_o,country_o2,qty2,cus;    
  16.     recibir_serial res_ser=new recibir_serial();
  17.    
  18. public void impresion(String item,String cust_num,String iden,String country,String country2,String qty,int valor){
  19.     item_no=item;
  20.     ident=iden;
  21.     country_o=country;
  22.     country_o2=country2;
  23.     qty2=qty;
  24.     cus=cust_num;
  25.     if(valor==1){
  26. PrinterJob printJob = PrinterJob.getPrinterJob();
  27.   Book book = new Book();
  28.   book.append(new IntroPage(), printJob.defaultPage());
  29.     printJob.setPageable(book);
  30.     if (printJob.printDialog()) {
  31.       try {
  32.         printJob.print();
  33.       } catch (Exception PrintException) {
  34.         PrintException.printStackTrace();
  35.       }
  36.     }
  37.     }
  38. }
  39.   private class IntroPage implements Printable {
  40.  
  41.          public int print(Graphics g, PageFormat pageFormat, int page) {
  42.             try {
  43.                 Graphics2D g2d2 = (Graphics2D) g;
  44.                  java.util.Date fecha = new Date();
  45. int [] diasDeMeses = new int [] {31,28,31,30,31,30,31,31,30,31,30,31,30,31};
  46. String [] numeroDeMeses = new String [] {"","1","2","3","4","5","6","7","8","9","10","11","12"};
  47. int mes = fecha.getMonth();
  48. System.out.println("mes: "+mes);
  49. int dia = fecha.getDay();
  50. int dia2=0;
  51. int dias = -1;
  52. for( int i = 0; i<mes; i++ ){
  53.     dia2=diasDeMeses[i];
  54. dias =dias + dia2;
  55. }
  56. dias += dia;
  57. String dias_acum=""+dias+"";
  58. if(dias_acum.length()==1){
  59. dias_acum="00"+dias_acum;
  60. }
  61. if(dias_acum.length()==2){
  62. dias_acum="0"+dias_acum;
  63. }
  64.                
  65.                 int iNumero=fecha.getYear();
  66.                 int anio = iNumero%10;              
  67.                                 g2d2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
  68.                 g2d2.setPaint(Color.black);
  69.                 Rectangle2D.Double rec_ind = new Rectangle2D.Double(1, 1, 144, 203); //se tiene q quitar
  70.                 g2d2.draw(rec_ind);
  71.                 String customer = cus;
  72.                 Font customerFont = new Font("Arial", Font.BOLD, 14);
  73.                 String julian_ind = anio+dias_acum;
  74.                 System.out.println("julian: "+julian_ind);
  75.                 Font julian_iFont = new Font("Arial", Font.BOLD, 10);
  76.                 Barcode barcode_c = BarcodeFactory.createCode39(cus, false);
  77.                 barcode_c.setDrawingText(false);
  78.                 barcode_c.setBarHeight(35);
  79.                 barcode_c.setBarWidth(1);
  80.                 BufferedImage image_c = BarcodeImageHandler.getImage(barcode_c);
  81.                 g2d2.setFont(julian_iFont);
  82.                 FontMetrics julian_ifontMetrics = g2d2.getFontMetrics();
  83.                 g2d2.drawString(julian_ind, 64, 16);
  84.                 g2d2.drawImage(image_c, 10, 21, null);
  85.                 g2d2.setFont(customerFont);
  86.                 FontMetrics customerfontMetrics = g2d2.getFontMetrics();
  87.                 g2d2.drawString(customer, 41, 84);
  88.                 ////
  89.                 g2d2.setFont(julian_iFont);
  90.                 FontMetrics julian_i2fontMetrics = g2d2.getFontMetrics();
  91.                 g2d2.drawString(julian_ind, 64, 200);
  92.                 g2d2.drawImage(image_c, 10, 145, null);
  93.                 g2d2.setFont(customerFont);
  94.                 FontMetrics customer2fontMetrics = g2d2.getFontMetrics();
  95.                 g2d2.drawString(customer, 43, 141);
  96.  
  97.             } catch (OutputException ex) {
  98.                 Logger.getLogger(imprimir_individual.class.getName()).log(Level.SEVERE, null, ex);
  99.             } catch (BarcodeException ex) {
  100.                 Logger.getLogger(imprimir_individual.class.getName()).log(Level.SEVERE, null, ex);
  101.             } return PAGE_EXISTS;
  102.    }
  103.   }
  104. }

el asunto es q con este codigo se me abre un cuadro de dialogo en el q escojo la impresora a la que quiero mandar a imprimir con este codigo q esta en el de arriba

PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage(), printJob.defaultPage());
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}

y si lo dejo asi

PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage(), printJob.defaultPage());
printJob.setPageable(book);
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}

me manda imprimir directamente a la impresora q tengo predeterminada en mi pc

mi pregunta es alguien sabe como puedo mandar a imprimir a una impresora x q no sea la predeterminada

quiero algo como esto no se si se pueda asi pero es un ejemplo de como quiero hacerle

PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage(), printJob.defaultPage());
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print("hp laserject P1002");//aqui darle el nombre de la impresora
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}

o algo q se le paresca

espero haberme explicado y puedan ayudarme gracias