Create Barcode in Word Document with C#, VB.NET

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.

barcode

CODE:

C#

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace DOCTEST

{

    class BarCode

    {

        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”;

                selection.GetAsOneRange().CharacterFormat.FontSize= 70;

            }

 

            document.SaveToFile(“Barcode.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“Barcode.docx”);

        }

    }

}

VB  

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

 

Namespace DOCTEST

    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

            Next selection

 

            document.SaveToFile(“Barcode.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“Barcode.docx”)

        End Sub

    End Class

End Namespace

Download Spire.Doc for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “Create Barcode in Word Document with C#, VB.NET”

  1. Thank you for every other informative blog. The place else may just I
    get that kind of info written in such an ideal means?

    I have a project that I’m just now working on, and I have been at the look out
    for such info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s