Formating text in ZPL

Mar 4, 2014 at 6:53 AM
Hi,
How to format text in ZPL?
I get text from database and I want to justify it on label.
Coordinator
Mar 4, 2014 at 3:10 PM
There isn't a public method in SharpZebra yet.

In my personal application, I've added this to StandardZPLCommand to justify text:
public static byte[] TextAlign(int width, Alignment alignment, byte[] textCommand)
        {
            return TextAlign(width, alignment, 1, 0, 0, textCommand);
        }

        public static byte[] TextAlign(int width, Alignment alignment, int maxLines, int lineSpacing, int indentSize, byte[] textCommand)
        {
            return TextAlign(width, alignment, maxLines, lineSpacing, indentSize, textCommand, 850);
        }

        public static byte[] TextAlign (int width, Alignment alignment, int maxLines, int lineSpacing, int indentSize, byte[] textCommand, int codepage)
        {
            //limits from ZPL Manual:
            //width [0,9999]
            //maxLines [1,9999]
            //lineSpacing [-9999,9999]
            //indentSize [0,9999]

            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);
            writer.Write(textCommand.Take(textCommand.Length-3).ToArray()); //strip ^FS from given command
            var s = string.Format("^FB{0},{1},{2},{3},{4}^FS", width, maxLines, lineSpacing, (char)alignment, indentSize);
            writer.Write(Encoding.GetEncoding(codepage).GetBytes(s));
            return stream.ToArray();
        }
This has a new enum Alignment, which I added to EnumDefinitions.cs:

    public enum Alignment
    {
        LEFT = 'L',
        CENTER = 'C',
        RIGHT = 'R',
        JUSTIFIED = 'J'
    }
After adding that I align as follows:
label.AddRange(ZPLCommands.TextAlign(300 Alignment.CENTER, ZPLCommands.TextWrite(50, 0, ElementDrawRotation.ROTATE_270_DEGREES, 20, "Centered Text")));
Where 300 is the width of the field block you would want to align the text to. I'm not happy with this implementation, so once I get time to update this for a public release it will likely change..