| In this article, we will learn about how we can add a barcode image generated by Aspose.BarCode into a Microsoft Word document (*.doc) programmatically by integrating Aspose.BarCode and Aspose.Word components.
Aspose.Word is one of the leading components offered by Aspose Pty Ltd. that allows developers to create and manage Microsoft Word documents programmatically. The discussion about Aspose.Word is beyond the scope of this article. We will only use Aspose.Word in this article to create a Word document and insert the barcode image (generated by Aspose.BarCode) into the document. |
Creating the Barcode
First of all, we will create an object of BarCodeBuilder.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder. Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder as shown below:
[C#]
//Instantiate linear barcode object
BarCodeBuilder builder = new BarCodeBuilder();
//Set the Code text for the barcode
builder.CodeText = "1234567";
//Set the symbology type to Code128
builder.SymbologyType = Symbology.Code128;
[VB.NET]
'Instantiate linear barcode object
Dim builder As BarCodeBuilder = New BarCodeBuilder()
'Set the Code text for the barcode
builder.CodeText = "1234567"
'Set the symbology type to Code128
builder.SymbologyType = Symbology.Code128
The main purpose of this step is to only create an instance of BarCodeBuilder and configure its properties so that it can be used in the next step to create a barcode image using its BitmapImage property.
First of all, we will create an object of BarCodeBuilder.
The desired data to be encoded into barcode, will be assigned to CodeText property of the BarCodeBuilder. Code128 symbology will be selected from the Symbology enumeration and then assigned to SymbologyType property of the BarCodeBuilder as shown below:
//Instantiate linear barcode object BarCodeBuilder builder = new BarCodeBuilder(); //Set the Code text for the barcode builder.CodeText = "1234567"; //Set the symbology type to Code128 builder.SymbologyType = Symbology.Code128;
'Instantiate linear barcode object Dim builder As BarCodeBuilder = New BarCodeBuilder() 'Set the Code text for the barcode builder.CodeText = "1234567" 'Set the symbology type to Code128 builder.SymbologyType = Symbology.Code128
The main purpose of this step is to only create an instance of BarCodeBuilder and configure its properties so that it can be used in the next step to create a barcode image using its BitmapImage property.
Creating a Word Document with Barcode Image
Aspose.Word provides a Document class that represents a Microsoft Word document. We can create an instance of Document class by calling its empty constructor.
To add content to the Document, a DocumentBuilder class is supplied by Aspose.Word. The Document object (in which the barcode image will be added) is passed to the constructor of DocumentBuilder that inserts barcode image into the Word document by calling its InsertImage method. InsertImage method takes the barcode image created by Aspose.BarCode using the BitmapImage property of the BarCodeBuilder object as shown below:
[C#]
//Create a word document object with Aspose.Word
Document doc = new Document();
//Create builder for document object
DocumentBuilder builder = new DocumentBuilder(doc);
//Insert the barCode image into document
builder.InsertImage(builder.BarCodeImage);
//Save the word document
doc.Save("Myfile.doc");
[VB.NET]
'Create a word document object with Aspose.Word
Document(doc = New Document())
Create builder for document object
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
Insert the barCode image into document
builder.InsertImage(builder.BarCodeImage)
Save the word document
doc.Save("Myfile.doc")
Aspose.Word provides a Document class that represents a Microsoft Word document. We can create an instance of Document class by calling its empty constructor.
To add content to the Document, a DocumentBuilder class is supplied by Aspose.Word. The Document object (in which the barcode image will be added) is passed to the constructor of DocumentBuilder that inserts barcode image into the Word document by calling its InsertImage method. InsertImage method takes the barcode image created by Aspose.BarCode using the BitmapImage property of the BarCodeBuilder object as shown below:
//Create a word document object with Aspose.Word Document doc = new Document(); //Create builder for document object DocumentBuilder builder = new DocumentBuilder(doc); //Insert the barCode image into document builder.InsertImage(builder.BarCodeImage); //Save the word document doc.Save("Myfile.doc");
'Create a word document object with Aspose.Word Document(doc = New Document()) Create builder for document object Dim builder As DocumentBuilder = New DocumentBuilder(doc) Insert the barCode image into document builder.InsertImage(builder.BarCodeImage) Save the word document doc.Save("Myfile.doc")
Conclusion 
Integration of Aspose.BarCode with Aspose.Word is rather easy than other Aspose Components. Aspose.Word provides a very simple API that makes it flexible for developers to add barcode functionality in their Word documents.
| Integration of Aspose.BarCode with Aspose.Word is rather easy than other Aspose Components. Aspose.Word provides a very simple API that makes it flexible for developers to add barcode functionality in their Word documents. |
About Author
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.) who obtained his Masters degree in Computer Sciences. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article

