首页 > 解决方案 > 有没有办法通过使用安装在应用程序托管服务器上的打印机从 Asp.Net MVC 应用程序“打印”(通过 c# 代码)?

问题描述

我公司有自己的服务器,我们在上面安装了几台打印机。Asp.Net MVC 应用程序在同一台服务器上运行。我的问题是,如何从应用程序打印到服务器上已安装的打印机之一?我不希望打印请求来自客户端,而是来自主机。

我已经使用“PrintServer”和“PrintQueue”类尝试了一些代码变体。但我似乎无法远程打印任何东西,程序运行后打印队列没有显示任何活动。下面的“选项”是我一直在尝试获取打印机列表以填充下拉列表的各种方法。但是,除了选项 4 之外的所有打印机都只能获取本地安装的打印机。我需要一种方法来填充服务器安装的打印机列表(通过选项 4 完成),然后选择该打印机并能够打印到它(仍然是一个谜)。

提前感谢我的代码骑士伙伴!

请不要不友善。:)

// GET: View
public ActionResult Index()
{      
  //BEGIN PRINTING AREA...
  //THIS SECTION IS FOR GETTING A LIST OF PRINTERS TO PRINT FROM...
  //OPTION 1 PRINTING...
  List<SelectListItem> optOnePrinters = new List<SelectListItem>();
  var server = new PrintServer();
  var queues = server.GetPrintQueues(new[]
  { EnumeratedPrintQueueTypes.Shared, EnumeratedPrintQueueTypes.Connections });
  foreach (var item in queues)
  {
    if(!optOnePrinters.Any(a=>a.Text == item.FullName))
    {
      optOnePrinters.Add(new SelectListItem() { Text = item.FullName, Value = item.ShareName });
    }
  }
  ViewData["PrinterListOptOne"] = optOnePrinters;

  //OPTION 2 PRINTING...
  List<SelectListItem> optTwoPrinters = new List<SelectListItem>();
  foreach (string sPrinters in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
  {
    optTwoPrinters.Add(new SelectListItem() { Text=sPrinters, Value=sPrinters });
  }
  ViewData["PrinterListOptTwo"] = optTwoPrinters;

  //OPTION 3 PRINTING...
  List<SelectListItem> optThreePrinters = new List<SelectListItem>();
  System.Management.ManagementScope objMS =
      new System.Management.ManagementScope(ManagementPath.DefaultPath);
  objMS.Connect();

  SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
  ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
  System.Management.ManagementObjectCollection objMOC = objMOS.Get();

  foreach (ManagementObject Printers in objMOC)
  {

    if (Convert.ToBoolean(Printers["Local"]))     // ALL LOCAL PRINTERS.
    {
      optThreePrinters.Add(new SelectListItem() { Text = Printers["DeviceID"] != null ? "(Local)" + (string)Printers["Name"] + ": " + (string)Printers["DeviceID"] : "(Local)" + (string)Printers["Name"], Value = (string)Printers["Name"] });
    }
    if (Convert.ToBoolean(Printers["Network"]))     // ALL NETWORK PRINTERS.
    {
      //Get list of Printer Properties...
      //foreach (PropertyData property in Printers.Properties)
      //{
      //  optThreePrinters.Add(new SelectListItem() { Text= property.Value == null ? property.Name : property.Name + ": " + property.Value.ToString(), Value=property.Value == null? property.Name: property.Value.ToString() });
      optThreePrinters.Add(new SelectListItem() { Text = "(Network)" + Printers["PortName"] !=null ? (string)Printers["Name"] + ": " + (string)Printers["PortName"] : (string)Printers["Name"], Value = (string)Printers["Name"] });
    }
  }
  ViewData["PrinterListOptThree"] = optThreePrinters;

  //OPTION 4 PRINTING...
  // Create a PrintServer
  // "theServer" must be a print server to which the user has full print access.
  List<SelectListItem> optFourPrinters = new List<SelectListItem>();

  PrintServer myPrintServer = new PrintServer(@"\\ServerName");
  // List the print server's queues
  PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
  foreach (PrintQueue pq in myPrintQueues)
  {
    optFourPrinters.Add(new SelectListItem() { Text = pq.Name, Value = pq.Name });
  }
  ViewData["PrinterListOptFour"] = optFourPrinters;
  return View();
}

  //END PRINTING...

//BEGIN PRINT FUNCTIONS ON DropDown SELECT FROM VIEW...
public ActionResult Print(string PrinterName)
{
  Feedback f = new Feedback();
  PrintDocument doc = new PrintDocument();
  doc.PrinterSettings.PrinterName = PrinterName;
  doc.PrinterSettings.PrintFileName = "AEMSNet_TestPrint";
  doc.PrintPage += new PrintPageEventHandler(PrintHandler);
  doc.Print();

  f.Success = true;
  f.Type = FeedbackType.Success;
  f.SuccessMsg = "Successfully Printing!";
  return Json(f, JsonRequestBehavior.AllowGet);
}

private void PrintHandler(object sender, PrintPageEventArgs ppeArgs)
{      
  Font FontNormal = new Font("Verdana", 9);
  Graphics g = ppeArgs.Graphics;      
  g.DrawString("Greetings from the Home Controller.", FontNormal, Brushes.Black, new RectangleF(10.0F, 100.0F, 750.0F, 80.0F), new StringFormat());           
}

标签: c#asp.net-mvcprintingserverprinters

解决方案


推荐阅读