C # print long string line breaks,
The main code is as follows:
StringFormat fmt = new StringFormat (); fmt. lineAlignment = StringAlignment. near; // left-aligned fmt. formatFlags = StringFormatFlags. lineLimit; // automatic line feed // sets the coordinate of Area B in the upper left corner of the text print area, and Size in the print area (rectangle) float mmtopt = 2.835f; unit conversion, mm * 2.835 = Point unit Rectangle r = new Rectangle (B, new Size (Convert. toInt32 (160 * mmtopt), Convert. toInt32 (100 * mmtopt); g. drawString (tb. text, titleFont, new SolidBrush (Color. black), r, fmt );
The following is the code of a software that I developed to automatically print a fixed table:
If (tb. text. trim (). length <28) // Title {g. drawString (tb. text, textFont, new SolidBrush (Color. black), B);} else {StringFormat fmt = new StringFormat (); fmt. lineAlignment = StringAlignment. near; // left-aligned fmt. formatFlags = StringFormatFlags. lineLimit; // automatic line feed // sets the coordinate of Area B in the upper left corner of the text print area, and Size in the print area (Rectangle) float mmtopt = 2.835f; Rectangle r = new Rectangle (B, new Size (Convert. toInt32 (160 * mmtopt), Convert. toInt32 (100 * mmtopt); r. Y = r. y-Convert. toInt32 (2 * mmtopt); Font titleFont = new Font (new FontFamily (""), 13, GraphicsUnit. point); g. drawString (tb. text, titleFont, new SolidBrush (Color. black), r, fmt );}
If the number of characters is less than 28, the default font is used to print the content. If the number of words is too large, the font is reduced, and the coordinates are slightly changed up to automatically print the line feed.
There will still be a single line but a small font, but it cannot be avoided as long as the font is reduced.