Thursday, 28 April 2011

Print preview richtexbox by rajesh


Print preview richtexbox by rajesh




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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private Font printFont;
        private PrintPreviewDialog ppdlg;
        private PrintDocument pd = new PrintDocument();
      
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();

            ofdlg.InitialDirectory = @"C:\";
            ofdlg.RestoreDirectory = true;
            ofdlg.Title = "Open Text Files";
            ofdlg.DefaultExt = "txt";
            ofdlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            ofdlg.FilterIndex = 2;
            ofdlg.CheckFileExists = true;
            ofdlg.CheckPathExists = true;

            if (ofdlg.ShowDialog() == DialogResult.OK)
            {              
                richTextBox1.LoadFile(ofdlg.FileName, RichTextBoxStreamType.PlainText);
                this.Text = ofdlg.FileName.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ppdlg = new PrintPreviewDialog();

            ppdlg.Document = pd;
            ppdlg.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            printFont = new Font("Arial", 10);
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        }
      
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {         
            string s1 = richTextBox1.Text;
            e.Graphics.DrawString(s1, new Font("Georgia", 10, FontStyle.Bold), Brushes.Black,50,50);
                      
        }
    }
}

No comments:

Post a Comment