我正在尝试用C#打印一个文件。我已经列出了所有的打印机,然后写了一些简单的逻辑来选择正确的打印机:
var server = new PrintServer();
var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }).ToList();
int count = 0;
foreach (var q in queues)
{
Console.WriteLine(count++ + " " + q.Name);
}
int iSelection = 0;
while (true)
{
Console.Write("Select printer: ");
string selection = Console.ReadLine();
if (int.TryParse(selection, out iSelection) && iSelection >= 0 && iSelection < queues.Count())
{
break;
}
else
{
Console.WriteLine("Bad selection, try again.");
}
}对于我在这个站点上看到的帖子,下一步是您需要选择特定的队列,然后添加一个作业,获取作业流,并将其写入流(至少这是我想要尝试的方式,除非它是错误的?)
var queue = queues[iSelection];
var job = queue.AddJob(@".\Test.txt");
var stream = job.JobStream;
var file = File.ReadBytes(@".\Test.txt");
stream.Write(file, 0, file.Length);当我这样做的时候,程序在使用AddJob时崩溃了。具体来说,
System.ArgumentNullException: 'Value cannot be null, Parameter name: printingHandler'现在,我想我知道问题出在哪里了。我昨天一直在玩System.Drawing.Printing.PrintDocument,但我正在尝试找到一种解决方案,让我可以打印文件,而不是手动将它们绘制出来并打印出来。最终,未来的目标是能够打印出文本和PDF文件(我希望这个解决方案能让我打开一个PDF文件并将字节转储到这个流中,但我不知道这是不是正确的方法?)
无论如何,我得到的异常我认为是类似于PrintDocument的PrintPageEventHandler,我需要以某种方式向PrintQueue添加一个回调,告诉它字体,颜色,字体大小等。问题是我没有看到PrintQueue允许我为它添加句柄来解决这个问题。
我能做些什么来修复这个异常?
发布于 2018-08-24 11:35:16
我也遇到了这个问题。最后,我发现在调用AddJob()之前,我们需要调用所选PrintQueue实例的Refresh()。
https://stackoverflow.com/questions/47837231
复制相似问题