Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
words get over lapped and i also not get whole paragraph in paragraph manner. it shows in single line. please see code.


What I have tried:

C#
private void printToolStripButton_Click(object sender, EventArgs e)
{
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
            printDocument1.Print();
        }
}
private int linesPrinted;
private string[] lines;
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    char[] param ={ '\n' };
    if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
    {
        lines = richTextBox1.SelectedText.Split(param);
    }
    else
    {
        lines = richTextBox1.Text.Split(param);
    }
    int i = 0;
    char[] trimParam = { '\r' };
    foreach (string s in lines)
    {
        lines[i++] = s.TrimEnd(trimParam);
    }
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    int x = e.MarginBounds.Left;
    int y = e.MarginBounds.Top;
    Brush brush = new SolidBrush(richTextBox1.ForeColor);

    while (linesPrinted < lines.Length)
    {
        e.Graphics.DrawString(lines[linesPrinted++],
            richTextBox1.Font, brush, x, y);
        y += 15;
        if (y >= e.MarginBounds.Bottom)
        {
            e.HasMorePages = true;
            return;
        }
    }

    linesPrinted = 0;
    e.HasMorePages = false;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900