如何让打印在 Zebra ZXP Series 3 卡上的标签文本居中?

How can I center text for a label being printed on a Zebra ZXP Series 3 Card?

我正在 Zebra ZXP Series 3 Card Printer 上打印卡片。

我正在使用他们从 here 提供的 SDK。

卡片尺寸为 86mm x 54mm

最大分辨率: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);

How can I center text on the Card using their SDK?

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

计算如何将此文本居中放置在卡片上

经过几个小时的文本对齐斗争,我终于得到了 Zebra 的回应。您必须使用 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 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);