using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void OpenBTN_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader sr;            
            openFileDialog1.Filter = "Text Files|*.txt|Doc Files|*.doc";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sr = new System.IO.StreamReader(openFileDialog1.FileName);
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }
        private void FontBTN_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = richTextBox1.SelectionFont;
            fd.ShowColor = true;           
            if (fd.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SelectionFont = fd.Font;
                richTextBox1.SelectionColor = fd.Color;
            }
        }
        int l;
        private void PrintBTN_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                l = 0;
                printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Blue, e.MarginBounds);
            Font fnt = richTextBox1.Font;
            float lm = e.MarginBounds.Left, tm = e.MarginBounds.Top;
            while (l < richTextBox1.Lines.Length)
            {
                e.Graphics.DrawString(richTextBox1.Lines[l], fnt, Brushes.Black, new PointF(lm, tm));
                l++;
                tm += fnt.GetHeight();
                if (tm > e.MarginBounds.Bottom - fnt.GetHeight()) break;                                
            }
            if (tm > e.MarginBounds.Bottom - fnt.GetHeight())
            {
                tm = e.MarginBounds.Top; e.HasMorePages = true;
            }
            else e.HasMorePages = false;
        }
    }
}
