C# code that scales font, now having issue getting the Zebra printer to interpret it


Audience
Tags

So a while back I posted about whether or not Zebra/ZPL had some sort of auto-scaling functionality to scale AND wrap text to fit a label. It didn't, so I came up with my own method, although now I'm having difficulty getting it to print properly.

My C# code takes in a user input of what they'd like printed, and then passes that along with the font name, font style, and label width into a function that scales it down:

public Font ScaleFont(string text, string fontName, FontStyle fontStyle, int startingFontSize, double labelWidth) {
for (int fontSize = startingFontSize; fontSize > 1; --fontSize) {
Font trialFont = new Font(fontName, fontSize, fontStyle);
Size textSize = TextRenderer.MeasureText(text, trialFont);
if (textSize.Width < labelWidth)
return trialFont;
}
return null;
}

This seems to work, and it returns a font whose Font.Size's width property is always smaller than the maximum label width that I pass. Now I'm just having difficulty getting that into ZPL and getting the printer to interpret it properly. To start, I've already downloaded Arial onto the printer, and when checking the on-board memory it returns that the font is in position "1". I read the documentation and saw that you're to use the "^Afo,h,w" command to set the font size along with other properties, so I have a string that gets concatenated with the rest of my pre-generated ZPL code:

$"^A1N,{fontsize.Height},{fontsize.Width}" Is right before my "^FD"block with the calculated values being used for the height and width property of the ^A command. However, upon printing it seems to output the default size regardless of the input-text's length or content. I'm not sure if I'm doing this correctly.

Submitted by SSi1 on December 16, 2021 Permalink

Looks like the downloaded Arial font is not designated to a correct letter.

If the ZebraDesigner driver is used to download the Arial font onto the printer, there would a *.TTF file called as 9835202.TTF (or something similar to it) on the E: drive or R: drive after the font is downloaded. The downloading process could take a while depending on the size of the font library. Then,  we need to use ^CW command to designate the 9835202.TTF font to a letter. For example, to designate the font to the letter Z, we use the following ZPL

^XA^CWZ,E:9835202.TTF^FS^XZ

Then we can call out the Z font with the following ZPL for the desired size we want.

^XA^FO10,10 ^CI28^AZN,32,32^FDArial 32x32^FS^XZ

^XA^FO10,10 ^CI28^AZN,64,64^FDArial 64x64^FS^XZ