问题
We are using Zebra Designer Pro to create the templates. We have placed various labels on the templates to which we pass values dynamically through the code at the runtime.
We are doing this by creating ZPL in the code and appending various parameter values which we want to print in the ZPL. Method to create the label is below:
private string GetPrinterInstruction(string templateName, IList<string> variables, int numberOfCopies)
{
StringBuilder printerInstruction = new StringBuilder();
//start the label format and sets the field origin
printerInstruction.AppendLine("^XA");
//Template
printerInstruction.AppendLine("^XF");
printerInstruction.AppendLine(templateName);
//Field Separator
printerInstruction.AppendLine("^FS");
// for each Variable
for (var i = 1; i <= variables.Count; i++)
{
//Assign field number FN1..FN2
printerInstruction.AppendLine("^FN");
printerInstruction.AppendLine(i.ToString());
//Indicate start of field data
printerInstruction.AppendLine("^FD");
//Field data
printerInstruction.AppendLine(variables[i - 1]);
//Field Separator
printerInstruction.AppendLine("^FS");
}
//Printing Quantities of Labels,
printerInstruction.AppendLine("^PQ" + numberOfCopies);
//indicates the end of the print field and the end of the label format.
printerInstruction.AppendLine("^XZ");
return printerInstruction.ToString();
}
void PrintLabel(CasthouseDataContext casthouseDataContext, Printer printer, LabelType.LabelTypeCodes labelTypeCode, IList<string> variables, int numberOfCopies)
{
try
{
string printerInstruction = GetPrinterInstruction(templateName, variables, numberOfCopies);
CodePrinter.SendStringToPrinter(printerName, labelTemplateName, printerInstruction);
}
catch (Exception ex)
{
ExceptionManager.HandleException(ex, ExceptionPolicies.Service);
}
}
Below please find the ZPL file which is being generated to pass parameters to the template on the printer:
^XA
^XF
E:Label.ZPL
^FS
^FN
1
^FD
^FS
^FN
2
^FD
^FS
^FN
3
^FD
^FS
^FN
4
^FD
EEEEEEEEEEE
^FS
^FN
5
^FD
BBBBBBBBBB
^FS
^FN
6
^FD
11111111
^FS
^FN
7
^FD
16/08/2014
^FS
^FN
8
^FD
2411
^FS
^FN
9
^FD
11
^FS
^FN
10
^FD
2422
^FS
^FN
11
^FD
644444
^FS
^FN
12
^FD
6
^FS
^FN
13
^FD
1
^FS
^FN
14
^FD
^FS
^FN
15
^FD
^FS
^FN
16
^FD
6
^FS
^FN
17
^FD
^FS
^FN
18
^FD
ACCCCC
^FS
^FN
19
^FD
^FS
^FN
20
^FD
ABCCCC
^FS
^FN
21
^FD
^FS
^FN
22
^FD
66666666
^FS
^FN
23
^FD
666666
^FS
^FN
24
^FD
NNNNNNNNN
^FS
^FN
25
^FD
^FS
^FN
26
^FD
ABCCC
^FS
^FN
2222222
^FD
1111111111
^FS
^FN
28
^FD
111111
^FS
^FN
29
^FD
111111111
^FS
^FN
30
^FD
1111111111
^FS
^PQ1
^XZ
But the data which is getting printed on the labels is consistent and gets misplaced as we are not getting data in right placeholders. Please help as to how we can fix the issue.
回答1:
Dont use input fields. replace input fields in the code as follows:
- Open the label where you need to add a new field(parameter to be displayed).
- On the Toolbox, Choose Text > Fixed Text.
- Enter the parameter name. Note that the string should be such that it can be searched and replaced with the parameter value from database. Click Finish
Generate the ZPL for the file using print to file option in Print dialogbox. The ZPL file which will be generated will consist of the parameter names as mentioned in the text wizrd dialog box.
Use this ZPL file in the code. Now instead of passing parameter values using ZPL file use the ZPL file which has been generated and replace the values in place of the paramter name as follows:
//pass the zpl generated from Zebra Designer Pro into this menthod
public string replaceParameterValues(string zplForLabel)
{
StringBuilder zpl = new StringBuilder();
zpl.Apppend(zplForLabel);
//do this for all the paramters
zpl = zpl.Replace("<ParemeterName>", valueForTheParamter);
//pass this zpl to the printer for printing as it will contain
//all the values for the parameters
return zpl.ToString();
}
This will remove your dependency to create ZPL for passing parameters.
来源:https://stackoverflow.com/questions/25485250/zpl-passing-parameters-to-the-labels-prints-with-misplaced-data