In .net applications, printing and previewing the form can be done using PrintDocument class. The PrintDocument component is an object that allows users to send an output to a printer.
The following code will help to print the form during the run time.
public void RunSample()
{
try
{
PrintDocument MyPrintDocument = new PrintDocument();
MyPrintDocument.PrintPage += new PrintPageEventHandler(this.PrintPageEvent);
MyPrintDocument.PrinterSettings.Copies = 2;
MyPrintDocument.Print();
}
catch (Exception ex)
{
Response.Write("Error while printing ... " + ex.ToString());
}
}
private void PrintPageEvent(object sender, PrintPageEventArgs ev)
{
string MyHelloString = "Welcome to Asp.net!";
Font MyFont = new Font("Arial", 10);
Rectangle marginRect = ev.MarginBounds;
ev.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Black), marginRect);
ev.Graphics.DrawString(MyHelloString, MyFont, new SolidBrush(System.Drawing.Color.Blue),
(ev.PageBounds.Right / 2), ev.PageBounds.Bottom / 2);
}
protected void Button1_Click(object sender, EventArgs e)
{
RunSample();
}