Java – How to Read Barcode from Word, Excel, PowerPoint and PDF
Tags: java, barcode, scan, document
Published: 2023-01-04 02:36:05

**A barcode, consisting of bars and spaces (or dots), is a machine-readable representation of numerals and characters.** Today, barcodes have been widely used in grocery stores, doctor offices, law firms, post offices, retail stores and countless others. Being able to read **Code39**, **Code128**, **PDF417**, **DataMatrix**, **QR**, and other barcodes from various kinds of documents (like Word, Excel, PowerPoint and PDF) can be a real benefit.

In this article, I am going to introduce how to **read barcodes from MS Office documents and PDFs in Java** using [Spire.Office for Java](https://www.e-iceblue.com/Introduce/office-for-java.html). More than 40+ commonly seen 1D or 2D barcode types are supported by this library.

- [Read Barcode from Word in Java](#read-barcode-from-word-in-java)
- [Read Barcode from Excel in Java](#read-barcode-from-excel-in-java)
- [Read Barcode from PowerPoint in Java](#read-barcode-from-powerpoint-in-java)
- [Read Barcode from PDF in Java](#read-barcode-from-pdf-in-java)

## Add Spire.Offfice.jar as Dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.office</artifactId>
        <version>7.12.4</version>
    </dependency>
</dependencies>

```
If you are not using maven, then you can find the required jar files from the zip file available in [this location](https://www.e-iceblue.com/Download/office-for-java.html). Include all the jar files into the application lib folder to run the sample code given in this tutorial.

## Read Barcode from Word in Java

The following are the steps to read barcodes from a specific page of a Word file using Spire.Office for Java.

- Create a **Document** object.
- Load a sample Word file containing barcodes using **Document.loadFromFile()** method.
- Convert the first page into image using **Document.saveToImages(int pageIndex, ImageType type)** method.
- Scan barcodes from the image using **BarcodeScanner.scan()** method and return results in a String array.

```java
import com.spire.barcode.BarcodeScanner;
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;

import java.awt.image.BufferedImage;

public class ReadBarcodeFromWord {

    public static void main(String[] args) {

        //Create a Document object
        Document doc = new Document();

        //Load a sample Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Invoice.docx");

        //Convert the first page to image
        BufferedImage image = doc.saveToImages(0, ImageType.Bitmap);

        //Scan barcode from the image
        String[] data = BarcodeScanner.scan(image);

        //Print out the result
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i]);
        }
    }
}
```

![ReadFromWord](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5g695pr0w72nan3zrld.png)

## Read Barcode from Excel in Java

Below are the steps to read barcodes from a worksheet using Spire.Office for Java.

- Create a **Workbook** object.
- Load a sample Excel file containing barcodes using **Workbook.loadFromFile()** method.
- Get the first worksheet using **Workbook.getWorksheets().get()** method.
- Convert the selected range of the worksheet into image using **XlsWorksheet.saveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn)** method.
- Scan barcodes from the image using **BarcodeScanner.scan()** method and return results in a String array.

```
import com.spire.barcode.BarcodeScanner;
import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.awt.image.BufferedImage;

public class ReadBarcodeFromExcel {

    public static void main(String[] args) {
        
        //Create a Workbook object
        Workbook wb = new Workbook();

        //Load a sample Excel file
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\Invoice.xlsx");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Get all located range from the first worksheet
        CellRange cellRange = sheet.getAllocatedRange();

        //Convert the range to image
        BufferedImage image =  sheet.saveToImage(1,1,cellRange.getLastRow(),cellRange.getLastColumn());

        //Scan barcode from the image
        String[] data = BarcodeScanner.scan(image);

        //Print out the result
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i]);
        }
    }
}
```

![FromExcel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g2ry7h7zf7tajbtde5t.png)

## Read Barcode from PowerPoint in Java
The steps to read barcodes from a PowerPoint slide are as follows.

- Create a **Presentation** object.
- Load a sample PowerPoint file using **Presentation.loadFromFile()** method.
- Get the first slide using **Presentation.getSlides().get()** method.
- Convert the slide into image using **ISlide.savAsImage()** method.
- Scan barcodes from the image using **BarcodeScanner.scan()** method and return results in a String array.

```java
import com.spire.barcode.BarcodeScanner;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadBarcodeFromPowerPoint {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load a sample PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Invoice.pptx");

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Save the first slide as image
        BufferedImage image = slide.saveAsImage();

        //Convert image to stream
        InputStream imageStream = bufferedImageToInputStream(image);

        //Scan barcode from the image stream
        String[] data = BarcodeScanner.scan(imageStream);

        //Print out the result
        for (int i = 0; i < data.length; i++) {
            System.out.println(data[i]);
        }
    }
    public static InputStream bufferedImageToInputStream(BufferedImage image) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        return input;
    }
}
```
![FromPowerPoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k46hp59urd0vvrl3gej9.png)

## Read Barcode from PDF in Java

The following are the steps to read barcodes from a PDF page using Spire.Office for Java.

- Create a **PdfDocument** object.
- Load a sample PDF file using **PdfDocument.loadFromFile()** method.
- Convert the first page into image using **PdfDocument.saveAsImage(int pageIndex, PdfImageType type)** method.
- Scan barcodes from the image using **BarcodeScanner.scan()** method and return results in a String array.

```java
import com.spire.barcode.BarcodeScanner;
import com.spire.license.LicenseProvider;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;

import java.awt.image.BufferedImage;

public class ReadBarcodeFromPdf {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Invoice.pdf");

        //Convert the first page to image
        BufferedImage image = doc.saveAsImage(0, PdfImageType.Bitmap);

        //Scan barcode from the image
        String[] data = BarcodeScanner.scan(image);

        //Print out the result
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i]);
        }
    }
}
```

![FromPDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omripe4dutki2len0q4m.png)

## Note:
If you get a message which reads “Not supported in Spire.Barcode Evaluation version”, it means that a specific barcode type cannot be recognized without a license. You can apply for a one-month temporary from [E-iceblue](https://www.e-iceblue.com/) to get rid of the limitation.