I am printing Cards on a Zebra ZXP Series 3 Card Printer.
I am using their provided SDK from here.
The card is 86mm x 54 mm
Maximum Resolution: 300 dpi
The code for me to produce text to be printed on the card goes a little something like this:
// text to draw details
DrawConfiguration firstnameConfiguration = new DrawConfiguration();
firstnameConfiguration.StringLabelText = "Johnny Appleseed";
firstnameConfiguration.LabelLocation = new Point(1, 350);
// zebra graphics thing
ZBRGraphics graphics = null;
graphics = new ZBRGraphics();
// font style
int fontStyle = FONT_BOLD;
// Draw First Name Text
if (graphics.DrawText(fn.LabelLocation.X, fn.LabelLocation.Y, graphics.AsciiEncoder.GetBytes(fn.StringLabelText), graphics.AsciiEncoder.GetBytes("Arial"), 12, fontStyle, 0x000000, out errorValue) == 0)
{
errorMessages += "\nPrinting DrawText [First Name] Error: " + errorValue.ToString();
noErrors = false;
}
then, DrawText looks like this:
public int DrawText(int x, int y, byte[] text, byte[] font, int fontSize, int fontStyle, int textColor, out int errValue)
{
return ZBRGDIDrawText(x, y, text, font, fontSize, fontStyle, textColor, out errValue);
}
[DllImport("ZBRGraphics.dll", EntryPoint = "ZBRGDIDrawText", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int ZBRGDIDrawText(int x, int y, byte[] text, byte[] font, int fontSize, int fontStyle, int color, out int err);
How can I center text on the Card using their SDK?
The only way I can figure out how to do it now is by "padding" the text with spaces". For example, " Johnny Appleseed "
will look like this after padding and may kind of look centered, but not really. Is there a generic formula where I can calculate how to center this text on the card based on the card dimensions/dpi?
After hours of fighting with the text alignment I finally got a response from Zebra. You have to use the DrawTextEx()
method and pass it in the alignment parameter. (Note this method is not provided in the SDK but it does exist in the DLL! You need to add it in your application to get it to work)
3 = left justified
4 = center justified
5 = right justified
Add this code to the ZBRGraphics.cs file
[DllImport("ZBRGraphics.dll", EntryPoint = "ZBRGDIDrawTextEx", CharSet = CharSet.Auto,
SetLastError = true)]
static extern int ZBRGDIDrawTextEx(int x, int y, int angle, int alignment, byte[] text, byte[] font, int fontSize, int fontStyle, int color, out int err);
public int DrawTextEx(int x, int y, int angle, int alignment, byte[] text, byte[] font, int fontSize, int fontStyle, int color, out int err)
{
return ZBRGDIDrawTextEx(x, y, angle, alignment, text, font, fontSize, fontStyle, color, out err);
}
Here is how to use it in your application.
How to use it (from the "SDK Manual"):
int x = 0;
int y = 0;
int angle = 0; //0 degrees rotation (no rotation)
int alignment = 4; //center justified
string TextToPrint = "Printed Text";
byte[] text = null;
string FontToUse = "Arial";
byte[] font = null;
int fontSise = 12;
int fontStyle = 1; //bold
int color = 0x0FF0000; //black
int err = 0;
int result = 0;
//use the function:
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
text = ascii.GetBytes(TextToPrint);
font = ascii.GetBytes(FontToUse);
result = ZBRGDIDrawTextEx(x, y, angle, alignment, text, font, fontSize,fontStyle, color, out err);