首页 > 解决方案 > 如何在基于 .NET 的非 GUI 应用程序中打印?

问题描述

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

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

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

标签: c#.netserviceprintingbase-class-library

解决方案


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

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

此答案推荐第三方产品:DevExpress' XtraReports

Reddit 上还有一个人描述了他是如何解决这个问题的。你可以在 Reddit 上给他发消息,看看你能不能得到他的代码。

此示例用于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;  
 }  
}

推荐阅读