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

时间:2018-01-08 20:53:13

标签: c# zebra-printers

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

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

卡片 86mm x 54 mm

最高分辨率: 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计算如何将此文本放在卡片上?

1 个答案:

答案 0 :(得分:1)

经过几个小时的文本对齐,我终于收到了Zebra的回复。您必须使用DrawTextEx()方法并将其传递给alignment参数。 (注意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);