ZPLTemplate Class

This class I use along with my previous post to extract the different place holders in the template file and produce the finished output.  The full project is located here: LabelPrinter

Usage:

Dim labelTemplate as New ZPLTemplate(filePath)
labelTemplate.FieldList(“[Part Number]”) = “ABC123”
Printer.RawHelper.SendStringToPrinter(“GK420t”, labelTemplate.Output)


‘ Written by: Ray Held

Option Explicit On
Option Strict On
Option Infer Off

Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class ZPLTemplate

Private Template As String
Private Const RegExPattern As String = “\[(.*?)\]” ‘Looks for values that are between brackets: [ExampleField]

”’
”’ This property will have all of the place holders in the file. Simply refer to the place holder by name and set the value before calling output
”’

”’
”’
”’
Public Property FieldList As New Dictionary(Of String, String)

”’
”’ Produces the output file with the chacter replacements
”’

”’
”’
”’
Public ReadOnly Property Output As String
Get
Dim out As String = Template
For Each k As String In FieldList.Keys
out = out.Replace(k, FieldList.Item(k))
Next
Return out
End Get
End Property

Private Sub New()
‘Intentionally left blank — i need an input template to make this work.
End Sub

”’
”’ Load this class with the template file you are working with
”’

”””
Public Sub New(inputTemplate As String)
Template = inputTemplate
FindFields()
End Sub

Private Sub FindFields()
Dim re As New Regex(RegExPattern)
FieldList = New Dictionary(Of String, String)

For Each m As Match In re.Matches(Template)
If Not FieldList.ContainsKey(m.ToString) Then
FieldList.Add(m.ToString, String.Empty)
End If
Next
End Sub

End Class

Get my full demo app here: LabelPrinter


Leave a comment