Print Document:
You can add a print document feature on your windows applications using the print dialog and print document control. Following is the step by step procedure for doing that task.
Step1: Drag a print dialog control on your windows form. Form the properties of that control change its name to Print Dialog from Print dialog1.
Step2: Now go to the file menu and double click on Print. printToolStripMenuItem_Click event will be opened. Add following code in its on_click event.
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult buttonClicked = printDialog.ShowDialog();
if (buttonClicked.Equals(DialogResult.OK))
{
}
}
Step3: Now add a print Document control on windows form and change its name to Print Document. Also remove the value of its Document Name property from properties window.
Step4: Now open the properties of Print Dialog and change Document property’s value to print document.
Step5: Now in the Print_click event Add following line of code.
Step6: Now double click on the Print Document control and add following code in printpage event.
private void printPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringBuilder data = new StringBuilder();
StringWriter writer = new StringWriter(data);
writer.WriteLine("First Name: " + FirstName.Text);
writer.WriteLine("Last Name: " + LastName.Text);
writer.WriteLine("Location: " + LocationName.Text)
writer.Close();
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
float yPos = 0;
Font printFont = null;
printFont = new Font("Arial", 12);
yPos = topMargin + printFont.GetHeight(e.Graphics);
e.HasMorePages = false;
e.Graphics.DrawString(data.ToString(), printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
}
Step7: Process of adding printing feature has been completed. Now run your application and click on the print option in file menu. A Print dialouge box will be opened. Select your printer and click on print button to print your document.
