Reputation: 13
In my billing application, bills are printed using Graphics.DrawString method, etc. When using Graphics.DrawString with word wrap to draw the ItemName it wrap words into many lines. so it is consuming many rows.
I recreated the problem using simple code. With this code I'm expecting output like the following image: Expected Output designed using MS Paint
This is my sample code.
string FONTNAME = "Tahoma";
text = "C# is a simple, modern, general-purpose, object-oriented programming language";
e.Graphics.DrawString(text, new Font(FONTNAME, 12, FontStyle.Bold), drawBrush, new RectangleF(0, 0, 100.0F, 200F), new StringFormat());
e.Graphics.DrawRectangle(new System.Drawing.Pen(drawBrush), 0, 0, 100.0F, 200F);
But I'm getting the following output: Output Image
Is there any option to break the words instead of word wrap ?
Upvotes: 0
Views: 3119
Reputation: 28162
No, Graphics.DrawString
does not have any option to break on characters (as opposed to whole words).
To control wrapping, you can pass a StringFormat
object initialised with a StringFormatFlags
. The only relevant flag is StringFormatFlags.NoWrap
. The .NET documentation isn't very helpful, but if we look at the GDI+ documentation (which is the underlying library), we read:
StringFormatFlagsNoWrap
Specifies that the wrapping of text to the next line is disabled. … When drawing text within a rectangle, by default, text is broken at the last word boundary that is inside the rectangle's boundary and wrapped to the next line.
Thus, the only options are wrapping at word boundaries, or performing no wrapping at all.
To get the output you want, you will need to measure character-by-character and draw multiple strings (one for each line).
Upvotes: 1