printing a long string

J

Jimbo

Hey

Its easy to print a string. You just write:

System.Drawing.Printing.PrintPageEventArgs.Graphics.DrawString("This is a
string!", new Font("Ariel", 14, FontStyle.Bold), Brushes.Black, 0, 782);

Ok, but what if theres more in the string than just "This is a string!"?

What im trying to do is print the text from a multiline textbox. If i use
the above DrawString to write out the text on the printer, it just writes
from right to left, but it does not wrap the text to the next line, when i
run out of paper. Seems like im gonna need a very wide paper (and a printer
support that paper).

I dont know what to do to print the whole string, but to break the string
into an array with 80 characters in each. But there must be a smarter way?

Is there a function to insert a newline every 80th character, or maybe
another way to print out the string than DrawString, or...

Can anyone help?
 
M

Morten Wennevik

Hi Jimbo,

You can specify bounds on DrawString using a Rectangle instead of just a
Position

Graphics.DrawString("This is a string!", this.Font, Brushes.Black, new
Rectangle(0, 782, 30, 40));

I believe the default wrapping is the nearest word, but you can get more
control of it if you specify a StringFormat in the DrawString().

If you need it to break every 80th word you can insert \r\n in the string
at the appropriate places before drawing it to screen.
 
D

Dennis Myrén

You would have to "manually" compute text length and wrap the text
appropriately.

Have a look at System.Drawing.Graphics::MeasureString
and System.Drawing.Graphics::MeasureCharacterRanges.

You would want to use System.Drawing.StringFormat.GenericTypographic.
 
J

Jimbo

Morten Wennevik said:
Hi Jimbo,

You can specify bounds on DrawString using a Rectangle instead of just a
Position

Graphics.DrawString("This is a string!", this.Font, Brushes.Black, new
Rectangle(0, 782, 30, 40));

The rectangle did the trick. Only problem is that the text gets cut of if i
write more than can be in the rectangle. I can live with that though, so the
rectangle is still the solution.

Thanks

Jimmy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top