Codabar Barcode
iText is a free and open source library for creating
PDF documents and generating Barcodes in Java.
This requires the library iText-5.0.2.jar to be in classpath.
The following example shows generating Codabar Barcode.
package com.bethecoder.tutorials.itext;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodeCodabar;
import com.itextpdf.text.pdf.PdfWriter;
public class CodabarTest {
/**
* @param args
* @throws DocumentException
* @throws FileNotFoundException
*/
public static void main ( String [] args ) throws FileNotFoundException, DocumentException {
Document document = new Document ( new Rectangle ( PageSize.A4 )) ;
PdfWriter writer = PdfWriter.getInstance ( document, new FileOutputStream ( "codabar.pdf" )) ;
document.open () ;
//Codabar Barcode
document.add ( new Paragraph ( "Codabar Barcode" )) ;
BarcodeCodabar codabar = new BarcodeCodabar () ;
codabar.setGenerateChecksum ( true ) ;
codabar.setCode ( "A12343231D" ) ;
//Add Barcode to PDF document
document.add ( codabar.createImageWithBarcode ( writer.getDirectContent () , null, null )) ;
document.close () ;
}
}
It gives the following output,