
Barcodes are graphical representations of data, typically composed of parallel lines, dots, or rectangles that encode essential information. They are widely used across various industries for embedding product details, tracking inventory, and managing product movement. In many scenarios, you may need to add a dynamic barcode to a Word document using C#. Microsoft Word supports popular file formats such as DOCX and DOC. In this article, we will explore how to create a barcode in a Word document using C# with Aspose.
Table of Contents
- C# API to Create Barcode in Word Documents
- Generate and Add Barcode to Word Document
- Add Barcode to Existing Word Document
- Add QR Code to Word Document
- Read Barcode from Word Document
C# API to Create Barcode in Word Documents
To read barcode data from a Word document using C# in ASP.NET MVC, we will employ a two-step approach. First, we will utilize the Aspose.Words for .NET API to create or load a Word document. Next, we will generate the barcode image using the Aspose.BarCode for .NET API. The Document class from Aspose.Words allows you to create new Word documents or open existing ones, while the Save() method facilitates saving the document. The DocumentBuilder class provides methods for building documents, including the InsertImage() method for inserting images.
The Aspose.BarCode for .NET API supports various types of barcodes. To generate barcodes, it provides the BarcodeGenerator class, which requires EncodeType and the text to encode as parameters. The generated barcode can be saved using the Save() method. Additionally, the API includes the BarCodeImageFormat enumeration for specifying save formats, and the BarCodeReader class is available for reading barcodes from images.
You can either download the DLLs of the APIs or install them via NuGet:
PM> Install-Package Aspose.BarCode
PM> Install-Package Aspose.Words
Generate and Add Barcode to Word Document in C#
To generate a barcode in a Word document using C# .NET, follow these steps:
- Create an instance of the BarcodeGenerator class with the EncodeType and text to encode as arguments.
- Instantiate a memory stream object.
- Call the Save() method to save the barcode image to the memory stream.
- Create a new instance of the Document class.
- Initialize a DocumentBuilder instance with the Document object.
- Use the InsertImage() method to insert the barcode image using the memory stream.
- Finally, call the Save() method, specifying the output DOCX file path.
The following code example demonstrates how to generate a barcode in a Word document using C# .NET efficiently:

Generate and add Barcode to a new Word document in C#.
Add Barcode to Existing Word Document in C#
To add a barcode to an existing Word document using C#, follow these steps:
- Instantiate the BarcodeGenerator class, providing the EncodeType and text to encode.
- Create a memory stream object.
- Save the barcode image to the memory stream using Save().
- Load the existing Word document with the Document class.
- Initialize a DocumentBuilder instance with the Document object.
- Insert the barcode image using InsertImage() method and the memory stream.
- Call the Save() method with the desired DOCX file path.
The following code example shows how to create a barcode in a Word document with C# .NET:

Add Barcode to Existing Word Document in C#.
Add QR Code to Word Document using C#
To add a QR code image to a Word document, follow these steps:
- Create an instance of the BarcodeGenerator class and set the EncodeType to QR or GS1QR.
- Follow the same steps as above to save and insert the image.
The following example illustrates how to add a QR code to a Word document using C# .NET:

Add QR Code to Word Document using C#.
Read Barcode from Word Document using C#
To read barcode data from a Word document using C#:
- Load the existing Word document with the Document class.
- Access the NodeCollection of Shape types using the GetChildNodes() method.
- Loop through the shapes and check if the shape is an image.
- Save the image to the stream.
- Create a BarCodeReader instance with the image stream and DecodeType parameters.
- Call ReadBarCodes() method to retrieve BarCodeResult.
- Display the barcode information.
The following code example demonstrates how to read barcode data from a Word document using Aspose C#:
Codetext found: 1234567890, Symbology: Code39Standard
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
Conclusion
In this article, we have covered:
- How to create a Word document programmatically.
- Steps to generate a barcode image and add it to the Word document.
- How to create a QR code and insert it into a Word document.
- Techniques to read a barcode image from a Word document in C#.
For more detailed information, refer to the Aspose.BarCode for .NET API documentation. If you have any questions, please feel free to reach out on the forum.