Barcode Generator in Java

/**

    you need two libraries

    1: Barcode.jar

    2: itextpdf.jar

*/ 

public void createPdf(String filename,String referenceNo){

    Document document = new Document();
PdfWriter documentWriter = null;
try{
documentWriter = PdfWriter.getInstance(document, new FileOutputStream("E:\\Barcode\\"+filename));
document.addAuthor("fahad");
document.addCreationDate();
document.addProducer();
document.addCreator("com.i2p.com");
document.addTitle("Barcode");
document.setPageSize(PageSize.LETTER);
document.open();
PdfContentByte cb = documentWriter.getDirectContent();
Barcode128 barcode128 = new Barcode128();
barcode128.setCode(referenceNo.trim());
barcode128.setCodeType(Barcode128.CODE128);
Image code128Image = barcode128.createImageWithBarcode(cb, null,null);
code128Image.setAbsolutePosition(10,700);
code128Image.scalePercent(125);
document.add(code128Image);


}
catch(Exception e){
e.printStackTrace();
}finally {
if (document!=null){
document.close();
}
if(documentWriter!=null){
documentWriter.close();
}
}
}

Comments

Popular Posts