Thursday 28 April 2011

Print Priview Page For Richtexbox


Print Priview Page For Richtexbox



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 print_dialog_boxes
{
    public partial class Form3 : Form
    {
        private PrintPreviewControl ppc;
        private PrintDocument docToPrint = new PrintDocument();     


        public Form3()
        {
            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.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.LoadFile(ofdlg.FileName, RichTextBoxStreamType.PlainText);
                this.Text = ofdlg.FileName.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();
            f4.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Form frm = new Form();
            CreatePrintPreviewControl();
            //frm.Show();
        }
        private void CreatePrintPreviewControl()
        {
            ppc = new PrintPreviewControl();
            ppc.Name = "PrintPreviewControl1";
            ppc.Dock = DockStyle.Fill;
            ppc.Location = new Point(150,100);
          
            ppc.Document = docToPrint;
            ppc.Zoom = 25;
            ppc.Document.DocumentName = "c:\\";
            ppc.UseAntiAlias = true;
            ppc.UseWaitCursor = true;
            ppc.AutoZoom = true;
            ppc.AllowDrop = true;
           

            // Add PrintPreviewControl to Form
            Controls.Add(this.ppc);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.docToPrint.PrintPage += new PrintPageEventHandler(PrintPage);

        }
      
        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            string text = "This text to be printed. ";
            e.Graphics.DrawString(text, new Font("Georgia", 35, FontStyle.Bold),Brushes.Black, 10, 10);

            if (richTextBox1.TextLength > 0)
            {
                string s1 = richTextBox1.Text;
                e.Graphics.DrawString(s1, new Font("Georgia", 35, FontStyle.Bold), Brushes.Black, 100, 100);
            }
            else
            {
                MessageBox.Show("rich box text Must Contain One Word:");
            }
           // e.Graphics.DrawImage(this.pictureBox1.Image, 0, 0, this.pictureBox1.Size.Height, this.pictureBox1.Size.Width);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

       


    }
}

No comments:

Post a Comment