3

I'm trying to print a file with C#. I have made some headway in listing off all the printers and then wrote some simple logic to select the correct printer:

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.");
    }
}

The next step, as for the posts I've seen on this site, is that you need to select the specific queue, and then add a job, grab the job stream, and write to the stream (at least that's how I want to try to do it, unless it's wrong?)

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

When I do this, the program crashes at the line with AddJob. Specifically,

System.ArgumentNullException: 'Value cannot be null, Parameter name: printingHandler'

Now, I think I understand what the issue is. I had been playing with System.Drawing.Printing.PrintDocument yesterday, but I am trying to find a solution that allows me to print files, rather than manually draw them out and them print them. Ultimately, the goal in the future is to be able to print out text and PDF files (I was hoping that this solution would allow me to open a PDF file and dump the bytes into this stream, but I don't know if that's the correct way to this?)

Anyway, the exception I got I think is something similar to PrintDocument's PrintPageEventHandler, I need to add a callback to the PrintQueue somehow that tells it the font, color, font size, etc. Problem is that I see nothing for PrintQueue that allows me to add a handle for it to fix this issue.

What can I do to fix this exception?

Zulukas
  • 1,180
  • 1
  • 15
  • 35
  • We can't answer this without seeing the `AddJob` method. I'm guessing that it has an optional parameter which defaults to null, but it doesn't handle the scenario where that parameter is null. – Scott Hannen Dec 15 '17 at 17:36
  • Please clarify the difference between “to print files” and to ”draw them out and them print them”. Are you trying to [have another application print the file](https://stackoverflow.com/questions/10174156/)? – Dour High Arch Dec 15 '17 at 17:37
  • @DourHighArch, I'm trying to have this application print the file, or if there is some application native to every Windows machine that can handle it, I suppose that'd be preferable if I could specify the printer. With the PrintDocument object, you have to provide a callback to the property `PrintPage`. I would hope there was some way to just throw a file at it, and it'd know how to print it out. Sorry if I can't explain myself better, this is something I have very little knowledge on and don't really know how to ask the question correctly. – Zulukas Dec 15 '17 at 17:41

1 Answers1

5

I was having this issue as well. Eventually I found out that we need to call into Refresh() of the selected PrintQueue instance and before calling AddJob().

Noctis Tong
  • 459
  • 1
  • 7
  • 17