iText generate Barcode and QrCode Example

In this example we generate various types of barcodes in a PDF document using the most popular Java library named iText. iText provides suppport for Barcode128, Barcode39, BarcodeCodabar, BarcodeEAN, BarcodeEANSUPP, BarcodeInter25, BarcodePostnet and BarcodeQRCode. With the help of these classes it is very easy to create an Image from the barcode and then position the image anywhere on the PDF. Here is how ...

iText generate Barcode and QrCode Example
package com.as400samplecode;

import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
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 GenerateBarcodes {

 public static void main(String[] args) {

  String pdfFilename = "SampleCodes.pdf";
  GenerateBarcodes generateInvoice = new GenerateBarcodes();
  generateInvoice.createPDF(pdfFilename);

 }

 private void createPDF (String pdfFilename){

  Document doc = new Document();
  PdfWriter docWriter = null;
  
  try {
   String path = "docs/" + pdfFilename;
   docWriter = PdfWriter.getInstance(doc , new FileOutputStream(path));
   doc.addAuthor("betterThanZero");
   doc.addCreationDate();
   doc.addProducer();
   doc.addCreator("MySampleCode.com");
   doc.addTitle("Sample BarCode and QRCode");
   doc.setPageSize(PageSize.LETTER);

   doc.open();
   PdfContentByte cb = docWriter.getDirectContent();
   
   String myString = "http://www.MySampleCode.com";
   
   
   Barcode128 code128 = new Barcode128();
         code128.setCode(myString.trim());
         code128.setCodeType(Barcode128.CODE128);
         Image code128Image = code128.createImageWithBarcode(cb, null, null);
         code128Image.setAbsolutePosition(10,700);
         code128Image.scalePercent(125);
         doc.add(code128Image);
         
         code128.setCodeType(Barcode128.CODE128_UCC);
         code128Image = code128.createImageWithBarcode(cb, null, null);
         code128Image.setAbsolutePosition(10,650);
         code128Image.scalePercent(125);
         doc.add(code128Image);
         
         BarcodeEAN codeEAN = new BarcodeEAN();
         codeEAN.setCode(myString.trim());
         codeEAN.setCodeType(BarcodeEAN.EAN13);
         Image codeEANImage = code128.createImageWithBarcode(cb, null, null);
         codeEANImage.setAbsolutePosition(10,600);
         codeEANImage.scalePercent(125);
         doc.add(codeEANImage);

         BarcodeQRCode qrcode = new BarcodeQRCode(myString.trim(), 1, 1, null);
         Image qrcodeImage = qrcode.getImage();
         qrcodeImage.setAbsolutePosition(10,500);
         qrcodeImage.scalePercent(200);
         doc.add(qrcodeImage);
         
  }
  catch (DocumentException dex)
  {
   dex.printStackTrace();
  }
  catch (Exception ex)
  {
   ex.printStackTrace();
  }
  finally
  {
   if (doc != null)
   {
    doc.close();
   }
   if (docWriter != null)
   {
    docWriter.close();
   }
  }
 }

 

}

References


8 comments :

  1. that code is helpful, but i'd like to get more than 3 data to barcode

    ReplyDelete
  2. that is what i am looking for.. thanks

    ReplyDelete
  3. Hi very good blog i going to add to marks, i share with you my post in spanish with more information

    http://java-white-box.blogspot.com.ar/2014/09/itext-codigos-de-barras-clases-de-itext.html

    thanks for all!

    ReplyDelete
  4. how to encode using barcode

    ReplyDelete
  5. We are offering Phone System App for your company!! also build website E-Commerce and other website contact us.....

    ReplyDelete
  6. Hi, First of all thanks for this article.
    I have one doubt in order to create BarcodeEAN , you have initialized a object as codeEAN , but i see below you are using the code128 for create image method. Can you please explain this?

    Many thanks.

    ReplyDelete