gpt4 book ai didi

c# - 如何在基于 .NET 的非 GUI 应用程序中打印?

转载 作者:行者123 更新时间:2023-11-30 17:25:42 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

有一些建议here ,但不是简单的(比如使用 P/Invoke 来使用 C++ 库进行打印,这是我的第一个想法)。如果您进行搜索,您或许能够找到已经这样做过的人。

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

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

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

关于c# - 如何在基于 .NET 的非 GUI 应用程序中打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57909756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com