Click or drag to resize

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
)

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: Int32
The assigned job ID number.
Exceptions
ExceptionCondition
ConnectionExceptionIf the device is busy or there is an error communicating with the printer.
SettingsExceptionIf the job settings are not valid.
ZebraCardExceptionIf a printer error occurs or if the specified arguments are invalid.
ZebraIllegalArgumentExceptionIf copies or templateJob are invalid or an error occurs while building the job.
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);

            // Generate template job
            TemplateJob templateJob = zebraCardTemplate.GenerateTemplateDataJob(templateData, fieldData);

            // Send job
            int jobId = zebraCardPrinter.PrintTemplate(1, templateJob);

            // Poll job status
            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);
        }
    }

-    #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>";
     }
 
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="System.IO.IOException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     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

-    #region JobStatus
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="IOException"></exception>
     /// <exception cref="OverflowException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     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

-    #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