How to Generate & Recognize Barcodes from Word Documents in Java Applications

This technical tip shows how Java Developers can read and recognize barcodes from Microsoft Word documents inside their Java apps using Aspose.BarCode for Java. For the purpose of this article, the task has been broken down into two separate tasks. First we will show how to generate a Microsoft Word document, complete with barcode and then read the barcode in the Word document. After that, use Aspose.BarCode for Java to generate a barcode image. Save the image to file and use Aspose.Words for Java to create a Microsoft Word document and insert this barcode in that document. For Recognition first extract images from the Microsoft Word document using Aspose.Words for Java and pass the images to Aspose.BarCode for Java for recognition.
//your code here...Below is a complete Java program that generates and recognizes 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";; // Add the image to a Word doc Document doc = new Document(); DocumentBuilder docBuilder = new DocumentBuilder(doc); docBuilder.insertImage(strBarCodeImageSave); String strWordFile = "docout.doc";; // Recognition part // Extract image from the 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 ( { 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."); } } }


Language: Java | User: Sheraz Khan | Created: Dec 10, 2014 | Tags: recognize barcodes from Word documents, generate barcode image, create Adobe word document, Save the image to file, insert barcode into word document, extract images from word document, pass images to Aspose.BarCode for Java recognize barcodes from Word documents generate barcode image create Adobe word document Save the image to file insert barcode into word document extract images from word document Java Barcode API