Sunday, 1 May 2011

PageSetup Dialog In Win Forms:-



PageSetup Dialog In Win Forms:-


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

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        private Font mainTextFont = new Font("Times New Roman", 14);
        private Font subTextFont = new Font("Times New Roman", 12);
        private PageSettings storedPageSettings;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                PageSetupDialog psDlg = new PageSetupDialog();
                if (this.storedPageSettings == null)
                    this.storedPageSettings = new PageSettings();
                psDlg.PageSettings = this.storedPageSettings;
                psDlg.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        protected void PrintPageEventHandler(Object obj, PrintPageEventArgs ev)
        {
            Graphics g = ev.Graphics;
            PaintDocument(g);
            ev.HasMorePages = false;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            PaintDocument(g);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);

                if (this.storedPageSettings != null)
                    pd.DefaultPageSettings = this.storedPageSettings;
                PrintPreviewDialog dlg = new PrintPreviewDialog();
                dlg.Document = pd;
                dlg.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);

                if (this.storedPageSettings != null)
                    pd.DefaultPageSettings = this.storedPageSettings;
                PrintDialog dlg = new PrintDialog();
                dlg.Document = pd;
                DialogResult result = dlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                    pd.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void PaintDocument(Graphics g)
        {
            g.PageUnit = GraphicsUnit.Point;
            g.DrawString("Simple Printing Sample",
                         this.mainTextFont,
                         Brushes.Black,
                         new Rectangle(10, 20, 180, 30));
            g.DrawRectangle(Pens.Blue, new Rectangle(new Point(10, 100), new Size(100, 50)));
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

No comments:

Post a Comment