Draw Barcode on PDF in Java

Spire.PDF for Java supports the creation of Codebar, Code11, Code128A, Code128B, Code32, Code39, Code39 Extended, Code93 and Code93 Extended barcodes in PDF. This tutorial give you an example of how to add Codebar, Code128A and Code39 in PDF.

import com.spire.pdf.barcode.*;
import com.spire.pdf.graphics.*;
import static com.spire.pdf.graphics.PdfFontStyle.Bold;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.EnumSet;

public class DrawBarcode {

    public static void main(String[] args) {

        //create a PdfDocument instance 
        PdfDocument doc = new PdfDocument();

        //add a page 
        PdfPageBase page = doc.getPages().add();

        //declare a double variable 
        double y = 15;

        //create font 
        PdfFont font= new PdfFont(PdfFontFamily.Helvetica, 12, EnumSet.of(Bold));

        //draw text "Codebar:" on PDF 
        PdfTextWidget text = new PdfTextWidget();
        text.setFont(font);
        text.setText("Codebar:");
        PdfLayoutResult result = text.draw(page, 0, y);
        y =(float)(result.getBounds().getY()+ result.getBounds().getHeight() + 2);

        //draw Codebar barcode on PDF 
        PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890");
        codebar.setBarcodeToTextGapHeight(1f);
        codebar.setBarHeight(50f);
        codebar.setEnableCheckDigit(true);
        codebar.setShowCheckDigit(true);
        codebar.setTextDisplayLocation(TextLocation.Bottom);
        PdfRGBColor blue = new PdfRGBColor(Color.blue);
        codebar.setTextColor(blue);
        Point2D.Float point = new Point2D.Float();
        point.setLocation(0,y);
        codebar.draw(page,point);
        y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5;

        //draw text "Code128-A:" on PDF 
        text.setText("Code128-A:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        //draw Code128 barcode on PDF 
        PdfCode128ABarcode code128 = new PdfCode128ABarcode("HELLO 00-123");
        code128.setBarcodeToTextGapHeight(1f);
        code128.setBarHeight(50f);
        code128.setTextDisplayLocation(TextLocation.Bottom);
        code128.setTextColor(blue);
        point.setLocation(point.x,y);
        code128.draw(page, point);
        y =code128.getBounds().getY()+ code128.getBounds().getHeight() + 5;

        //draw text "Code39:" on PDF 
        text.setText("Code39:");
        result = text.draw(page, 0, y);
        page = result.getPage();
        y =result.getBounds().getY()+ result.getBounds().getHeight() + 2;

        //draw Code39 barcode on PDF 
        PdfCode39Barcode code39 = new PdfCode39Barcode("16-273849");
        code39.setBarcodeToTextGapHeight(1f);
        code39.setBarHeight(50f);
        code39.setTextDisplayLocation(TextLocation.Bottom);
        code39.setTextColor(blue);
        point.setLocation(point.x,y);
        code39.draw(page, point);

        //save to file 
        doc.saveToFile("DrawBarcode.pdf");
    }
}

Draw Barcode in PDF in Java