Tuesday, November 10, 2009

Bar code generation with print option

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package barcode;

/**
*
* @author amber imam
*/
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;

public class BarcodePrint {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Document document = new Document(PageSize.NOTE, 50, 50, 50, 50);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();

Barcode128 code128 = new Barcode128();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
int no = 5;
String data = "23456 "+no+" " + sdf.format(new Date());
code128.setCodeType(Barcode.CODE128_RAW);
code128.setCode(data.trim());
//code128.setBarHeight(10);
//code128.setSize(20);
Image image128 = code128.createImageWithBarcode(cb, Color.black, Color.blue);
Chunk park = new Chunk("Zoological Park of India , Kolkata",new Font(Font.COURIER, 8,Font.BOLD) );
//park.setBackground(Color.yellow);
//park.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL,0f,Color.red);
document.add(new Phrase(park));
document.add(new Phrase("\n"));
document.add(Chunk.NEWLINE);
document.add(new Phrase(new Chunk(image128, 0, 0)));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk(sdf1.format(new Date()) , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("Rs-250.50/- C4" , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("solution by : KMA infonet solution (p) Ltd." , new Font(Font.COURIER, 6, Font.BOLD))));

document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);

code128.setCodeType(Barcode.CODE128);
code128.setCode(data.trim());

image128 = code128.createImageWithBarcode(cb, Color.black, Color.blue);
park = new Chunk("Zoological Park of India",new Font(Font.TIMES_ROMAN, 8,Font.BOLD) );
//park.setBackground(Color.yellow);
//park.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL,0f,Color.red);
document.add(new Phrase(park));
document.add(new Phrase("\n"));
document.add(Chunk.NEWLINE);
document.add(new Phrase(new Chunk(image128, 0, 0)));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk(sdf1.format(new Date()) , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("Rs-250.50/- C4" , new Font(Font.COURIER, 6, Font.BOLD))));
document.add(new Phrase("\n", new Font(Font.COURIER, 6)));
document.add(new Phrase(new Chunk("solution by : KMA infonet solution (p) Ltd." , new Font(Font.COURIER, 6, Font.BOLD))));


// document.add(Chunk.NEWLINE);

// UPC E
// BarcodeEAN codeEAN = new BarcodeEAN();
// document.add(new Paragraph("Barcode UPC-E"));
// codeEAN.setCodeType(Barcode.UPCE);
// codeEAN.setCode(data.trim());
// document.add(codeEAN.createImageWithBarcode(cb, null, null));
// codeEAN.setBarHeight(codeEAN.getSize() * 3f);



document.close();

String filename = "test.pdf";
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();


//pras.add(MediaSize.findMedia((float) 12, (float) 8, MediaSize.MM));
//Media printable area
// pras.add(new MediaPrintableArea(0, 0, (float) 12, (float) 8,MediaPrintableArea.MM));




DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);


if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
//Thread.sleep(10000);

}
System.exit(0);

}
catch (Exception de)
{
System.out.println(de);
}
}

}

No comments:

Post a Comment