Click here to Skip to main content
15,898,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void printReceipt()
{
    printDialog.Document = printDocument;

    DialogResult result = printDialog.ShowDialog();

    if (result != DialogResult.OK) return;
    try
    {
        sPrint = new StreamReader(
                 new MemoryStream(
                     Encoding.ASCII.GetBytes(richTextBoxResult.Text)));

        printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);

        printDocument.Print();
    }
    catch (Exception e)
    {
        MessageBox.Show("Failed to print \n" + e.Message);

    }
    finally
    {
        if (sPrint != null)
            sPrint.Close();
    }
}

void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    // No of lines that fit on to the page
    float linesPerPage = e.MarginBounds.Height / richTextBoxResult.Font.GetHeight(e.Graphics);

    float fontHeight = richTextBoxResult.Font.GetHeight(e.Graphics);

    for (int count = 0; count < linesPerPage && !sPrint.EndOfStream; count++)
    {
        e.Graphics.DrawString(sPrint.ReadLine(),
            richTextBoxResult.Font,
            Brushes.Black,
            e.MarginBounds.Left,
            e.MarginBounds.Top + (count * fontHeight),
            new StringFormat());
    }
    e.HasMorePages = !sPrint.EndOfStream;

}



My questions:-

1. I need to fit all textbox content correctly when I print. (Lengthy single row not printed, rather jumped outside of the page)

2. Are there any possibilities to enable print range option under print Menu? (On output GUI, Let's say i have text content which can fit on more than one page)


Please help.
Thank You.
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