SPIRO THE TECH GURU

Back

How to Generate Barcodes Using Java, Barcodes Example

By Admin on

Let us see how to generate Barcodes with java, we have different types of barcodes, among them i am going to explain about ‘Code 128‘ type :-)

Files Required

  • BarCode128Java4s.java
  • iText.jar [ Make sure you have iText jar file in your class path ]
  • Install Barcode scanner in your Smart Phone to test, whether its working perfectly or not

BarCode128Java4s.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

package pdf;

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

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfWriter;

public class BarCode128 {

 public static void main(String[] args) throws FileNotFoundException, DocumentException {

    Document document = new Document(new Rectangle(PageSize.A4));    
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/Java4s_BarCode_128.pdf"));    

    document.open();
    document.add(new Paragraph("Code_128 Format_Java4s.com"));

    Barcode128 code128 = new Barcode128();
    code128.setGenerateChecksum(true);
    code128.setCode("1234554321");    

    document.add(code128.createImageWithBarcode(writer.getDirectContent(), null, null));
    document.close();

    System.out.println("Document Generated...!!!!!!");
  }

}



Explanation

I am going to explain much about iText API as we already explained clearly at Creating PDF with Java and iText

  • At line number 19, specifying my output file
  • At line number 24,25,26 created Barcode128 class object and added required data to be appeared in my Barcode
SPIRO Google Plus