JobUtilPrint Method |
Namespace: Zebra.Sdk.Card.Job
Exception | Condition |
---|---|
ConnectionException | If the device is busy or there is an error communicating with the printer. |
SettingsException | If there is an error with the job settings. |
ZebraCardException | If a printer error occurs or there is an error creating the job. |
ZebraIllegalArgumentException | If copies or graphicsData are invalid or an error occurs while building the job. |
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Threading; using Zebra.Sdk.Card.Containers; using Zebra.Sdk.Card.Enumerations; using Zebra.Sdk.Card.Graphics; using Zebra.Sdk.Card.Graphics.Enumerations; using Zebra.Sdk.Card.Job; using Zebra.Sdk.Card.Printer; using Zebra.Sdk.Comm; public class PrintYMCKOExample { private const int CARD_FEED_TIMEOUT = 30000; public static void Main(string[] args) { Connection connection = null; ZebraCardPrinter zebraCardPrinter = null; try { connection = new TcpConnection("1.2.3.4", 9100); connection.Open(); zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection); List<GraphicsInfo> graphicsData = DrawGraphics(zebraCardPrinter); // Set the card source zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Feeder"); // Feeder=default // Set the card destination - If the destination value is not specifically set, it will be auto set to the most appropriate value if (zebraCardPrinter.HasLaminator()) { zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "LaminatorAny"); } else { zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "Eject"); } // Send job int jobId = zebraCardPrinter.Print(1, graphicsData); // Poll job status JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter); Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'."); } catch (Exception e) { Console.WriteLine($"Error printing image: {e.Message}"); } finally { CloseQuietly(connection, zebraCardPrinter); } } - #region Graphics /// <exception cref="ConnectionException"></exception> /// <exception cref="IOException"></exception> /// <exception cref="NotSupportedException"></exception> /// <exception cref="System.Security.SecurityException"></exception> /// <exception cref="UnauthorizedAccessException"></exception> /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception> /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception> private static List<GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter) { // Generate image data ZebraCardImageI zebraCardImage = null; List<GraphicsInfo> graphicsData = new List<GraphicsInfo>(); using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) { // Front side color string colorImagePath = @"path\to\myColorImage.bmp"; byte[] imageData = File.ReadAllBytes(colorImagePath); zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 0, 0); graphicsData.Add(AddImage(CardSide.Front, PrintType.Color, 0, 0, -1, zebraCardImage)); graphics.Clear(); // Front side full overlay graphicsData.Add(AddImage(CardSide.Front, PrintType.Overlay, 0, 0, 1, null)); // Back side mono string monoImagePath = @"path\to\myMonoImage.bmp"; imageData = File.ReadAllBytes(monoImagePath); zebraCardImage = DrawImage(graphics, PrintType.MonoK, imageData, 0, 0, 0, 0); graphicsData.Add(AddImage(CardSide.Back, PrintType.MonoK, 0, 0, -1, zebraCardImage)); } return graphicsData; } /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception> /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception> private static ZebraCardImageI DrawImage(ZebraGraphics graphics, PrintType printType, byte[] imageData, int xOffset, int yOffset, int width, int height) { graphics.Initialize(0, 0, OrientationType.Landscape, printType, Color.White); graphics.DrawImage(imageData, xOffset, yOffset, width, height, RotationType.RotateNoneFlipNone); return graphics.CreateImage(); } private static GraphicsInfo AddImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, ZebraCardImageI zebraCardImage) { return new GraphicsInfo { Side = side, PrintType = printType, GraphicType = zebraCardImage != null ? GraphicType.BMP : GraphicType.NA, XOffset = xOffset, YOffset = yOffset, FillColor = fillColor, Opacity = 0, Overprint = false, GraphicData = zebraCardImage ?? null }; } #endregion Graphics - #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 }
using System; using System.Collections.Generic; using System.IO; using System.Threading; using Zebra.Sdk.Card.Containers; using Zebra.Sdk.Card.Enumerations; using Zebra.Sdk.Card.Graphics; using Zebra.Sdk.Card.Graphics.Enumerations; using Zebra.Sdk.Card.Printer; using Zebra.Sdk.Comm; public class PrintMonoExample { private const int CARD_FEED_TIMEOUT = 30000; public static void Main(string[] args) { Connection connection = null; ZebraCardPrinter zebraCardPrinter = null; try { connection = new TcpConnection("1.2.3.4", 9100); connection.Open(); zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection); List<GraphicsInfo> graphicsData = DrawGraphics(zebraCardPrinter); // Send job and poll for status int jobId = zebraCardPrinter.Print(1, graphicsData); JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter); Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'."); } catch (Exception e) { Console.WriteLine($"Error printing image: {e.Message}"); } finally { CloseQuietly(connection, zebraCardPrinter); } } - #region Graphics /// <exception cref="ConnectionException"></exception> /// <exception cref="IOException"></exception> /// <exception cref="NotSupportedException"></exception> /// <exception cref="System.Security.SecurityException"></exception> /// <exception cref="UnauthorizedAccessException"></exception> /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception> /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception> private static List<GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter) { // Generate image data List<GraphicsInfo> graphicsData = new List<GraphicsInfo>(); using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) { // Back side mono string monoImagePath = @"path\to\myMonoImage.bmp"; byte[] monoImageData = File.ReadAllBytes(monoImagePath); graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, null); graphics.DrawImage(monoImageData, 0, 0, 0, 0, RotationType.RotateNoneFlipNone); ZebraCardImageI zebraCardImage = graphics.CreateImage(); graphicsData.Add(AddImage(CardSide.Back, PrintType.MonoK, 0, 0, -1, zebraCardImage)); } return graphicsData; } private static GraphicsInfo AddImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, ZebraCardImageI zebraCardImage) { return new GraphicsInfo { Side = side, PrintType = printType, GraphicType = zebraCardImage != null ? GraphicType.BMP : GraphicType.NA, XOffset = xOffset, YOffset = yOffset, FillColor = fillColor, Opacity = 0, Overprint = false, GraphicData = zebraCardImage ?? null }; } #endregion Graphics - #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 }