try{
System.out.println(" Zebra Print testing!");
// Prepare date to print in dd/mm/yyyy format
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String dateString = format.format(now);
// Search for an installed zebra printer...
// is a printer with "zebra" in its name
PrintService psZebra = null;
String sPrinterName = null;
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < services.length; i++) {
PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class);
sPrinterName = ((PrinterName) attr).getValue();
if (sPrinterName.toLowerCase().indexOf("zebra") >= 0) {
psZebra = services[i];
break;
}
}
if (psZebra == null) {
System.out.println("Zebra printer is not found.");
return;
}
String code="1323";
BufferedImage img=new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
g.setColor(Color.BLACK);
g.drawString(code,5,5);
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
ImageIO.write( img, "jpg", outstream);
byte[] buf=outstream.toByteArray();
String value=new String(buf);
System.out.println("Found printer: " + sPrinterName);
DocPrintJob job = psZebra.createPrintJob();
// Prepare string to send to the printer
String s = "R0,0\n" + // Set Reference Point
"N\n" + // Clear Image Buffer
"ZB\n" + // Print direction (from Bottom of buffer)
"GW160,50,100,100,"+value+"\r\n"+
"Q122,16\n" + // Set label Length and gap
"A160,2,0,3,1,1,N,\"DATA: " + dateString + " - CARUGATE\"\n" +
"B160,30,0,1A,2,7,50,N,\"6120\"\n" +
"A160,92,0,1,1,1,N,\"AIA AGRICOLA IT.ALIMENT.S - 594679/VR \"\n" +
"P1\n"; // Print content of buffer, 1 label
byte[] by = s.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
// MIME type = "application/octet-stream",
// print data representation class name = "[B" (byte array).
Doc doc = new SimpleDoc(by, flavor, null);
System.out.println("Pronti alla stampa");
job.print(doc, null);
System.out.println("Stampa inviata");
} catch (Exception e) {
e.printStackTrace();
}