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.
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
In the Bookmark dialog box type BarcodeBookmark and click Add button.
Close the dialog box.
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);