Windows Forms: Printing Text example in a Windows Form using C#

Printing Text example in a Windows Form using C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintText" and then click OK

print text exampleStep 2: Design your form as below

print text in windows form

You need to add a PrintDocument, PrintPreviewDialog to your form

print preview dialog in c#

Step 3: Add code to handle your form

using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //Draw text to your document
            e.Graphics.DrawString(txtValue.Text, new Font("Times New Roman", 14, FontStyle.Bold), Brushes.Black, new PointF(100, 100));
        }

        private void Print_Click(object sender, EventArgs e)
        {
            //Open print dialog
            if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }
    }
}

VIDEO TUTORIALS