Ver Mensaje Individual
  #6 (permalink)  
Antiguo 14/02/2011, 11:40
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
Respuesta: imprimir con java y printjob

HackmanC

la solucion q me diste en este post me sirvio de mucho muchas gracias por tu ayuda dejo mi codigo como quedo para q alguna otra persona q tenga el mismo problema tenga la solucion

muchas gracias


Código Java:
Ver original
  1. import com.mysql.jdbc.Connection;
  2. import com.mysql.jdbc.Statement;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.geom.Rectangle2D;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.print.*;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.util.Date;
  14. import java.util.logging.*;
  15. import javax.print.PrintService;
  16. import javax.print.PrintServiceLookup;
  17. import javax.print.attribute.AttributeSet;
  18. import javax.print.attribute.HashAttributeSet;
  19. import javax.print.attribute.standard.PrinterName;
  20. import net.sourceforge.barbecue.*;
  21. import net.sourceforge.barbecue.output.OutputException;
  22.  
  23. public class imprimir_caja {
  24.    int count=0;
  25.     String iden, country, country2, qty, cust_num;
  26.     String item="16U-26084-03";
  27.    boolean band=false;
  28.      String item_no,ident,country_o,country_o2,qty2,cus;
  29.   public static void main(String args[]) throws Exception{
  30.       imprimir_caja obj=new imprimir_caja();
  31.   obj.impresion();
  32.   }  
  33.  private static Connection getConnection() throws Exception {
  34.         Class.forName("com.mysql.jdbc.Driver");
  35.         String url = "jdbc:mysql://localhost/etiquetas";
  36.         return (Connection) DriverManager.getConnection(url, "root", "");
  37.     }
  38.  public void impresion() throws Exception{
  39.       Connection con;
  40.         ResultSet rs;
  41.         Statement smt;
  42.         con = getConnection();
  43.         smt = (Statement) con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  44.         rs = smt.executeQuery("consulta sql");
  45.         rs.first();
  46.         item = rs.getString("item");
  47.         System.out.println(item);
  48.         iden = rs.getString("identifier");
  49.         country = rs.getString("country_origin");
  50.         country2 = rs.getString("country2");
  51.         qty = rs.getString("qty");
  52.         cust_num = rs.getString("customer_num");
  53.         String printName="";
  54. PrinterJob printJob = PrinterJob.getPrinterJob();
  55.   Book book = new Book();
  56.   book.append(new IntroPage(), printJob.defaultPage());
  57.     printJob.setPageable(book);
  58.    int count = 0;
  59.         PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);//con este vemos las impresoras instaladas como mis pruebas estan en red pues por eso uso este
  60.         for (PrintService printService : services) {//este for se quita
  61.             if (count == 1) {//junto con esto
  62.                 printName = printService.getName();//si se le da el nombre a la impresora cambia por printName="nombre impresora"
  63.             }//esto
  64.             count = count + 1;//esto
  65.         }//y esto si se quiere dar el nombre de la impresora
  66.          AttributeSet aset = new HashAttributeSet();
  67.         aset.add(new PrinterName(printName, null));
  68.         services = PrintServiceLookup.lookupPrintServices(null, aset);//busca la impresora
  69.         for (PrintService printService : services) {
  70.             PrintService printers[] = PrintServiceLookup.lookupPrintServices(null, aset);
  71.                 if (printers.length == 1) {
  72.                printJob.setPrintService(printers[0]);//le asignamos la impresora de trabajo al job
  73.              try {
  74.         printJob.print();//imprimimos lo  q esta en el job
  75.       } catch (Exception PrintException) {
  76.         PrintException.printStackTrace();
  77.       }
  78.       }
  79.         }
  80.       }
  81. private class IntroPage implements Printable {
  82.          public int print(Graphics g, PageFormat pageFormat, int page) {
  83.             try {
  84.                 Graphics2D g2d = (Graphics2D) g;
  85.                 java.util.Date fecha = new Date();
  86.                 int iNumero=fecha.getYear();
  87.                 int anio = iNumero%100;
  88.                 int m= fecha.getMonth()+1;
  89.                String mes=""+m+"";
  90.                 if(mes.length()==1){
  91.                 mes="0"+mes;
  92.                 }
  93.                 String julian = anio+mes;
  94.                 String serial = "0001";
  95.                 g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
  96.                 g2d.setPaint(Color.black);
  97.                 Rectangle2D.Double rec_item = new Rectangle2D.Double(1, 1, 227, 84);
  98.                 Rectangle2D.Double rec_q = new Rectangle2D.Double(1, 85, 227, 56);
  99.                 Rectangle2D.Double rec_l = new Rectangle2D.Double(1, 141, 227, 56);
  100.                 Rectangle2D.Double rec_c = new Rectangle2D.Double(1, 197, 227, 30);
  101.                 g2d.draw(rec_item);
  102.                 g2d.draw(rec_q);
  103.                 g2d.draw(rec_l);
  104.                 g2d.draw(rec_c);
  105.                 String country = "HARADA INDUSTRY OF AMERICA";
  106.                 Font countryFont = new Font("Arial", Font.BOLD, 8);
  107.                 String item2 = "ITEM No.";
  108.                 Font itemFont = new Font("Arial", Font.BOLD, 6);
  109.                 String n = "(N)";
  110.                 Font nFont = new Font("Arial", Font.BOLD, 6);
  111.                 String itemn = item;
  112.                 String item3="N"+itemn;
  113.                 Font itemnFont = new Font("Arial", Font.BOLD, 14);
  114.                 System.out.println("el valor de itemn: "+itemn);
  115.                 System.out.println("el valor de item3: "+item3);
  116.                 Barcode barcode = BarcodeFactory.createCode39(item3, false);
  117.                 barcode.setDrawingText(false);
  118.                 barcode.setBarHeight(45);
  119.                 barcode.setBarWidth(1);
  120.                 BufferedImage image = BarcodeImageHandler.getImage(barcode);
  121.                 String quality = "QUALITY";
  122.                 Font qualityFont = new Font("Arial", Font.BOLD, 6);
  123.                 String q = "(Q)";
  124.                 Font qFont = new Font("Arial", Font.BOLD, 6);
  125.                 String qtyn = "Q"+qty;
  126.                 Font qtyFont = new Font("Arial", Font.BOLD, 14);
  127.                 Barcode barcodeqty = BarcodeFactory.createCode39(qtyn, false);
  128.                 barcodeqty.setDrawingText(false);
  129.                 barcodeqty.setBarHeight(42);
  130.                 barcodeqty.setBarWidth(1);
  131.                 BufferedImage imageqty = BarcodeImageHandler.getImage(barcodeqty);
  132.                 String lote = "Lot No.";
  133.                 Font loteFont = new Font("Arial", Font.BOLD, 6);
  134.                 String l = "(L)";
  135.                 Font lFont = new Font("Arial", Font.BOLD, 6);
  136.                 String lotn = julian;
  137.                 Font lotFont = new Font("Arial", Font.BOLD, 14);
  138.                 julian="L"+julian;
  139.                 Barcode barcodelot = BarcodeFactory.createCode39(julian, false);
  140.                 barcodelot.setDrawingText(false);
  141.                 barcodelot.setBarHeight(42);
  142.                 barcodelot.setBarWidth(1);
  143.                 BufferedImage imagelot = BarcodeImageHandler.getImage(barcodelot);
  144.                 String cus = "Customer No.";
  145.                 Font cusFont = new Font("Arial", Font.BOLD, 6);
  146.                 String cust_n = cust_num;
  147.                 Font cus_nFont = new Font("Arial", Font.BOLD, 14);
  148.                 String country2 = "ASSEMBLED IN MEXICO";
  149.                 Font country2Font = new Font("Arial", Font.BOLD, 10);
  150.                 g2d.setFont(countryFont);
  151.                 FontMetrics countryfontMetrics = g2d.getFontMetrics();
  152.                 g2d.drawString(country, 51, 12);
  153.                 g2d.setFont(itemFont);
  154.                 FontMetrics itemfontMetrics = g2d.getFontMetrics();
  155.                 g2d.drawString(item2, 6, 24);
  156.                 g2d.setFont(nFont);
  157.                 FontMetrics nfontMetrics = g2d.getFontMetrics();
  158.                 g2d.drawString(n, 15, 33);
  159.                 g2d.setFont(itemnFont);
  160.                 FontMetrics itemnfontMetrics = g2d.getFontMetrics();
  161.                 g2d.drawString(itemn, 82, 32);
  162.                 g2d.drawImage(image, 18, 37, null);
  163.                 g2d.setFont(qualityFont);
  164.                 FontMetrics qualityfontMetrics = g2d.getFontMetrics();
  165.                 g2d.drawString(quality, 6, 93);
  166.                 g2d.setFont(qFont);
  167.                 FontMetrics qfontMetrics = g2d.getFontMetrics();
  168.                 g2d.drawString(q, 15, 102);
  169.                 g2d.setFont(qtyFont);
  170.                 FontMetrics qtyfontMetrics = g2d.getFontMetrics();
  171.                 g2d.drawString(qty, 115, 96);
  172.                 g2d.drawImage(imageqty, 86, 97, null);
  173.                 g2d.setFont(loteFont);
  174.                 FontMetrics lotefontMetrics = g2d.getFontMetrics();
  175.                 g2d.drawString(lote, 6, 150);
  176.                 g2d.setFont(lFont);
  177.                 FontMetrics lfontMetrics = g2d.getFontMetrics();
  178.                 g2d.drawString(l, 12, 159);
  179.                 g2d.setFont(lotFont);
  180.                 FontMetrics lotfontMetrics = g2d.getFontMetrics();
  181.                 g2d.drawString(lotn, 109, 152);
  182.                 g2d.drawImage(imagelot, 83, 153, null);
  183.                 g2d.setFont(cusFont);
  184.                 FontMetrics cusfontMetrics = g2d.getFontMetrics();
  185.                 g2d.drawString(cus, 6, 204);
  186.                 g2d.setFont(cus_nFont);
  187.                 FontMetrics cust_nfontMetrics = g2d.getFontMetrics();
  188.                 g2d.drawString(cust_n, 80, 209);
  189.                 g2d.setFont(country2Font);
  190.                 FontMetrics country2fontMetrics = g2d.getFontMetrics();
  191.                 g2d.drawString(country2, 76, 225);
  192.             } catch (OutputException ex) {
  193.                 Logger.getLogger(imprimir_caja.class.getName()).log(Level.SEVERE, null, ex);
  194.             } catch (BarcodeException ex) {
  195.                 Logger.getLogger(imprimir_caja.class.getName()).log(Level.SEVERE, null, ex);
  196.             } return PAGE_EXISTS;
  197.    }
  198.   }
  199. }

espero les sirva

Suerte