int NewLine=0; int width=0; int =0;
public void Print_text(PrintPageEventArgs e, ref string doc, Font TextFont, Brush TextColor)
{
width= e.MarginBounds.Width;
StringBuilder SB=new StringBuilder(1000);
SizeF strLength; bool MorePs=true;
float yPos=0; string word="";
Rectangle R=new Rectangle(e.MarginBounds.Left, e.MarginBounds.Top+Convert.ToInt32(yPos), e.MarginBounds.Width, e.MarginBounds.Height);
while(yPos < e.MarginBounds.Bottom-e.MarginBounds.Top)
{
if(word=="") word= GetWord(doc); if(word=="") {MorePs=false; break;}
strLength= e.Graphics.MeasureString(SB.ToString()+word, TextFont, e.MarginBounds.Width, new StringFormat());
if(NewLine > 0)
{yPos= yPos+(TextFont.GetHeight(e.Graphics)*NewLine); NewLine= 0;}
if(strLength.Width > e.MarginBounds.Width)
{yPos= yPos+TextFont.GetHeight(e.Graphics);}
else if(yPos < e.MarginBounds.Bottom-e.MarginBounds.Top)
{SB.Append(word); word="";}
}
e.Graphics.DrawString(SB.ToString(), TextFont, TextColor, R);
if(MorePs) e.HasMorePages=true;
else =0;
}
string GetWord(string doc)
{
StringBuilder sb=new StringBuilder(8);
if( >= doc.Length) return "";
while(!Char.IsLetterOrDigit(doc[ ]))
{
if(doc[ ]==\n) ++NewLine;
sb.Append(doc[ ]);
++ ;
if( >= doc.Length || sb.ToString().Length==width) return sb.ToString();
}
while(!Char.IsWhiteSpace(doc[ ]))
{
if(doc[ ]==\n) ++NewLine;
sb.Append(doc[ ]);
++ ;
if( >= doc.Length || sb.ToString().Length==width) return sb.ToString();
}
return sb.ToString();
}
}