| In this article, we will recognize barcode from a Word document using Aspose.BarCode and Aspose.Words.
We will follow the below steps for reading barcode from the Word document:
|
Below are the details and sample code snippet for each step:
Use Aspose.Words.Document class to open the Word document. Call Document.GetChildNodes() method to get a collection of all the shapes (including images) in the Word document.
In a foreach loop on the collection, check if the shape has an image using Shape.HasImage property and call Shape.ImageData.Save() method and save the image to stream. Initialize Aspose.BarCodeRecognition.BarCodeReader class with the stream and symbology type and then call BarCodeReader.Read() method to read the barcodes from the stream (image).
The sample code snippet is given below:
[C#]
// Load the word document Document wordDocument = new Document("Invitation.doc"); // get all the shapes NodeCollection shapes = wordDocument.GetChildNodes(NodeType.Shape, true, false); // loop through all the shapes foreach (Shape shape in shapes) { // check if it has an image if (shape.HasImage) { // save the image in memory stream MemoryStream imgStream = new MemoryStream(); shape.ImageData.Save(imgStream); // recognize the barcode from the image stream above BarCodeReader reader = new BarCodeReader(new Bitmap(imgStream), BarCodeReadType.Code39Standard); while(reader.Read()) { Console.WriteLine("Codetext found: " + reader.GetCodeText()); } // close the reader reader.Close(); }
[VB.NET]
' Load the word document Dim wordDocument As Document = New Document("Invitation.doc") ' get all the shapes Dim shapes As NodeCollection = wordDocument.GetChildNodes(NodeType.Shape, True, False) ' loop through all the shapes For Each shape As Shape In shapes ' check if it has an image If shape.HasImage Then ' save the image in memory stream Dim imgStream As MemoryStream = New MemoryStream() shape.ImageData.Save(imgStream) ' recognize the barcode from the image stream above Dim reader As BarCodeReader = New BarCodeReader(New Bitmap(imgStream), BarCodeReadType.Code39Standard) Do While reader.Read() Console.WriteLine("Codetext found: " & reader.GetCodeText()) Loop ' close the reader reader.Close() End If Next shape
| The evaluation version of Aspose.BarCode for .NET can only recognize Code39 barcodes. If the image contains barcode of other than Code39 symbology type, a valid license must be set. For getting temporary license for 30 days, please visit http://www.aspose.com/corporate/purchase/temporary-license.aspx for more details. |

