Take any products from shelf in supermarket. You can find the graphic which is formed with black/blank stripes and numbers on products. Now, you come to checkstand and put your products on the desk. The cashier scans the graphic and the products price is presented. So, do you know what the graphic is used to do? It is barcode, which shows much information of one product, including country, manufacturer, name, price, date to produce etc. Not only used in supermarket, barcode is also widely used in library, postal management, banking system.
So, how to create a barcode? Of course, you can search for some online tools to generate barcode directly. But it is useless when you want to create one programmatically. This post will show a solution to create barcode in Word by using C#, VB.NET.
I prepare a Word document which I have imported some texts. Then, I need to convert the texts to barcode. Please note that I have downloaded barcode font on my system. Also, in order to realize this function more quickly, I use a .NET Word component in my project.
Firstly, invoke document.FindAllString(string, bool caseSensitive, bool wholeWord) method to find barcode text and save it in TextSelection array. Use foreach sentence to set Text, FontName and FontSize properties for each selection in this TextSelection array. Finally, save this file and you can find the barcode as following screenshot after launching.
static void Main(string args)
Document document = new Document(@”E:\word.docx”);
TextSelection textSelections = document.FindAllString(“Cola Barcode”, true, true);
foreach (TextSelection selection in textSelections)
selection.GetAsOneRange().Text = “619051207”;
selection.GetAsOneRange().CharacterFormat.FontName = “C39HrP60DlTt”;
Friend Class BarCode
Shared Sub Main(ByVal args() As String)
Dim document As New Document(“E:\word.docx”)
Dim textSelections() As TextSelection = document.FindAllString(“Cola Barcode”, True, True)
For Each selection As TextSelection In textSelections
selection.GetAsOneRange().Text = “619051207”
selection.GetAsOneRange().CharacterFormat.FontName = “C39HrP60DlTt”
selection.GetAsOneRange().CharacterFormat.FontSize = 70