C# Printing
Can anyone help me out with this printing problem? I am trying to print a document and have written the following:
public class PrintMe()
{
private PrintDocument printDoc = new PrintDocument();
public PrintMe()
{
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.Print();
}
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
String textToPrint = "The Doc Header";
Font printFont = new Font("Courier New", 12, FontStyle.Bold);
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, 0, 0);
}
}
I added this code to a small Windows application just for testing purposes and it worked. Having tested it I wanted to add it to a web application I am working on and did the following:
I added PrintMe class to the code behind file which happened to be called top.aspx.cs (outside 'top' class of course), then I added a HtmlAnchor called lnkPrint to the page and added an event handler:
private void lnkPrint_ServerClick(object sender, EventArgs e)
{
PrintMe pm = new PrintMe();
}.
The handler works and makes a call to PrintMe and everything is OK up to the point when printDoc.Print() is called and trying to execute it the application freezes forever. The target printer is connected (I checked) and works.
Is this method of printing meant to work in Web apps at all or I am missing some silly little thing?
Janette.
|