Search code examples
c#.net-3.5printingwindows-servicessystem.printing

Printing EMF from a Windows service


I have some code that prints a document using the System.Drawing.Printing namespace. This now needs to be moved into a windows service. The documentation states that this is not possible, which would support my findings so far (in that it doesn't work). I'm working under the assumption that I can get this to work by using the System.Printing namespace instead.

Here is the code that I have so far:

private Metafile myDoc;

myDoc = GetEmfDoc();

PrintServer ps = new PrintServer("\\server1\printer1");
EnumeratedPrintQueueTypes[] flags = { EnumeratedPrintQueueTypes.Local };
PrintQueueCollection queues = ps.GetPrintQueues(flags);

PrintQueue pq = queues.First<PrintQueue>();                
pq.AddJob("JobName", ?);

So I want to pass myDoc to pq.AddJob(), but this doesn't seem to be possible. Using System.Drawing.Printing it was possible to simply render this on a page by page basis in the PrintDocument.PrintPage event.

Is what I'm trying to do possible, and if so, could someone point me in the right direction?


Solution

  • Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

    It is important to understand what "not supported" means in this context. It literally means what it says, it you try to print from a service and discover a problem then you cannot call Microsoft Support and ask for help.

    This is not a problem that's induced by System.Drawing, it is a generic problem with printer drivers. Chunks of code that are supplied by the printer manufacturer, not Microsoft. A significant problem with them is that they are invariably designed to be used from a desktop program. Word, Acrobat, etcetera. They readily take advantage of that, they often pop up a window to let you know that they are Working On It. Not in the least to spam the manufacturer name and reminding you that the toner cartridge is half empty. These drivers are likely to misbehave in a service. The "runtime exceptions" phrase mentioned in the note.

    The worst problem with them is that they use a popup window to let you know that there's a problem. These windows are not visible when you use the printer driver from a service. So you'll have to deal with print jobs that for no discernible reason refuse to print. The "diminished service performance" phrase in the note. That's very difficult to deal with, IT staff is just powerless to have a shot at fixing the problem when they can't see any diagnostic.

    Your approach doesn't solve that fundamental problem. If you really want to pursue this then contact the printer manufacturer and ask for specific advise. Don't get your hopes up, getting good answers out of hardware companies is pretty difficult.