Core Java

iText Barcode Example

In the previous example we studied how to generate watermark in the PDF documents using Itext. In this example, we will learn how we can add Barcode to the PDF using Itext.

Barcodes are now-a-days omnipresent due to their ease of use and reliability. Barcodes are cost effective, take less time to read and versatile. We will have a look at how we can encode data into a Barcode in a PDF Document.

1. Project Setup

We shall use Maven to setup our project. Open eclipse and create a simple Maven project and check the skip archetype selection checkbox on the dialogue box that appears. Replace the content of the existing pom.xml


<project xmlns="" xmlns:xsi=""




That’s all from setting-up project point of view, let’s start with the actual code implementation now:

2. Implementation

package com.jcg.examples;


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class CreateWatermarkedPDF
		public static void main(String[] args)
						Document document = new Document();
						PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
						PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
						Barcode128 barcode128 = new Barcode128();
						Image code128Image = barcode128.createImageWithBarcode(pdfContentByte, null, null);
						code128Image.setAbsolutePosition(10, 700);

						BarcodeEAN barcodeEAN = new BarcodeEAN();
						Image codeEANImage = barcodeEAN.createImageWithBarcode(pdfContentByte, null, null);
						codeEANImage.setAbsolutePosition(20, 600);

						BarcodeQRCode barcodeQrcode = new BarcodeQRCode("", 1, 1, null);
						Image qrcodeImage = barcodeQrcode.getImage();
						qrcodeImage.setAbsolutePosition(20, 500);

				catch (FileNotFoundException e)
				catch (DocumentException e)

We start off by creating instances of com.itextpdf.text.Document and com.itextpdf.text.pdf.PdfWriter classes. Then we create different types of barcode and pass the data we wish to be encoded in the Barcode.

We start off with the type Barcode128. We create an instance of com.itextpdf.text.pdf.Barcode128 and set the appropriate Code type and then create an image from the barcode which is embedded in to the document. Barcode 128 is typically used only for numeric or alpha-numeric data.

Next we create Barcode of the type EAN-13. EAN-13(European/International Article Number) Barcode format is usually compact and hence is used extensively on products with limited surface area. We use com.itextpdf.text.pdf.BarcodeEAN class to generate EAN/IAN Barcodes.

Next is the QR(Quick Response) Barcode. QR code can encapsulate large amounts of data compared to other UPC codes and is very fast. We use com.itextpdf.text.pdf.BarcodeQRCode class to generate QR Barcode

Here’s how the different Barcode formats look like in a PDF document :

Fig 1 : Barcode Types in a Document
Fig 1 : Barcode Types in a Document

Itext supports different variants of types of Barcodes that are described above like the EAN-8, CODE 128 RAW, CODE 128 UCC etc

3. Download the Source Code

Here we studied how to generate and embed Barcode into the PDF Document using Itext library.

You can download the source code of this example here:

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Notify of

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

Inline Feedbacks
View all comments
Back to top button