How to Read Barcode from Word Documents

Skip to end of metadata
Go to start of metadata
In this article, we will explain how we can use Aspose.BarCode for Java to recognize barcodes from word documents.
Generate Barcode and Insert in Word Document

First, we will use Aspose.BarCode for Java and generate a barcode image. We will save this image to a file on disk. Then, we will use Aspose.Words for Java to create a Microsoft Word document and insert this barcode in that document.

Extract Images from Word Document and Read Barcodes

For the recognition part, we will first extract images from the Word document using Aspose.Words for Java. Once images are extracted, we will pass these images to Aspose.BarCode for Java for barcode recognition.

Following is a complete java program to generate and recognize barcodes from Microsoft Word documents:

[Java]
import com.aspose.barcode.*;
import com.aspose.barcoderecognition.BarCodeReadType;
import com.aspose.barcoderecognition.BarCodeReader;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageType;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;
import java.awt.Toolkit;
import java.text.MessageFormat;

public class RecognitionFromWord {
    public static void main(String[] args)
    {
        try
        {
            // Generate barcode image
            BarCodeBuilder builder = new BarCodeBuilder();
            builder.setSymbologyType(Symbology.Code39Standard);
            builder.setCodeText("test-123");
            String strBarCodeImageSave = "img.jpg";
            builder.save(strBarCodeImageSave);

            // add this image to word doc
            Document doc = new Document();
            DocumentBuilder docBuilder = new DocumentBuilder(doc);
            docBuilder.insertImage(strBarCodeImageSave);
            String strWordFile = "docout.doc";
            doc.save(strWordFile);

            // recognition part
            // extract image from word document
            NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
            int imageIndex = 0;

            for(Shape shape :  shapes)
            {
                if (shape.hasImage())
                {
                    // if this shape is an image, extract image to file
                    String extension = ImageTypeToExtension(shape.getImageData().getImageType());
                    String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
                    String strBarCodeImageExtracted = "" + imageFileName;
                    shape.getImageData().save(strBarCodeImageExtracted);

                    // recognize barcode from this image
                    BarCodeReader reader = new BarCodeReader(Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
                    while (reader.read())
                    {
                        System.out.println("codetext: " + reader.getCodeText());
                    }
                    imageIndex++;
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    private static String ImageTypeToExtension(int imageType) throws Exception
    {
        switch (imageType)
        {
            case ImageType.BMP:
                return "bmp";
            case ImageType.EMF:
                return "emf";
            case ImageType.JPEG:
                return "jpeg";
            case ImageType.PICT:
                return "pict";
            case ImageType.PNG:
                return "png";
            case ImageType.WMF:
                return "wmf";
            default:
                throw new Exception("Unknown image type.");
        }
    }

}
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.