Let's say I have a .NET-based Windows application without a GUI (e.g., a Windows service, a console application or a web service), and I need to print something (e.g., an automatically created invoice) on a physical printer.
Which classes of the BCL would I use? This is what I have found so far:
System.Drawing.Printing.PrintDocumentClass: The documentation explicitly mentions that this is for printing in Windows Forms applications. In particular, it mentions that WPF applications should use classes from the System.Printing namespace instead.System.PrintingNamespace: The documentation explicitly mentions that these classes are "...not supported for use within a Windows service or ASP.NET application or service."
So, what should I use to print from non-GUI applications? (And why in the world are the printing classes "tied" to the UI framework anyway?)
It should work just fine from a console app. But yeah, Windows service or ASP.NET aren't so easy.
There are some suggestions here, but not easy ones (like using P/Invoke to use the C++ libraries to print, which was my first idea). You might be able to find people who have already done that if you search.
This answer recommends a third-party product: DevExpress' XtraReports.
There is also this guy on Reddit that described how he solved this exact thing. You could send him a message on Reddit and see if you can get his code somehow.
This example uses
Microsoft.Office.Interop.Wordto print Word documents from a Windows service. Seems "hacky", but I don't see why it wouldn't work: