如何将打印在 Zebra ZXP 系列 3 存储卡上的标签的文本居中



我正在 Zebra ZXP 系列 3 证卡打印机上打印证卡。

我从这里使用他们提供的 SDK。

存储卡尺寸为 86 毫米 x 54 毫米

最大分辨率:300 dpi

我生成要打印在卡片上的文本的代码有点像这样:

// 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;
}

然后,DrawText 看起来像这样:

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);

如何使用他们的 SDK 在卡片上居中显示文本?

我现在能弄清楚如何做到这一点的唯一方法是用空格"填充"文本"。例如,填充后" Johnny Appleseed "看起来像这样,可能看起来有点居中,但不是真的。是否有通用公式,我可以计算如何根据卡片尺寸/dpi 将此文本在卡片上居中?

经过数小时的文字对齐斗争,我终于得到了斑马的回应。您必须使用 DrawTextEx() 方法并将其传递到对齐参数中。(请注意,SDK 中未提供此方法,但 DLL 中确实存在此方法!您需要将其添加到您的应用程序中才能使其工作(

3 = 左对齐

4 = 居中对齐

5 = 右对齐

将此代码添加到 ZBRGraphics.cs 文件中

[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);
}

下面介绍如何在应用程序中使用它。

如何使用它(来自"SDK手册"(:

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);

最新更新