While helping a friend with some code, I ran into a situation where
I needed to do something to word wrap lines that were going to
print. The lines were actually data coming from a database where
the max length of the field is over 300 characters. When making
this data show up in a multiline textbox, it isn’t a big deal because
we can word wrap it easily enough. The problem comes when you
want to print this data. I needed to take the data stored in the
textboxes and write it to a text file for printing at a later
time. That was all fine, except when I printed, naturally, the
lines would run off the side of the page.
So here is what I came up with. It was very difficult looking
for solutions to handle word wrapping out on google. The
solutions I did find seems overly complex for the problem that needed
to be solved. If you have written a better solution, please let
me know. This is the best I came up with in the time
constraint I had to write it in (2 hours).
Keep in mind, this word wrapping is rather specific, because it is
setup for a basic print layout in Portrait mode, 1” margins and
printing 10pt Times New Roman font, which is where the 86 characters
per line comes from.
My next update to this routine will be to handle changing printer settings and adjusting for those for proper word wrapping.