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

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>ITextBarcodeExample</groupId>
	<artifactId>ITextBarcodeExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.bouncycastle</groupId>
			<artifactId>bcprov-jdk15on</artifactId>
			<version>1.52</version>
		</dependency>


	</dependencies>

</project>

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

2. Implementation

CreateBarcode.java

package com.jcg.examples;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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)
		{
				try
				{
						Document document = new Document();
						PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
						
						document.open();
						PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
						
						Barcode128 barcode128 = new Barcode128();
						barcode128.setCode("examples.javacodegeeks.com/author/chandan-singh");
						barcode128.setCodeType(Barcode128.CODE128);
						Image code128Image = barcode128.createImageWithBarcode(pdfContentByte, null, null);
						code128Image.setAbsolutePosition(10, 700);
						code128Image.scalePercent(100);
						document.add(code128Image);

						BarcodeEAN barcodeEAN = new BarcodeEAN();
						barcodeEAN.setCodeType(BarcodeEAN.EAN13);
						barcodeEAN.setCode("1234523453323");
						Image codeEANImage = barcodeEAN.createImageWithBarcode(pdfContentByte, null, null);
						codeEANImage.setAbsolutePosition(20, 600);
						codeEANImage.scalePercent(100);
						document.add(codeEANImage);

						BarcodeQRCode barcodeQrcode = new BarcodeQRCode("examples.javacodegeeks.com/author/chandan-singh", 1, 1, null);
						Image qrcodeImage = barcodeQrcode.getImage();
						qrcodeImage.setAbsolutePosition(20, 500);
						qrcodeImage.scalePercent(100);
						document.add(qrcodeImage);

						document.close();
				}
				catch (FileNotFoundException e)
				{
						e.printStackTrace();
				}
				catch (DocumentException e)
				{
						e.printStackTrace();
				}
		}
}

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.

Download
You can download the source code of this example here: ItextBarcodeExample.zip

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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button