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

C# 如何将打印在Zebra ZXP Series 3卡上的标签文本居中?,c#,zebra-printers,C#,Zebra Printers,我正在一台打印机上打印卡片 我正在使用他们从提供的SDK 该卡的尺寸为86毫米x 54毫米 最大分辨率:300 dpi 我生成要打印在卡片上的文本的代码有点像这样: // text to draw details DrawConfiguration firstnameConfiguration = new DrawConfiguration(); firstnameConfiguration.StringLabelText = "Johnny Appleseed"; firstnameConf

我正在一台打印机上打印卡片

我正在使用他们从提供的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;
}
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);
然后,DrawText如下所示:

// 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;
}
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在卡片上居中显示文本


我现在唯一能弄明白怎么做的方法就是用空格“填充”文本“填充后会像这样,看起来可能有点居中,但实际上并非如此。有没有一个通用公式,我可以根据卡片尺寸/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手册”):


ZBRGraphics
是否有办法以像素为单位测量字符串的宽度?没有。我需要找出如何以像素为单位测量字符串的宽度,以便我可以在正确的位置设置X位置如果
ZBRGraphics
无法以像素为单位测量字符串的宽度,则放弃它并使用它
系统.绘图.打印.打印文档
,它执行。