从Web C#(MVC / WebForm)

时间:2017-03-14 15:36:35

标签: c# asp.net asp.net-mvc printing

我遇到从网络自动打印到网络打印机的问题。 此Web应用程序将位于本地Intranet上,并且客户端请求该功能。我有一些非常简单的打印代码,我已经复制并粘贴在几个.NET项目之间。我能够从WinForm应用程序和Windows服务应用程序成功执行并打印它。当我从网上尝试代码执行但没有任何内容发送到打印机并且没有发生异常。如果我在IIS Express下运行Web应用程序,代码会执行并成功打印,这使我相信它是一个权限问题。我已经做的步骤试图让它工作。仅供参考,我使用Win7x64和IIS7.5,LocalUser可以访问网络打印机。

  1. 将我的应用程序池更改为以LocalUser帐户身份运行。
  2. 将我的LocalUser帐户添加到IIS_WPG和IIS_IUSRS组。
  3. 执行.NET 4的aspnet_regiis -ga DOMAIN \ USER命令
  4. 将我的LocalUser添加到"作为服务登录"和"以批处理作业登录"根据当地政策。
  5. 重新启动IIS和计算机。
  6. 这是打印代码:

        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);
        }
    

    对此的任何帮助将不胜感激。

0 个答案:

没有答案