How to data binding CSV files to print barcode labels with Zebra ZPL-EPL printers and VB.NET or C# by using ThermalLabel SDK for .NET
Product ThermalLabel SDK for .NET Published 06/13/2011 Updated 04/18/2018
Requirements
Neodynamic ThermalLabel SDK 8.0+
Microsoft .NET Framework 4.6.1+
Microsoft Visual Studio 2017+
Any Zebra Thermal Printer supporting ZPL (Zebra Programming Language) or EPL (Eltron Programming Language)
ThermalLabel SDK supports .NET Data Binding scenarios allowing you to print thermal labels bound to a data source such as custom .NET objects, XML files, Databases, ADO.NET, CSV files, etc. The data binding settings are very simple. You set up a valid data source to the ThermalLabel object by using the DataSource property. In the label, items like TextItem, ImageItem and BarcodeItem can be bound to fields of the data source by using the DataField property.
The following sample features a CSV (comma-separated values) file called Products.csv containing info about some fictitious items. The csv file contains in the first line (row) the name of the "columns" which in this case are PRODUCT_CODE and PRODUCT_NAME. Those names are the one which must be set up to the DataField property of the item object used for designing the thermal label object.
We'll be designing a label which will print out both fields; the product name as a TextItem and the product code as a Code 128 BarcodeItem. The output will look like the following figure:
A sample output of printed thermal labels from CSV file
To reproduce this demo, please follow up these steps:
- Open Notepad or any text editor and paste the following:
PRODUCT_CODE, PRODUCT_NAME
OO2935, Olive Oil
CS4948, Curry Sauce
CH0094, Chocolate
MZ1027, Mozzarella - Save the file with *.CSV extension in C:\products.csv
- Open Visual Studio and create a new Windows Forms project. Please ensure to select .NET 4.6.1 Framework or greater
- Add a reference to Neodynamic.SDK.ThermalLabel.dll and Neodynamic.SDK.ThermalLabel.WindowsPrinting.dll assemblies.
-
Add a button to the default Form1 and paste the following code in the Click event handler of such button:
NOTE: The following code was printer settings to an USB Zebra ZPL printer called "Zebra TLP2844-Z". You must change it to match your target printer i.e. a Zebra ZPL or EPL printer including the name. In addition, in the code the label size is set to 3in x 2in so please change it based on your needs.VB
'Define a ThermalLabel object and set unit to inch and label size Dim tLabel As New ThermalLabel(UnitType.Inch, 3, 2) tLabel.GapLength = 0.2 'Define a TextItem object for product name Dim txt As New TextItem(0.1, 0.1, 2.8, 0.5, "") 'set data field which must match some field/column name of the first row in the CSV file txt.DataField = "PRODUCT_NAME" 'set font txt.Font.Name = "Arial" txt.Font.Unit = FontUnit.Point txt.Font.Size = 16 'set border txt.BorderThickness = New FrameThickness(0.03) txt.CornerRadius = New RectangleCornerRadius(0.1, 0.1, 0, 0) 'set alignment txt.TextAlignment = TextAlignment.Center txt.TextPadding = New FrameThickness(0, 0.1, 0, 0) 'Define a BarcodeItem object for encoding product id with a Code 128 symbology Dim bc As New BarcodeItem(0.1, 0.57, 2.8, 1.3, BarcodeSymbology.Code128, "") 'set data field which must match some field/column name of the first row in the CSV file bc.DataField = "PRODUCT_CODE" 'set barcode size bc.BarWidth = 0.01 bc.BarHeight = 0.75 'set barcode alignment bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter 'set text alignment bc.CodeAlignment = BarcodeTextAlignment.BelowJustify 'set border bc.BorderThickness = New FrameThickness(0.03) bc.CornerRadius = New RectangleCornerRadius(0, 0, 0.1, 0.1) 'Add items to ThermalLabel object... tLabel.Items.Add(txt) tLabel.Items.Add(bc) 'set data source to a CSV file... tLabel.DataSource = "c:\products.csv" 'Create a WindowsPrintJob object Using pj As New WindowsPrintJob() 'Create PrinterSettings object Dim myPrinter As New PrinterSettings() myPrinter.Communication.CommunicationType = CommunicationType.USB myPrinter.Dpi = 203 myPrinter.ProgrammingLanguage = ProgrammingLanguage.ZPL myPrinter.PrinterName = "Zebra TLP2844-Z" 'Set PrinterSettings to PrintJob pj.PrinterSettings = myPrinter 'Print ThermalLabel object... pj.Print(tLabel) End UsingC#
//Define a ThermalLabel object and set unit to inch and label size ThermalLabel tLabel = new ThermalLabel(UnitType.Inch, 3, 2); tLabel.GapLength = 0.2; //Define a TextItem object for product name TextItem txt = new TextItem(0.1, 0.1, 2.8, 0.5, ""); //set data field which must match some field/column name of the first row in the CSV file txt.DataField = "PRODUCT_NAME"; //set font txt.Font.Name = "Arial"; txt.Font.Unit = FontUnit.Point; txt.Font.Size = 16; //set border txt.BorderThickness = new FrameThickness(0.03); txt.CornerRadius = new RectangleCornerRadius(0.1, 0.1, 0, 0); //set alignment txt.TextAlignment = TextAlignment.Center; txt.TextPadding = new FrameThickness(0, 0.1, 0, 0); //Define a BarcodeItem object for encoding product id with a Code 128 symbology BarcodeItem bc = new BarcodeItem(0.1, 0.57, 2.8, 1.3, BarcodeSymbology.Code128, ""); //set data field which must match some field/column name of the first row in the CSV file bc.DataField = "PRODUCT_CODE"; //set barcode size bc.BarWidth = 0.01; bc.BarHeight = 0.75; //set barcode alignment bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter; //set text alignment bc.CodeAlignment = BarcodeTextAlignment.BelowJustify; //set border bc.BorderThickness = new FrameThickness(0.03); bc.CornerRadius = new RectangleCornerRadius(0, 0, 0.1, 0.1); //Add items to ThermalLabel object... tLabel.Items.Add(txt); tLabel.Items.Add(bc); //Set the data source to a CSV file... tLabel.DataSource = @"c:\products.csv"; //Create a WindowsPrintJob object using (WindowsPrintJob pj = new WindowsPrintJob()) { //Create PrinterSettings object PrinterSettings myPrinter = new PrinterSettings(); myPrinter.Communication.CommunicationType = CommunicationType.USB; myPrinter.Dpi = 203; myPrinter.ProgrammingLanguage = ProgrammingLanguage.ZPL; myPrinter.PrinterName = "Zebra TLP2844-Z"; //Set PrinterSettings to PrintJob pj.PrinterSettings = myPrinter; //Print ThermalLabel object... pj.Print(tLabel); }
Need help? Contact Support
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.