Data Binding
ThermalLabel SDK supports .NET Data Binding scenarios allowing you to print thermal labels bound to a data source. The supported data source types are List or Collection of custom .NET objects, ADO.NET, CSV, XML & JSON Array. 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 are some sample of data binding scenarios with different type of data sources.
Note
CSV, XML & JSON data sources can be specified as a system file path (file extension must be .csv
, .xml
& .json
respectivelly) or as a String (which must be prefixed by csv:
, xml:
& json:
respectivelly). If using a String, the CSV, XML or JSON content could be encoded in Base64 format as well.
Custom Object Data Binding scenario
The following sample features a class called Product with two basic properties: Id and Name. A list of Product and a ThermalLabel objects will be used to perform data binding scenario printing a set of thermal labels for each product as shown in the following figure.
- The Product class:
VB
Public Class Product
Dim _id As String
Dim _name As String
Public Sub New(ByVal id As String, ByVal name As String)
Me.Id = id
Me.Name = name
End Sub
Public Property Id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
CS
public class Product
{
string _id;
string _name;
public Product(string id, string name)
{
this.Id = id;
this.Name = name;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
- ThermalLabel with Custom Object Data Binding
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
txt.DataField = "Name"
'set font
txt.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt.Font.Unit = FontUnit.Point
txt.Font.Size = 10
'set border
txt.BorderThickness = new FrameThickness(0.03)
'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
bc.DataField = "Id"
'set barcode size
bc.BarWidth = 0.01
bc.BarHeight = 0.75
'set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter
'set text alignment
bc.CodeAlignment = BarcodeTextAlignment.BelowCenter
'set border
bc.BorderThickness = new FrameThickness(0.03)
'Add items to ThermalLabel object...
tLabel.Items.Add(txt)
tLabel.Items.Add(bc)
'Create data source...
Dim products As New List(Of Product)
products.Add(New Product("OO2935", "Olive Oil"))
products.Add(New Product("CS4948", "Curry Sauce"))
products.Add(New Product("CH0094", "Chocolate"))
products.Add(New Product("MZ1027", "Mozzarella"))
'set data source...
tLabel.DataSource = products
'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 WindowsPrintJob
pj.PrinterSettings = myPrinter
'Print ThermalLabel object...
pj.Print(tLabel)
End Using
CS
//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
txt.DataField = "Name";
//set font
txt.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt.Font.Unit = FontUnit.Point;
txt.Font.Size = 10;
//set border
txt.BorderThickness = new FrameThickness(0.03);
//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
bc.DataField = "Id";
//set barcode size
bc.BarWidth = 0.01;
bc.BarHeight = 0.75;
//set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter;
//set text alignment
bc.CodeAlignment = BarcodeTextAlignment.BelowCenter;
//set border
bc.BorderThickness = new FrameThickness(0.03);
//Add items to ThermalLabel object...
tLabel.Items.Add(txt);
tLabel.Items.Add(bc);
//Create data source...
List<Product> products = new List<Product>();
products.Add(new Product("OO2935", "Olive Oil"));
products.Add(new Product("CS4948", "Curry Sauce"));
products.Add(new Product("CH0094", "Chocolate"));
products.Add(new Product("MZ1027", "Mozzarella"));
//set data source...
tLabel.DataSource = products;
//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 WindowsPrintJob
pj.PrinterSettings = myPrinter;
//Print ThermalLabel object...
pj.Print(tLabel);
}
XML file Data Binding scenario
The following sample features an XML file containing books info. An ADO.NET DataSet object wrapping the books info from the XML source and a ThermalLabel objects will be used to perform data binding scenario printing a set of thermal labels for each book as shown in the following figure.
- Create an XML File in C:\temp\books.xml with the following content:
XML
<Books xmlns="">
<Book ISBN="0-7356-0562-9" Title="XML in Action" />
<Book ISBN="0-7356-1377-X" Title="Introducing Microsoft .NET" />
<Book ISBN="0-7356-1288-9" Title="Inside C#" />
<Book ISBN="0-7356-1370-2" Title="Programming Microsoft Windows With C#" />
<Book ISBN="0-7356-1448-2" Title="Microsoft C# Language Specifications" />
</Books>
- ThermalLabel with an XML file Data Binding
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 binding to book's title
Dim txt1 As New TextItem(0.1, 0.05, 2.8, 0.3, "")
'set data field
txt1.DataField = "Title"
'set font
txt1.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt1.Font.Unit = FontUnit.Point
txt1.Font.Size = 5
'set alignment
txt1.TextAlignment = TextAlignment.Left
'Define a TextItem object for binding to ISBN code
Dim txt2 As New TextItem(0.1, 0.3, 0.75, 0.5, "ISBN:")
'set data field
txt2.DataField = "ISBN"
txt2.DataFieldFormatString = "ISBN: {0}"
'set font
txt2.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt2.Font.Unit = FontUnit.Point
txt2.Font.Size = 10
'set alignment
txt2.TextAlignment = TextAlignment.Left
'Define a BarcodeItem object for encoding ISBN barcode
Dim bc As New BarcodeItem(0.1, 0.57, 2.8, 1.3, BarcodeSymbology.Isbn, "")
'set data field
bc.DataField = "ISBN"
'set barcode size
bc.BarWidth = 0.013
bc.BarHeight = 1
bc.EanUpcGuardBar = True
bc.EanUpcGuardBarHeight = bc.BarHeight + 5 * bc.BarWidth
'set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter
'set font for human readable text
bc.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
bc.Font.Unit = FontUnit.Point
bc.Font.Size = 5
'Add items to ThermalLabel object...
tLabel.Items.Add(txt1)
tLabel.Items.Add(txt2)
tLabel.Items.Add(bc)
'Create data source...
Dim books As New DataSet()
books.ReadXml("c:\temp\books.xml")
'set data source...
tLabel.DataSource = books
'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 WindowsPrintJob
pj.PrinterSettings = myPrinter
'Print ThermalLabel object...
pj.Print(tLabel)
End Using
CS
//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 binding to book's title
TextItem txt1 = new TextItem(0.1, 0.05, 2.8, 0.3, "");
//set data field
txt1.DataField = "Title";
//set font
txt1.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt1.Font.Unit = FontUnit.Point;
txt1.Font.Size = 5;
//set alignment
txt1.TextAlignment = TextAlignment.Left;
//Define a TextItem object for binding to ISBN code
TextItem txt2 = new TextItem(0.1, 0.32, 2, 0.5, "");
//set data field
txt2.DataField = "ISBN";
txt2.DataFieldFormatString = "ISBN: {0}";
//set font
txt2.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt2.Font.Unit = FontUnit.Point;
txt2.Font.Size = 10;
//set alignment
txt2.TextAlignment = TextAlignment.Left;
//Define a BarcodeItem object for encoding ISBN barcode
BarcodeItem bc = new BarcodeItem(0.1, 0.57, 2.8, 1.3, BarcodeSymbology.Isbn, "");
//set data field
bc.DataField = "ISBN";
//set barcode size
bc.BarWidth = 0.013;
bc.BarHeight = 1;
bc.EanUpcGuardBar = true;
bc.EanUpcGuardBarHeight = bc.BarHeight + 5 * bc.BarWidth;
//set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.MiddleCenter;
//set font for human readable text
bc.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
bc.Font.Unit = FontUnit.Point;
bc.Font.Size = 5;
//Add items to ThermalLabel object...
tLabel.Items.Add(txt1);
tLabel.Items.Add(txt2);
tLabel.Items.Add(bc);
//Create data source...
DataSet books = new DataSet();
books.ReadXml(@"c:\temp\books.xml");
//set data source...
tLabel.DataSource = books;
//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 WindowsPrintJob
pj.PrinterSettings = myPrinter;
//Print ThermalLabel object...
pj.Print(tLabel);
}
ADO.NET Database Data Binding scenario
The following sample features an MS Access Database file (C:\Temp\DatabaseSample.mdb) containing an Employees table. An ADO.NET DataTable as well as a ThermalLabel objects will be used for data binding scenario printing a set of thermal labels for each employee as shown in the following figure.
- The structure of the Employees table in DatabaseSample.mdb file is as follows:
Note
Please copy the DatabaseSample.mdb to C:\Temp\DatabaseSample.mdb before testing the following code.
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 couple of TextItem objects for Employee info
Dim txt1 As New TextItem(0.1, 0.05, 2.8, 0.3, "")
'set data field
txt1.DataField = "Name"
'set font
txt1.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt1.Font.Unit = FontUnit.Point
txt1.Font.Size = 10
'set alignment
txt1.TextAlignment = TextAlignment.Left
Dim txt2 As New TextItem(0.1, 0.35, 2.8, 0.3, "")
'set data field
txt2.DataField = "Address"
'set font
txt2.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt2.Font.Unit = FontUnit.Point
txt2.Font.Size = 5
'set alignment
txt2.TextAlignment = TextAlignment.Left
Dim txt3 As New TextItem(0.1, 0.65, 1.5, 0.3, "")
'set data field
txt3.DataField = "City"
'set font
txt3.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt3.Font.Unit = FontUnit.Point
txt3.Font.Size = 5
'set alignment
txt3.TextAlignment = TextAlignment.Left
Dim txt4 As New TextItem(1.6, 0.65, 0.5, 0.3, "")
'set data field
txt4.DataField = "State"
'set font
txt4.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt4.Font.Unit = FontUnit.Point
txt4.Font.Size = 5
'set alignment
txt4.TextAlignment = TextAlignment.Left
Dim txt5 As New TextItem(2.1, 0.65, 0.7, 0.3, "")
'set data field
txt5.DataField = "PostalCode"
'set font
txt5.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA
txt5.Font.Unit = FontUnit.Point
txt5.Font.Size = 5
'set alignment
txt5.TextAlignment = TextAlignment.Left
'Define a BarcodeItem object for encoding the postal code in USPS Postnet
Dim bc As New BarcodeItem(0.1, 0.95, 2.8, 0.65, BarcodeSymbology.Postnet, "")
'set data field
bc.DataField = "PostalCode"
'set narrow bar width
bc.BarWidth = 0.02
'do not set a quiet zone
bc.QuietZone = new FrameThickness(0)
'set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.TopLeft
'hide human readable text
bc.DisplayCode = False
'Add items to ThermalLabel object...
tLabel.Items.Add(txt1)
tLabel.Items.Add(txt2)
tLabel.Items.Add(txt3)
tLabel.Items.Add(txt4)
tLabel.Items.Add(txt5)
tLabel.Items.Add(bc)
'Create data source...
Dim employees As New DataTable()
Using conn As New System.Data.OleDb.OleDbConnection("Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb")
'open db connection...
conn.Open()
'execute db command...
Using cmd As New System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees", conn)
Using reader As System.Data.OleDb.OleDbDataReader = cmd.ExecuteReader()
employees.Load(reader)
End Using
End Using
End Using
'set data source...
tLabel.DataSource = employees
'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 WindowsPrintJob
pj.PrinterSettings = myPrinter
'Print ThermalLabel object...
pj.Print(tLabel)
End Using
CS
//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 couple of TextItem objects for Employee info
TextItem txt1 = new TextItem(0.1, 0.05, 2.8, 0.3, "");
//set data field
txt1.DataField = "Name";
//set font
txt1.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt1.Font.Unit = FontUnit.Point;
txt1.Font.Size = 10;
//set alignment
txt1.TextAlignment = TextAlignment.Left;
TextItem txt2 = new TextItem(0.1, 0.35, 2.8, 0.3, "");
//set data field
txt2.DataField = "Address";
//set font
txt2.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt2.Font.Unit = FontUnit.Point;
txt2.Font.Size = 5;
//set alignment
txt2.TextAlignment = TextAlignment.Left;
TextItem txt3 = new TextItem(0.1, 0.65, 1.5, 0.3, "");
//set data field
txt3.DataField = "City";
//set font
txt3.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt3.Font.Unit = FontUnit.Point;
txt3.Font.Size = 5;
//set alignment
txt3.TextAlignment = TextAlignment.Left;
TextItem txt4 = new TextItem(1.6, 0.65, 0.5, 0.3, "");
//set data field
txt4.DataField = "State";
//set font
txt4.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt4.Font.Unit = FontUnit.Point;
txt4.Font.Size = 5;
//set alignment
txt4.TextAlignment = TextAlignment.Left;
TextItem txt5 = new TextItem(2.1, 0.65, 0.7, 0.3, "");
//set data field
txt5.DataField = "PostalCode";
//set font
txt5.Font.Name = Neodynamic.SDK.Printing.Font.NativePrinterFontA;
txt5.Font.Unit = FontUnit.Point;
txt5.Font.Size = 5;
//set alignment
txt5.TextAlignment = TextAlignment.Left;
//Define a BarcodeItem object for encoding the postal code in USPS Postnet
BarcodeItem bc = new BarcodeItem(0.1, 0.95, 2.8, 0.65, BarcodeSymbology.Postnet, "");
//set data field
bc.DataField = "PostalCode";
//set narrow bar width
bc.BarWidth = 0.02;
//do not set a quiet zone
bc.QuietZone = new FrameThickness(0);
//set barcode alignment
bc.BarcodeAlignment = BarcodeAlignment.TopLeft;
//hide human readable text
bc.DisplayCode = false;
//Add items to ThermalLabel object...
tLabel.Items.Add(txt1);
tLabel.Items.Add(txt2);
tLabel.Items.Add(txt3);
tLabel.Items.Add(txt4);
tLabel.Items.Add(txt5);
tLabel.Items.Add(bc);
//Create data source...
DataTable employees = new DataTable();
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb"))
{
//open db connection...
conn.Open();
//execute db command...
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees", conn))
{
using (System.Data.OleDb.OleDbDataReader reader = cmd.ExecuteReader())
{
employees.Load(reader);
}
}
}
//set data source...
tLabel.DataSource = employees;
//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 WindowsPrintJob
pj.PrinterSettings = myPrinter;
//Print ThermalLabel object...
pj.Print(tLabel);
}