Creating barcodes in a Word document is a useful technique for enhancing productivity and organization. Barcodes facilitate quick scanning and tracking, making them essential for businesses, events, and personal projects.
This article explains two methods for creating barcodes in a Word document using C#: one with barcode fonts via Spire.Doc for .NET API, and the other using a Barcode API alongside the Word API.
- Create Barcodes in a Word Document Using Barcode Fonts
- Create Barcodes in a Word Document Using Barcode API
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Create Barcodes in a Word Document Using Barcode Fonts
A barcode font is a typeface that converts alphanumeric data into a scannable format of bars and spaces. To use it, you typically need to install the font on your system and then format text in a Word document.
The steps to create barcodes in a Word document using barcode fonts are as follows:
- Download and install the desired barcode font on your computer.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific section and add a paragraph using Section.AddParagraph() method.
- Add text to the paragraph using Paragraph.AppendText() method.
- Apply the barcode font to the text using TextRange.CharacterFormat.FontName property.
- Set the font size and color for the text.
- Save the document to a different Word file.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace Name { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); // Get a specific section Section section = document.Sections[0]; // Add a paragraph Paragraph paragraph = section.AddParagraph(); // Append text to the paragraph TextRange txtRang = paragraph.AppendText("Hello,World"); // Apply barcode font to the text txtRang.CharacterFormat.FontName = "Code 128"; // Set the font size and text color txtRang.CharacterFormat.FontSize = 80; txtRang.CharacterFormat.TextColor = Color.Black; // Save the document to a different Word file document.SaveToFile("Barcode.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
Create Barcodes in a Word Document Using Barcode API
Spire.Barcode for .NET is a Barcode API that allows you to easily create a barcode with customized settings, such as barcode type, data, size, and color. You can install the library from NuGet using the following command.
PM> Install-Package Spire.Barcode
After the barcode image is created, you can then insert it to a Word document with the help of the Spire.Doc for .NET library.
The steps to create barcode in a Word document using a Barcode API are as follows:
- Install Spire.Barcode for .NET in your .NET program.
- Create a BarcodeSettings object.
- Specify the barcode type, data, width and other attributes using the properties under the BarcodeSettings object.
- Generate a barcode image based on the settings using BarCodeGenerator.GenerateImage() method.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific section and add a paragraph using Section.AddParagraph() method.
- Add the barcode image to the paragraph using Paragraph.AppendPicture() method.
- Save the document to a different Word file.
- C#
using Spire.Barcode; using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace Name { class Program { static void Main(string[] args) { // Create a BarcodeSettings object BarcodeSettings settings = new BarcodeSettings(); // Set barcode type settings.Type = BarCodeType.QRCode; // Set barcode data settings.Data2D = "Hello, World"; // Set the other attributes of the barcode settings.X = 1.5f; settings.QRCodeECL = QRCodeECL.H; settings.ShowTopText = false; settings.ShowText = false; // Create a BarCodeGenerator object BarCodeGenerator generator = new BarCodeGenerator(settings); // Generate a barcode image Image image = generator.GenerateImage(); // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx"); // Get a specific section Section section = document.Sections[0]; // Add a paragraph Paragraph paragraph = section.AddParagraph(); // Add the barcode image to the paragraph paragraph.AppendPicture(image); // Save the document to a different Word file document.SaveToFile("Barcode.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.