I have code that prints a till receipt which includes users string, the string is not fixed the user can type more or even less, thus i am not sure about the length of string user will type, i have made a rectangle to print the users string and the height will be the amount of letters in the string, it works but when the string has more characters the receipt has a big white gap before it prints the string so basically not the right way. also as the string changes, becomes short the rectangle position changes and distorts everything.
THIS IS LINK OF THE IMAGE HOW ITS PRINTED
this is the code that prints the rectangle:
graphics.DrawString("CONDITIONS OF SALE", bold2, Brushes.Black, 70, 60 + offX);
//THIS IS WHERE THE RECTANGLE IS PRINTED
Graphics pe = e.Graphics;
float aa = pe.MeasureString(SALE, regular, 0, StringFormat.GenericTypographic).Width + 40;
Size textSize = TextRenderer.MeasureText(SALE, regular);
var r = new Rectangle(2, textSize.Height + 130, 275, textSize.Height + offX);
//THIS IS THE RECTANGLE WHICH IS USED TO PRINT THE STRING WHICH IS CALLED SALE
StringFormat s = new StringFormat();
s.Alignment = StringAlignment.Center;
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(SALE + "[ " + COUNT.ToString() + " ]", regular, Brushes.Black, r, drawFormat); //PRINTS THE STRING CALLED SALE.//THIS IS THE PROBLEM
e.HasMorePages = false;
regular.Dispose();
bold.Dispose();