1

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.

  1. Changed my application pool to run as LocalUser account.
  2. Added my LocalUser account to the IIS_WPG and IIS_IUSRS group.
  3. Ran the aspnet_regiis -ga DOMAIN\USER command for .NET 4
  4. Added my LocalUser to "Log on as a service" and "Log on as a batch job" under Local Policies.
  5. 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.

briank
  • 83
  • 11
  • May be doesn't work for your scenario, but I normally generate PDFs so I let the user to print it, if he wants to, in whatever printer. There are some nice .NET pdf generation libraries out there. – mattinsalto Mar 14 '17 at 15:49
  • Yeah, not quite what I need. I am actually creating SSRS reports through code and printing automatically based off an event happening. But I am just trying to really get the proof of concept to work with the simple code from above. – briank Mar 14 '17 at 15:50
  • Have you executed `System.Drawing.Printing.PrinterSettings.InstalledPrinters` in your web app to see whether the named printer is in that list in different contexts, maybe with different names? – NineBerry Mar 14 '17 at 16:22
  • Have you seen? http://stackoverflow.com/questions/3729153/printing-from-asp-net-to-a-network-printer?rq=1 – NineBerry Mar 14 '17 at 16:43
  • @NineBerry I have, and the printer name that is listed is the one I am using. I did see that question and tried the impersonation through code and web.config but no luck with that either. I currently have the network printer installed on my OS. – briank Mar 14 '17 at 17:35
  • Did you consider the Javascript plugin ? You can use CSS styling to format your output refer to this http://www.htmlgoodies.com/beyond/javascript/article.php/3471121/Print-a-Web-Page-Using-JavaScript.htm – Sailor Mar 14 '17 at 18:20

0 Answers0