JobUtilPrintTemplate Method
|
Prints a card from the provided templatejobData.
Namespace:
Zebra.Sdk.Card.Job
Assembly:
SdkApi_Card_Core (in SdkApi_Card_Core.dll) Version: 2.14.1989
Syntax int PrintTemplate(
int copies,
TemplateJob templateJob
)
Function PrintTemplate (
copies As Integer,
templateJob As TemplateJob
) As Integer
int PrintTemplate(
int copies,
TemplateJob^ templateJob
)
Parameters
- copies
- Type: SystemInt32
The number of copies to be printed. - templateJob
- Type: Zebra.Sdk.Card.ContainersTemplateJob
A TemplateJob instance containing details of the job to be executed.
Return Value
Type:
Int32The assigned job ID number.
Exceptions Examples Demonstrates how to create and print a template job.
using System;
using System.Collections.Generic;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Job.Template;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;
public class PrintTemplateExample {
private const int CARD_FEED_TIMEOUT = 30000;
public static void Main(string[] args) {
Connection connection = null;
ZebraCardPrinter zebraCardPrinter = null;
try {
connection = new UsbConnection("\\\\?\\usb#vid_0a5f&pid_00a7#411738706#...");
connection.Open();
zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);
ZebraTemplate zebraCardTemplate = new ZebraCardTemplate(zebraCardPrinter);
string templateData = GetTemplateData();
List<string> templateFields = zebraCardTemplate.GetTemplateDataFields(templateData);
Dictionary<string, string> fieldData = PopulateTemplateFieldData(templateFields);
TemplateJob templateJob = zebraCardTemplate.GenerateTemplateDataJob(templateData, fieldData);
int jobId = zebraCardPrinter.PrintTemplate(1, templateJob);
JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
} catch (Exception e) {
Console.WriteLine($"Error printing template: {e.Message}");
} finally {
CloseQuietly(connection, zebraCardPrinter);
}
}
+Template- #region Template
private static string GetTemplateData() {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<template name=\"TemplateExample\" card_type=\"2\" card_thickness=\"30\" delete=\"no\">\r\n" +
" <fonts>\r\n" +
" <font id=\"1\" name=\"Arial\" size=\"12\" bold=\"no\" italic=\"no\" underline=\"no\"/>\r\n" +
" <font id=\"2\" name=\"Arial\" size=\"14\" bold=\"yes\" italic=\"yes\" underline=\"no\"/>\r\n" +
" </fonts>\r\n" +
" <sides>\r\n" +
" <side name=\"front\" orientation=\"landscape\" rotation=\"0\" k_mode=\"text\">\r\n" +
" <print_types>\r\n" +
" <print_type type=\"mono\">\r\n" +
" <text field=\"firstName\" font_id=\"1\" x=\"100\" y=\"100\" width=\"0\" height=\"0\" angle=\"0\" color=\"0x000000\"/>\r\n" +
" <text field=\"lastName\" font_id=\"2\" x=\"100\" y=\"150\" width=\"0\" height=\"0\" angle=\"0\" color=\"0x000000\"/>\r\n" +
" <line x1=\"100\" y1=\"235\" x2=\"260\" y2=\"235\" thickness=\"10\" color=\"0\"/>\r\n" +
" <barcode field=\"qrCode\" code=\"qrCode\" x=\"100\" y=\"270\" width=\"75\" height=\"75\" quiet_zone_width=\"0\"/>\r\n" +
" </print_type>\r\n" +
" </print_types>\r\n" +
" </side>\r\n" +
" </sides>\r\n" +
"</template>";
}
private static Dictionary<string, string> PopulateTemplateFieldData(List<string> templateFields) {
Dictionary<string, string> fieldData = new Dictionary<string, string>();
foreach (string fieldName in templateFields) {
string fieldValue = "";
switch (fieldName) {
case "firstName":
fieldValue = "Bob";
break;
case "lastName":
fieldValue = "Smith";
break;
case "qrCode":
fieldValue = "https://www.zebra.com";
break;
}
if (!string.IsNullOrEmpty(fieldValue)) {
if (!fieldData.ContainsKey(fieldName)) {
fieldData.Add(fieldName, fieldValue);
}
}
}
return fieldData;
}
#endregion Template
+JobStatus- #region JobStatus
private static JobStatusInfo PollJobStatus(int jobId, ZebraCardPrinter zebraCardPrinter) {
JobStatusInfo jobStatusInfo = new JobStatusInfo();
bool isFeeding = false;
long start = Math.Abs(Environment.TickCount);
while (true) {
jobStatusInfo = zebraCardPrinter.GetJobStatus(jobId);
if (!isFeeding) {
start = Math.Abs(Environment.TickCount);
}
isFeeding = jobStatusInfo.CardPosition.Contains("feeding");
string alarmDesc = jobStatusInfo.AlarmInfo.Value > 0 ? $" ({jobStatusInfo.AlarmInfo.Description})" : "";
string errorDesc = jobStatusInfo.ErrorInfo.Value > 0 ? $" ({jobStatusInfo.ErrorInfo.Description})" : "";
Console.WriteLine($"Job {jobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
break;
} else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled")) {
Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
break;
} else if (jobStatusInfo.ErrorInfo.Value > 0) {
Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
zebraCardPrinter.Cancel(jobId);
} else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding) {
if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT) {
Console.WriteLine("The job timed out waiting for a card and was cancelled.");
zebraCardPrinter.Cancel(jobId);
}
}
Thread.Sleep(1000);
}
return jobStatusInfo;
}
#endregion JobStatus
+CleanUp- #region CleanUp
private static void CloseQuietly(Connection connection, ZebraCardPrinter zebraCardPrinter) {
try {
if (zebraCardPrinter != null) {
zebraCardPrinter.Destroy();
}
} catch { }
try {
if (connection != null) {
connection.Close();
}
} catch { }
}
#endregion CleanUp
}
See Also