Sunday, 3 February 2013

how to print in c#.net using print doucument

using System.Drawing.Printing;
namespace WindowsFormsApplication22
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
      
        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString();
            pd.PrintPage+=new PrintPageEventHandler(pd_PrintPage);
            PrintPreviewDialog tmpprdiag = new PrintPreviewDialog();
            tmpprdiag.Document = pd;
            tmpprdiag.Show();
            pd.Print();
        }
        public void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Graphics g = ev.Graphics;
            Font f = new Font("Arial", 16);
            SolidBrush sb = new SolidBrush(Color.Black);
            g.DrawString(textBox1.Text, f, sb, new Rectangle(0, 5, 200, 100));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (PrinterSettings.InstalledPrinters.Count <= 0)
            {
                MessageBox.Show("Printer not found!");
                return;
            }
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                comboBox1.Items.Add(PrinterSettings.InstalledPrinters[i].ToString());
            comboBox1.SelectedIndex = 0;
        }

      
    }
}

No comments:

Post a Comment