Bar Code Generation In Java Using iText API

You need to download iText.jar from http://www.lowagie.com/itext in order to be able to run this Barcode Example. This tutorial is about generating various BarCode types using Java and iText API. The generated bar codes will then be exported to a PDF file. Here are the kinds of bar codes we will be generating:

      BarCode
      BarCode 128
      Barcode 39
      BarCode EAN
      BarCode EANS upplemental
      BarCode Inter 25
      BarCode Postnet
      BarCode PostnetPlanet

Here is the code that generates these bar codes for our website “www.sanjaal.com”

/**
 * @ Author Kushal Paudyal
 * Original Code referenced from Lowagie iText tutorial website.
 */
package com.sanjaal.utilities;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.Barcode39;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodeEANSUPP;
import com.lowagie.text.pdf.BarcodeInter25;
import com.lowagie.text.pdf.BarcodePostnet;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class BarCodeGenerator {
PdfContentByte contentByte;

public void generateBarCode() {
	/** Step 1: Create a Document*/
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	try {

		/** Step 2: Create PDF Writer*/
		PdfWriter writer = PdfWriter.getInstance(document,
				new FileOutputStream("sanjaalDotCom_BarCode1.pdf"));

		/** Step 3: Open the document so that we can write over it.*/
		document.open();

		/** Step 4: We have to create a set of contents.*/
		contentByte = writer.getDirectContent();

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.getDefaultCell().setFixedHeight(70);

		String myText = "www.sanjaal.com"; // Text to encode

		table.addCell("CODE 39");
		table.addCell(new Phrase(new Chunk(createBarCode39(myText
				.toUpperCase()), 0, 0)));

		table.addCell("CODE 39 EXTENDED");
		table.addCell(new Phrase(new Chunk(createBarcode39Extended(myText),
				0, 0)));

		table.addCell("CODE 128");
		table.addCell(new Phrase(new Chunk(createBarcode128(myText), 0,
						0)));

		table.addCell("CODE INTERLEAVED");
		String myTextNum = "12345";
		table.addCell(new Phrase(new Chunk(createBarcodeInter25(myTextNum),
				0, 0)));

		table.addCell("CODE POSTNET");
		table.addCell(new Phrase(new Chunk(createBarcodePostnet(myTextNum),
				0, 0)));

		table.addCell("CODE PLANET");
		table.addCell(new Phrase(new Chunk(
				createBarcodePostnetPlanet(myTextNum), 0, 0)));

		String myTextEAN13 = "1234567890123";
		table.addCell("CODE EAN");
		table.addCell(new Phrase(new Chunk(createBarcodeEAN(myTextEAN13),
				0, 0)));

		table.addCell("CODE EANnWITHnSUPPLEMENTAL 5");
		table.addCell(new Phrase(new Chunk(createBARCodeEANSUPP(
				myTextEAN13, "12345"), 0, 0)));

		document.add(table);
	} catch (Exception de) {
		de.printStackTrace();
	}

	// step 5: we close the document
	document.close();
}

public static void main(String args[]) {
	new BarCodeGenerator().generateBarCode();
}

/**
 * Method to create barcode image of type Barcode39 for mytext
 */
public Image createBarCode39(String myText) {
	/**
	 * Code 39 character set consists of barcode symbols representing
	 * characters 0-9, A-Z, the space character and the following symbols:
	 * - . $ / + %
	 */

	Barcode39 myBarCode39 = new Barcode39();
	myBarCode39.setCode(myText);
	myBarCode39.setStartStopText(false);
	Image myBarCodeImage39 = myBarCode39.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImage39;
}

/**Creating a barcode image using Barcode39 extended type for myText*/
public Image createBarcode39Extended(String myText) {
	Barcode39 myBarCode39extended = new Barcode39();
	myBarCode39extended.setCode(myText);
	myBarCode39extended.setStartStopText(false);
	myBarCode39extended.setExtended(true);
	Image myBarCodeImage39Extended = myBarCode39extended
			.createImageWithBarcode(contentByte, null, null);
	return myBarCodeImage39Extended;
}

/** Creating a barcode image using Barcode 128 for myText*/
public Image createBarcode128(String myText) {
	Barcode128 code128 = new Barcode128();
	code128.setCode(myText);
	Image myBarCodeImage128 = code128.createImageWithBarcode(contentByte,
			null, null);
	return myBarCodeImage128;
}

/** Creating a barcode image using BarcodeEAN for myText*/
public Image createBarcodeEAN(String myText) {
	BarcodeEAN myBarcodeEAN = new BarcodeEAN();
	myBarcodeEAN.setCodeType(Barcode.EAN13); // 13 characters.
	myBarcodeEAN.setCode(myText);
	Image myBarCodeImageEAN = myBarcodeEAN.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImageEAN;
}

/** creating a barcode image using BarCodeInter25 for myText*/
public Image createBarcodeInter25(String myText) {
	BarcodeInter25 myBarcode25 = new BarcodeInter25();
	myBarcode25.setGenerateChecksum(true);
	myBarcode25.setCode(myText);
	Image myBarCodeImageInter25 = myBarcode25.createImageWithBarcode(
			contentByte, null, null);
	return myBarCodeImageInter25;
}

/**creating a barcode image using BarcodePostnet for myText*/
public Image createBarcodePostnet(String myText) {
	BarcodePostnet myBarcodePostnet = new BarcodePostnet();
	myBarcodePostnet.setCode(myText);
	Image myBarcodeImagePostnet = myBarcodePostnet.createImageWithBarcode(
			contentByte, null, null);
	return myBarcodeImagePostnet;
}

/** creating a barcode image using BarCodeInter25 */
public Image createBarcodePostnetPlanet(String myText) {
	BarcodePostnet myBarCodePostnetPlanet = new BarcodePostnet();
	myBarCodePostnetPlanet.setCode(myText);
	myBarCodePostnetPlanet.setCodeType(Barcode.PLANET);
	Image myBarCodeImagePostntPlanet = myBarCodePostnetPlanet
			.createImageWithBarcode(contentByte, null, null);
	return myBarCodeImagePostntPlanet;
}

public Image createBARCodeEANSUPP(String myTextPrimary,
		String myTextSupplementary5) {
	PdfTemplate pdfTemplate = contentByte.createTemplate(0, 0);
	BarcodeEAN myBarcodeEAN = new BarcodeEAN();
	myBarcodeEAN.setCodeType(Barcode.EAN13);
	myBarcodeEAN.setCode(myTextPrimary);
	PdfTemplate ean = myBarcodeEAN.createTemplateWithBarcode(contentByte,
			null, Color.blue);
	BarcodeEAN codeSUPP = new BarcodeEAN();
	codeSUPP.setCodeType(Barcode.SUPP5);
	codeSUPP.setCode(myTextSupplementary5);
	codeSUPP.setBaseline(-2);
	BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(myBarcodeEAN, codeSUPP);
	Image imageEANSUPP = eanSupp.createImageWithBarcode(contentByte, null,
			Color.blue);
	return imageEANSUPP;
}
}