Draw Barcode in PDF using Java
Tags: java, barcode
Published: 2020-12-14 07:18:50

This article will introduce how to use Free Spire.PDF for Java to add Codebar, Code128A and Code39 in PDF. With this third party API, you can also create Code11, Code128B, Code32, Code39 Extended, Code93 and Code93 Extended barcodes in PDF documents.

**Installation**
**Method 1:** Download the [Free Spire.PDF for Java](https://www.e-iceblue.com/Download/pdf-for-java-free.html) and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

**Method 2:** You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
```

**Code Snippet**
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
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");
    }
}
```
![](https://dev-to-uploads.s3.amazonaws.com/i/4e6xf2bmwt19buomfsl0.png)