Print Wrapped Text

Problem

You want to parse a large block of text into distinct lines that fit on a page.

Solution

Use the Graphics.DrawString method overload that accepts a bounding rectangle. Or, if you need custom wrapping ability, write code that moves through the text word by word and measures it using the Graphics.MeasureString method until the line exceeds a set threshold.

Discussion

Often, you’ll need to break a large block of text into separate lines that can be printed individually on a page. In .NET, you can take two approaches: one that performs the wrapping for you, and one by which your code controls the wrapping process.

To use automatic wrapping, you simply need to use the version of the Graphics.DrawString method that accepts a bounding ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.