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:
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
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!");
}
'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)
//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);
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
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");
}
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.