See Also
Other Platforms
Features
Buy Now
Online Help
Demo
Licensing

Back

How to insert barcode images into a Microsoft Word document using C# or VB.NET and Barcode Professional for .NET

Technologies used
  • Neodynamic Barcode Professional 2.5 for .NET Windows Forms or greater
  • Microsoft .NET Framework 1.x or greater
  • Microsoft Visual Studio .NET or greater
  • Microsoft Office Primary Interop Assemblies (PIA)
Due to its flexible design, Barcode Professional allows you to easily insert barcode images into a Microsoft Word document using .NET technology.
In the following sample we're going to generate a simple .NET Windows Forms application that will allow you to insert a barcode image into a predefined Word document.
Follow these steps:
  • Open Visual Studio and create a new Windows Forms Application.
  • Open the default Form at design time and design it as is shown in the following figure
    Inserting barcode into Microsoft Word
  • In order to manipulate Word documents from .NET, we're going to use the Microsoft Office Primary Interop Assemblies (PIA) provided by Microsoft. PIA is available for Microsoft Office XP and Office 2003. You can get them from the following locations:
    Office XP PIA
    http://www.microsoft.com/downloads/details.aspx?FamilyID=c41bd61e-3060-4f71-a6b4-01feba508e52&DisplayLang=en
    Office 2003 PIA
    http://www.microsoft.com/downloads/details.aspx?FamilyID=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&DisplayLang=en
    Please, download the correct PIA and install it on your box.
    NOTE:
    For older versions of Office, please read the following papers at Microsoft website about interoperation between Microsoft .NET managed code and Microsoft Office applications.
    http://msdn.microsoft.com/office/archive/default.aspx?pull=/library/en-us/odc_of2003_bk/html/officeinteroperabilitych2_part1.asp
  • With your Windows Forms project opened, add a reference to Microsoft Word [Version] Object Library as is shown in the following figure:
    Add Reference
    And add a reference to Barcode Professional as is shown in the following figure. IMPORTANT: Please, select the correct version of Barcode Professional depending on what version of the .NET Framework you're using.
    Add Reference
  • Now, we'll need to prepare our predefined Word document so we can specify where the barcode image will be inserted by code. In our case we're going to use a fictitious Invoice document that you'll find at the end of this guide available for download.
    We have prepared the Word document so we can specify where to insert the barcode image. To do that, we've added a bookmark. You can do the same like follows:
    Open your predefined document using Microsoft Word. Locate the point where you want the barcode be inserted, type "BarcodeHere" and select it. With that word selected, go to Insert > Bookmark
    INVOIVE.doc - Microsoft Word
    In the Bookmark dialog box type BarcodeBookmark and click Add button. Close the dialog box.
    INVOIVE.doc - Microsoft Word
    IMPORTANT: We've inserted a bookmark labeled BarcodeBookmark. The bookmark name is very important because we'll reference it in code.
  • After the Word document is ready, back to the Windows Forms project and add the following method in the form's code behind file:
    Visual Basic .NET
    Private Sub AddBarcodeIntoWordDoc()
    'Create barcode professional instance
    Dim bc As New Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional
    'Set some barcode symbology
    bc.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128
    'Set value to encode
    bc.Code = Me.TextBox1.Text
    bc.Text = ""

    'Create barcode image
    Dim ms As New System.IO.MemoryStream(bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png))
    Dim barcodeImage As Bitmap = CType(Image.FromStream(ms), Bitmap)

    'Create a Word App
    Dim wordApp As New Microsoft.Office.Interop.Word.Application
    wordApp.Visible = False

    'Interop params
    Dim oMissing As Object = System.Reflection.Missing.Value
    Dim save As Object = False

    'The Word doc paths
    Dim destFile As Object = "C:\INVOICE_WITH_BARCODE.doc"
    Dim docFile As Object = "C:\INVOICE.doc"

    'Open the doc file
    Dim wordDoc As Microsoft.Office.Interop.Word.Document
    wordDoc = wordApp.Documents.Open(docFile)

    'Find the predefined barcode bookmark into the doc
    Dim oBarcodeBookmark As Object = "BarcodeBookmark"
    Dim range As Microsoft.Office.Interop.Word.Range
    range = wordDoc.Bookmarks.Item(oBarcodeBookmark).Range

    'Copy the barcode image into Clipboard
    Clipboard.SetDataObject(barcodeImage)

    'Paste the barcode image
    range.Paste()

    'Save a doc copy with barcode
    wordDoc.SaveAs(destFile)

    'Quit
    wordApp.Quit(save)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp)

    ms.Close()
    barcodeImage.Dispose()

    MessageBox.Show("Barcode inserted!")
    End Sub
    Visual C# .NET
    private void AddBarcodeIntoWordDoc()
    {
    //Create barcode professional instance
    Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional bc = new Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional();
    //Set some barcode symbology
    bc.Symbology = Neodynamic.WinControls.BarcodeProfessional.Symbology.Code128;
    //Set value to encode
    bc.Code = this.textBox1.Text;
    bc.Text = "";
    //Create barcode image
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png));
    Bitmap barcodeImage = (Bitmap)Image.FromStream(ms);

    //Create a Word App
    Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    wordApp.Visible = false;

    //Interop params
    object oMissing = System.Reflection.Missing.Value;
    object save = false;

    //The Word doc paths
    object docFile = @"C:\INVOICE.doc";
    object destFile = @"C:\INVOICE_WITH_BARCODE.doc";

    //Open the doc file
    Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(ref docFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    //Find the predefined barcode bookmark into the doc
    object oBarcodeBookmark = "BarcodeBookmark";
    Microsoft.Office.Interop.Word.Range range = wordDoc.Bookmarks.get_Item(ref oBarcodeBookmark).Range;

    //Copy the barcode image into Clipboard
    Clipboard.SetDataObject(barcodeImage);

    //Paste the barcode image
    range.Paste();

    //Save a doc copy with barcode
    wordDoc.SaveAs(ref destFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    //Quit
    wordApp.Quit(ref save, ref oMissing, ref oMissing);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);

    ms.Close();
    barcodeImage.Dispose();

    MessageBox.Show("Barcode inserted!");
    }
    TIP: How to insert barcode images with DPI support
    Barcode Professional 3.0 or grater lets you to generate barcode images with DPI support. If you want to insert barcode images with such feature, then you can do that as follows:
    Visual Basic .NET
    • Replace the following code in the AddBarcodeIntoWordDoc() method:
    'Copy the barcode image into Clipboard
    Clipboard.SetDataObject(barcodeImage)
    'Paste the barcode image
    range.Paste()
    • By this code:
    'Save the barcode image at 300 DPI
    Dim tmpBarcode As System.Drawing.Image = bc.GetBarcodeImage(300)
    Dim barcodeTempFileName As String = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".jpg"
    tmpBarcode.Save(barcodeTempFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
    tmpBarcode.Dispose()
    range.Select()
    wordDoc.Application.Selection.InlineShapes.AddPicture(barcodeTempFileName)
    System.IO.File.Delete(barcodeTempFileName)
    
    Visual C# .NET
    • Replace the following code in the AddBarcodeIntoWordDoc() method:
    //Copy the barcode image into Clipboard
    Clipboard.SetDataObject(barcodeImage);
    //Paste the barcode image
    range.Paste();
    • By this code:
    //Save the barcode image at 300 DPI
    System.Drawing.Image tmpBarcode = bc.GetBarcodeImage(300);
    string barcodeTempFileName = System.IO.Path.GetTempPath() +
    Guid.NewGuid().ToString() + ".jpg";
    tmpBarcode.Save(barcodeTempFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    tmpBarcode.Dispose();
    range.Select(); 
    wordDoc.Application.Selection.InlineShapes.AddPicture(barcodeTempFileName);
    System.IO.File.Delete(barcodeTempFileName);

  • Add the following code for button's Click events
    Visual Basic .NET
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Me.TextBox1.Text.Length > 0 Then
    Me.AddBarcodeIntoWordDoc()
    End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    System.Diagnostics.Process.Start("C:\INVOICE_WITH_BARCODE.doc")
    End Sub
    Visual C# .NET
    private void button1_Click(object sender, System.EventArgs e)
    {
    if (this.TextBox1.Text.Length > 0)
    this.AddBarcodeIntoWordDoc();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    System.Diagnostics.Process.Start(@"C:\INVOICE_WITH_BARCODE.doc");
    }

  • That's it. Run your project, type some value to encode and click Insert button. The barcode image will appear into the Word document!
    Inserting barcode into Microsoft Word
    INVOICE_WITH_BARCODE.doc - Microsoft Word
Sample Files Download
If you need more information or assistance, please contact our .
 Copyright © 2003 - 2008 Neodynamic S.R.L. All rights reserved.