ASP.NET applications run under an account with limited privileges that doesn’t have the right to connect to a network printer. The normal answer for this is to give rights to the user running your application or impersonate a user during execution that does have the rights.
We found another way that doesn’t require changing the user or privileges of the existing account that ASP.NET apps run under.
I can hear someone asking “Why would you want to do this anyway? Can’t the end user choose a printer?” There might be situations where this isn’t sufficient enough. We had to control the printer line by line in order to print on the form that is being used and a particular printer had to be used.
We added the network printer as a printer on the server where our app was running. When we setup the printer we chose local not networked but then on the options to use a printer port we didn’t use LPT1. Instead we created a new port with the IP address of the device. In our code we then call the printer by the name we gave it on our server.
The PrintDocument class in the .NET Framework allows for control of a printer.
Public Class SpecialReportPrintJob
Protected Overrides Sub OnBeginPrint(ByVal ev as Printing.PrintEventArgs)
Me.PrinterSettings.PrinterName = "SCLaser2"
'setup rest of stuff....
'And we then call it like so
Dim printSpecialReport as new SpecialReportPrintJob()
public class SpecialReportPrintJob : Printing.PrintDocument
protected override void OnBeginPrint(Printing.PrintEventArgs ev)
this.PrinterSettings.PrinterName = "SCLaser2";
//setup rest of stuff....
//And we then call it like so
SpecialReportPrintJob printSpecialReport = new SpecialReportPrintJob();
PrintDocument is pretty powerful. It can control just about anything for your printer. We passed in a text file of our data to print and our code setup the x, y coordinates of each item as it related to where on the form it needed to be.
All this without having to muck around with permissions. Always a good thing!