我正在尝试使用 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.");
}
}
Run Code Online (Sandbox Code Playgroud)
下一步,至于我在这个网站上看到的帖子,你需要选择特定的队列,然后添加一个作业,抓取作业流,然后写入流(至少我是这样想的)尝试这样做,除非它是错的?)
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);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,程序在AddJob. 具体来说,
System.ArgumentNullException: 'Value cannot be null, Parameter name: printingHandler'
Run Code Online (Sandbox Code Playgroud)
现在,我想我明白问题是什么了。我昨天一直在玩 System.Drawing.Printing.PrintDocument,但我试图找到一种解决方案,让我可以打印文件,而不是手动绘制它们并打印它们。最终,未来的目标是能够打印出文本和 PDF 文件(我希望这个解决方案能让我打开一个 PDF 文件并将字节转储到这个流中,但我不知道这是否是正确的方法?)
无论如何,我认为的异常类似于 PrintDocument 的 PrintPageEventHandler,我需要向 PrintQueue 添加一个回调,以某种方式告诉它字体、颜色、字体大小等。问题是我看不到 PrintQueue 的任何内容,它允许我添加一个句柄来解决这个问题。
我能做些什么来解决这个异常?
| 归档时间: |
|
| 查看次数: |
686 次 |
| 最近记录: |