How to center text on Card?

J Joshua Preston 2 years 11 months ago
34 2 0

I am having trouble center aligning text on a card for a Zebra ZXP Series 3 using the Card Printing SDK.
 
I found in one of your examples codes how to center a barcode for the Card. But how can I center text for a card?
 
For example, for a barcode its:
 
                // Barcode Drawing---------------------------------------------------
                int rotation = 0; // origin lower left and no rotation
                int barcodeType = 0; // Code 39
                int barcodeWidthRatio = 2; // narrow bar = 2 dots, wide bar = 5 dots
                int barcodeMultiplier = 2; // {2..9}
                int barcodeHeight = 75; // 75 dots
                int textUnder = 1; // true
                string barcodeData = "123456789";
 
 
                //To calculate the full length of a Code 39 bar code:
                //L = [(C+2) (3R + 7) - 1] X Where
                //L = Length of bar code
                //C = Number of characters
                //R = Ratio of wide-to-narrow bars
                //X = Number of dots times 0.0033 inches per dot (0.08847 mm per dot); for the 5:2 ratio, X = Dots times 2
                //See ZXP3 SDK Manual for the forumulas used to calcuate the length of other barcode types.
 
 
                // Calculate the length of the barcode
                int C = barcodeData.Length;
                double R = 5.0 / 2.0;
                int X = 2;
                int length = (int)((C + 2) * (3 * R + 7) - 1) * X;
 
 
                int startX = (int)Math.Floor((CARD_WIDTH - length) / 2.0); //Center barcode horizontally
                int startY = (int)Math.Floor(((CARD_HEIGHT - barcodeHeight) / 2.0) + barcodeHeight); //Center barcode vertically
 
 
                // Sends Barcode data to the Monochrome Buffer
                if (DrawBarcode(startX, startY, rotation, barcodeType, barcodeWidthRatio, barcodeMultiplier, barcodeHeight, textUnder, _asciiEncoder.GetBytes(barcodeData), out errValue) == 0)
                {
                    msg = "Printing DrawBarcode Error: " + errValue.ToString();
                    return;
                }
 
But how and where can i find the formula for centering a label / text on a card??? It says it's in the manual but it isn't can someone please provide this formula?

Please register or login to post a reply

2 Replies

J Joshua Preston

As of right now, I am having to manually pad the text left and right in C# to try and get it to appear “centered” on the card. I have different font sizes on the card so It’s really hard when I don’t know the formula on how to center the text on the card. If I could just get the formula, the generic formula to center any text on the card – that would fix my issue! I am only printing on the front side of the card and it is just text.

Right now I am using this to pad my text with spaces left and right. It's sketchy and it appears to look centered in some cases but there may be cases where it isn't. I of course can't sit here and test the 3 billion different name combinations I may be putting on the card.

        public string SetTextAlignmentToCenter(string textToCenterAlign, int lengthOfLine)
        {
            // # of Characters you can put per line on the Badge. Badge is Landscape Orientation
            string centeredText = textToCenterAlign.PadLeft(((lengthOfLine - textToCenterAlign.Length) / 2) + textToCenterAlign.Length).PadRight(lengthOfLine);
            return centeredText;
        }

S Stephen Troup

I stopped using the graphics methods used in the sdk for this reason. I extracted the .net graphics object that the zebra graphics sdk uses to print to a card. I use the following routine to get access to it.

        internal static System.Drawing.Graphics GetGraphicsObject(ref ZMTGraphics.ZMotifGraphics g)
        {
            System.Reflection.FieldInfo dynField = g.GetType().GetField("helper", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

            object gg = dynField.GetValue(null);
            return (System.Drawing.Graphics)gg.GetType().GetField("graphics").GetValue(gg);
        }

The reason I came up with this methods was because I needed to measure a string to be able to shrink to fit and the sdk doesn't provide a public method to do it.

If I were printing on the landscape on a black panel then I'd do

int datalen;
byte[] bmpFrontMonoKPanels;
ZMTGraphics.ZMotifGraphics g = new ZMTGraphics.ZMotifGraphics();
System.Drawing.Drawing2D.GraphicsState gfxstate;

g.ClearGraphics();
g.InitGraphics(0, 0, ZMTGraphics.ZMotifGraphics.ImageOrientationEnum.Landscape, ZMTGraphics.ZMotifGraphics.RibbonTypeEnum.MonoK);
gfx = GetGraphicsObject(ref g);
gfxstate = gfx.Save();

gfx.Restore(gfxstate);
bmpFrontMonoKPanels = g.CreateBitmap(out dataLen);
g.ClearGraphics();

You just need to use the standard methods for drawing objects i.e. exactly the same as when printing to the old printer object. So finally to draw centred text, create a rectangle that's as big as the region you want the text to appear in, then use the gfx.drawstring overload that has the system.drawing.stringformat and use that to centre the text. No need to pad the string or calculate anything, unless it's too big to fit.

CONTACT
Can’t find what you’re looking for?