I am developing a notepad using Visual C# but cant figure out which function to use among those to print the text in the main text box through a printer.

Recommended Answers

All 4 Replies

Use following classes or controls.

System.Drawing.Printing.PrintDocument
System.Windows.Forms.PrintDialog

Use following classes or controls.

System.Drawing.Printing.PrintDocument
System.Windows.Forms.PrintDialog

I have used the System.Windows.Forms.PrintDialog but now which function should I use to print the document through a printer.

Use following code :

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
        private void Form1_Load(object sender, EventArgs e)
        {
            // pd.PrinterSettings.PrinterName = "novaPDF Lite v3";

            pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
            pd.BeginPrint += new System.Drawing.Printing.PrintEventHandler(pd_BeginPrint);
            pd.EndPrint += new System.Drawing.Printing.PrintEventHandler(pd_EndPrint);
            pd.Print();
        }

        void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString("Hello", new Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(20, 20));
            
        }

        void pd_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            
        }

        void pd_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.