【问题标题】:How do I print in .NET-based non-GUI applications?如何在基于 .NET 的非 GUI 应用程序中打印?
【发布时间】:2019-09-12 15:12:37
【问题描述】:

假设我有一个没有 GUI 的基于 .NET 的 Windows 应用程序(例如,Windows 服务、控制台应用程序或 Web 服务),我需要在物理上打印一些东西(例如,自动创建的发票)打印机。

我将使用 BCL 的哪些类?这是我目前发现的:

  • System.Drawing.Printing.PrintDocument Class:文档明确提到这是为了在 Windows 窗体应用程序中打印。特别是,它提到 WPF 应用程序应该使用 System.Printing 命名空间中的类。
  • System.Printing Namespace:文档明确提到这些类“...不支持在 Windows 服务或 ASP.NET 应用程序或服务中使用。

那么,我应该使用什么从非 GUI 应用程序打印? (以及为什么打印类无论如何都“绑定”到 UI 框架?)

【问题讨论】:

  • 我猜他们都依赖 UI 库进行渲染和布局。许多用于构建打印文档的对象与用于在屏幕上显示内容的对象相同。 (而且 ASP 不呈现任何东西,它把它留给浏览器)

标签: c# .net service printing base-class-library


【解决方案1】:

它应该可以在控制台应用程序中正常工作。但是,是的,Windows 服务或 ASP.NET 并不那么容易。

有一些建议here,但并不容易(比如使用 P/Invoke 使用 C++ 库进行打印,这是我的第一个想法)。如果您搜索,您也许可以找到已经这样做的人。

This answer 推荐第三方产品:DevExpress' XtraReports

Reddit 上还有 this guy 描述了他是如何解决这个问题的。你可以在 Reddit 上send him a message 看看你能不能得到他的代码。

This example 使用Microsoft.Office.Interop.Word 从 Windows 服务打印 Word 文档。似乎“hacky”,但我不明白为什么它不起作用:

public class WordPrintTask  
{  
 private static object locker = new Object();  
 public WordPrintTask() { }  
 public void PrintWord()  
 {  
   try  
   {  
     // Kill opened word instances.  
     if (KillProcess("WINWORD"))  
     {  
       // Thread safe.  
       lock (locker)  
       {  
         string fileName = "D:\\PrinterDocs\\TEST.docx";  
         string printerName = "\\\\10.0.0.89\\PRINTER1020";  
         if (File.Exists(fileName))  
         {  
           Application _application = new Application();  
           _application.Application.ActivePrinter = printerName;  
           object oSourceFilePath = (object)fileName;  
           object docType = WdDocumentType.wdTypeDocument;  
           object oFalse = (object)false;  
           object oMissing = System.Reflection.Missing.Value;  
           Document _document = _application.Documents.Open(ref oSourceFilePath,  
                              ref docType,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing,  
                              ref oMissing);  
           // Print  
           _application.PrintOut(ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,  
                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);  
           object saveOptions = WdSaveOptions.wdDoNotSaveChanges;  
           _document.Close(ref oFalse, ref oMissing, ref oMissing);  
           if (_application != null)  
           {  
             object oSave = false;  
             Object oMiss = System.Reflection.Missing.Value;  
             _application.Quit(ref oSave, ref oMiss, ref oMissing);  
             _application = null;  
           }  
           // Delete the file once it is printed  
           File.Delete(fileName);  
         }  
       }  
     }  
   }  
   catch (Exception ex)  
   {  
     KillProcess("WINWORD");  
   }  
   finally  
   {  
   }  
 }  
 private static bool KillProcess(string name)  
 {  
   foreach (Process clsProcess in Process.GetProcesses().Where(p => p.ProcessName.Contains(name)))  
   {  
     if (Process.GetCurrentProcess().Id == clsProcess.Id)  
       continue;  
     if (clsProcess.ProcessName.Contains(name))  
     {  
       clsProcess.Kill();  
       return true;  
     }  
   }  
   return true;  
 }  
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多