Bar Code Generation In Java Using iText API

You need to download iText.jar from 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 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 “”

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

import java.awt.Color;

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.*/;

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

		PdfPTable table = new PdfPTable(2);

		String myText = ""; // 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,

		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)));

	} catch (Exception de) {

	// step 5: we close the document

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();
	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();
	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();
	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.
	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();
	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();
	Image myBarcodeImagePostnet = myBarcodePostnet.createImageWithBarcode(
			contentByte, null, null);
	return myBarcodeImagePostnet;

/** creating a barcode image using BarCodeInter25 */
public Image createBarcodePostnetPlanet(String myText) {
	BarcodePostnet myBarCodePostnetPlanet = new BarcodePostnet();
	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();
	PdfTemplate ean = myBarcodeEAN.createTemplateWithBarcode(contentByte,
	BarcodeEAN codeSUPP = new BarcodeEAN();
	BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(myBarcodeEAN, codeSUPP);
	Image imageEANSUPP = eanSupp.createImageWithBarcode(contentByte, null,;
	return imageEANSUPP;

[My Video Promotion]
  1. How to extract plain Text from HTML Website easily in Java
  2. How To Find What Java Version You Are Using?
  3. Computing the total, free and usable disk space easily using JDK 1.6
  4. Creating and java.util.sql objects from Calendar object
  5. Bar Code Generation In Java Using iText API
  6. Simple Tag Generator In Java For Music Blogs
  7. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  8. Calling URL Browser From Java Application
  9. Java Reflection Tutorial – List Methods Of A Class
  10. Cleaning Punctuation And White Spaces From A String
  11. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  12. Prime Number Finder In Java
  13. Utility Class For Getting SQL Connection and Closing It
  14. Java Design Pattern – Singleton Pattern
  15. Reading / Writing File in Java and String Manipulation
  16. How to capture a screenshot using Java and save it to a file?
  17. Calculating The Difference Between Two Dates In Terms of Months
  18. Getting The Name Of Currently Logged In User In The System
  19. How To Set And Get System Clipboard Contents In Java
  20. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  21. Generating Nepali Unicode Sequence For Characters
  22. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  23. Replacing non-ASCII characters using Java Regular Expressions
  24. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    } else {
    return false;
    public long getTotal()
    return total;

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    return counter.getTotal();

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply to Jamie Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.