我正在 Zebra ZXP Series 3 Card Printer 上打印卡片。
我正在使用他们提供的 SDK here .
卡片尺寸为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);
How can I center text on the Card using their SDK?
我现在能弄清楚如何做到这一点的唯一方法是用空格“填充”文本。例如,“Johnny Appleseed”
在填充后看起来像这样,可能有点看起来居中,但实际上并非如此。是否有一个通用公式可以计算如何根据卡片尺寸/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手册》):
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);
关于c# - 如何使 Zebra ZXP 系列 3 卡上打印的标签文本居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48157901/