Tuesday, June 23, 2009

Printing using C#

Hi , printing is very easy case in all the task ,but when using ASP.NET, printing of web form is not that easy.
if we're building a UI we usually do so by painting controls on the screen and allowing whoever developed them to worry about the whys and wherefores about how that control should look.

If you want to print on server side, then you can use System.Drawing.Printing classes just like when you work with .Net Windows Forms application.
but if you are using client side then you need to consider more constraints and different possible solutions, depending of your specific needs.

For printing, you use the System.Drawing.Printing namespace. The main class in this namespace is the PrintDocument class, which represents an object that sends output to the printer. The role of this class is so central that you can achieve simple and complex printing tasks by using this class alone. However, as we shall see, other classes in this and other namespaces help make coding easier.
o print text or graphics, you call the Print method of the System.Drawing.Printing.PrintDocument class. One of the events the Print method invokes is the PrintPage event. You need to wire an event handler to the PrintPage event and write the code to send output to the printer. The event handler will receive an argument of type

Here are way to print the Document / Form.
Using PrintDialog

private void filePrintMenuItem_Click(Object sender , EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}



The filePrintMenuItem_Click event handler sends output to the printer if the user clicks the Print dialog box's OK button.

Page Setup
while priting adds the feature to set up the page that is used for printing.


1. private PageSettings pgSettings = new PageSettings();
2. Set pgSettings to the DefaultPageSettings property of printDoc before printing.

private void filePrintMenuItem_Click(Object sender , EventArgs e)
{
printDoc.DefaultPageSettings = pgSettings;
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}

3. Allow the user to change the setting of the page..

private void filePageSetupMenuItem_Click(Object sender , EventArgs e)
{
PageSetupDialog pageSetupDialog = new PageSetupDialog();
pageSetupDialog.PageSettings = pgSettings;
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.AllowMargins = true;
pageSetupDialog.ShowDialog();
}


4.Set the printDoc_PrintPage event handler to take into account the top and left margins.
private void printDoc_PrintPage(Object sender , PrintPageEventArgs e)
{
String textToPrint = ".NET Printing is easy";
Font printFont = new Font("Courier New", 12);
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black,
leftMargin, topMargin);
}




Print Preview
In a Windows application, before the user prints, they can normally view a preview of how the printout will like on paper. You can also provide this feature by adding the following code to the filePrintPreviewMenuItem_Click event handler.

private void filePrintPreviewMenuItem_Click(Object sender , EventArgs e)
{
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = printDoc;
dlg.ShowDialog();
}





here you can get best printing
example
Hope it will helpful for you,plz comment on it.

Thank You,

No comments:

Post a Comment