Source: huide Control Network
In this article, we will use the aspose. barcode and aspose. Words controls to recognize bar codes from Word documents. The procedure is as follows:
1. Use aspose. Words for. Net to extract images from documents
2. Save the extracted image as a stream
3. Pass the image to aspose. barcode for. NET as a stream
4. Read the barcode from the image
[C #]
1234567891011121314151617181920212223 |
// 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]
123456789101112131415161718192021 |
' 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 |