I am having issues automatically printing to a network printer from the Web. This web application will be on a local intranet and the feature was requested by the client. I have some really simple print code that I have copied and pasted between several .NET projects. I was able to successfully execute and print this from a WinForm application and a windows service application. When I try from the web the code executes but nothing is sent to the printer and no exceptions have occurred. If I run the web application under IIS Express the code executes and prints successfully which leads me to believe it's a permissions issue. Steps I have done to try and get this to work. FYI, I am using Win7x64 with IIS7.5 and the LocalUser does have access to network printer.
- Changed my application pool to run as LocalUser account.
- Added my LocalUser account to the IIS_WPG and IIS_IUSRS group.
- Ran the aspnet_regiis -ga DOMAIN\USER command for .NET 4
- Added my LocalUser to "Log on as a service" and "Log on as a batch job" under Local Policies.
- Restarted IIS and machine.
Here is the print code:
protected void Button1_Click(object sender, EventArgs e)
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = "Xerox WorkCentre 7845";
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
public void PrintPage(object sender, PrintPageEventArgs e)
{
// Create string to draw.
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create rectangle for drawing.
float x = 150.0F;
float y = 150.0F;
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(x, y, width, height);
// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Black);
e.Graphics.DrawRectangle(blackPen, x, y, width, height);
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
}
Any help with this will be much appreciated.