How wrap lines while print?
Hi, first of all sorry for bad english..
In this article
http://www.c-sharpcorner.com/winforms/texteditor.asp
I found a very useful way to print without use cristalreport.
Now, my only problem is:
if i have a string very long, this routine print the text out of the paper... (go out of right margin)
In wich way can i wrap the text ??
My routine use readline function, so i think to insert every x char a \r special char...
This works but is not good, infact "x i" use less space of "x o"....
This is a portion of my code:
string strText = "";
..
..
// what i want print
strText = "bla bla bla \r \r \r";
strText = strText + "this is a very long line, more of the right margin";
strText = strText + "bla bla bla"
myReader = new StringReader(strText);
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = this.ThePrintDocument ;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
printPreviewDialog1.ShowDialog();
}
..
..
private void ThePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs {
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.txtcognome.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
// Leggo riga per riga tutto quello che voglio stampare
while(count < linesPerPage && ((line=myReader.ReadLine()) != null))
{
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
myBrush.Dispose();
}
}
and this is my not good routine used to split long lines of text:
void splitta(string nota1)
{
int i = 0;
int cr = 0;
while(i < nota1.Length)
{
if (((cr > caratteri) & (nota1.Substring(i,1) == " ")) || (cr > caratteri))
{
Console.Out.WriteLine ("Stacco all'elemento: "+nota1.Substring(i,1)+" in posizione: "+cr );
nota1 = nota1.Insert(i, "\r");
cr=0;
}
cr++;
i++;
}
strText = strText + nota1;
}
the only way to use this routine well, is to use "Courier new" form..but this is not good solution..
(in courier new every char has the same width)
Con u solve my problem (or pheraps exist a free report tool ?) ?
(i dont have a crystal report license)
Sorry for long message..
i hope u help me