Thursday 28 April 2011

Print_preview in WinForms


Print_preview in WinForms




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

namespace print_examples1
{
    public partial class Form1 : Form
    {
            private Font printFont;
                    private StreamReader streamToPrint;
                    private PrintDocument pd = new PrintDocument();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {              
            //print priview button
            try
                   {
               OpenFileDialog ofdlg = new OpenFileDialog();

               ofdlg.Title = "Open Text Files";
               ofdlg.DefaultExt = "txt";
               ofdlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
               ofdlg.FilterIndex = 2;

               if (ofdlg.ShowDialog() == DialogResult.OK)
               {
                   this.Text = ofdlg.FileName.ToString();
                   //string s1=ofdlg.FileName ;

                   streamToPrint = new StreamReader(ofdlg.FileName);
                   try
                   {
                       PrintPreviewDialog ppdlg = new PrintPreviewDialog();

                       ppdlg.Document = pd;
                       ppdlg.ShowDialog();
                   }
                   finally
                   {
                       streamToPrint.Close();
                   }
               }
           }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

        }
       
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
                    {
                        float linesPerPage = 0;
                        float yPos = 0;
                        int count = 0;
                        float leftMargin = ev.MarginBounds.Left;
                        float topMargin = ev.MarginBounds.Top;
             
                        string line = null;
             
                        // Calculate the number of lines per page...
                        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
             
                        // Print each line of the file...
                        while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
                        {
                            yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin,
                                yPos);
                            count++;
                        }
             
                        // If more lines exist, print another page...
                        if (line != null)
                            ev.HasMorePages = true;
                        else
                            ev.HasMorePages = false;
                    }

        private void Form1_Load(object sender, EventArgs e)
        {
            printFont = new Font("Arial", 10);
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
       
        }
   
           
    }
}

No comments:

Post a Comment