How to Read Barcode in Word, Excel, PowerPoint or PDF Documents in Java

Introduction

A barcode is an image of data that can be read by machines. Barcodes are often used to help organize information or prices about an object. If you own a business, being able to read barcodes such as Code39, Code128, PDF417, EAN-8, EAN-13, DataMatrix and QR from different sources, is important. In this article, you will learn how to read barcode from MS office documents and PDFs in Java by using Spire.Office for Java library.

  • Read Barcode in Word in Java
  • Read Barcode in Excel in Java
  • Read Barcode in PowerPoint in Java
  • Read Barcode in PDF in Java

Add Spire.Office.jar as a Dependency

Spire.Office for Java is a class library for processing Word, Excel, PowerPoint, PDF and many more file formats in Java applications. You can download it from E-iceblue website and add the Spire.Office.jar file in your Java application as a dependency. If you use Maven, you can easily import the jar file in your application using the following configurations.

<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>
        <verson>7.10.1</version>
    </dependency>
</dependencies>

Steps to Read Barcode from Different Documents

Using Spire.Office to read barcode from various types of documents can be very simply. Firstly, we just need to convert these files to BufferedImage, and then use the BarcodeScanner.scan method to detect and recognize all barcodes in the image. The following are the detailed steps.

  • Create an object of the class that represents Word, Excel, PowerPoint or PDF.
  • Load the sample document from file using loadFromFile method. 
  • Convert a specific page, worksheet or slide to image.
  • Read all barcodes from the image using scan method.

Read Barcode in Word in Java

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

import java.awt.image.BufferedImage;

public class ReadBarcodeInWord {
    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\\Barcode.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]);
        }
    }
}

How to Read Barcode in Word, Excel or PDF Documents in Java

Read Barcode in Excel in Java

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

import java.awt.image.BufferedImage;

public class ReadBarcodeInExcel {

    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\\Barcode.xlsx");
        
        //Get all located range from the first worksheet
        CellRange cellRange = wb.getWorksheets().get(0).getAllocatedRange();

        //Convert the range to image
        BufferedImage image =  wb.getWorksheets().get(0).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]);
        }
    }
}

How to Read Barcode in Word, Excel or PDF Documents in Java

Read Barcode in PowerPoint in Java

import com.spire.barcode.BarcodeScanner;
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 ReadBarcodeInPowerPoint {

    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\\barcode.pptx");

        //Save the first slide as image
        BufferedImage image = presentation.getSlides().get(0).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;
    }
}

How to Read Barcode in Word, Excel or PDF Documents in Java

Read Barcode in PDF in 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 ReadBarcodeInPdf {

    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\\Barcode.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]);
        }
    }
}

How to Read Barcode in Word, Excel or PDF Documents in Java

Note
If you get a message that 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 to get rid of the limitation.


Similar Articles