tags:

views:

598

answers:

3

This is a two-part question:

1.

The original .NET print classes (in System.Drawing.Printing) are not supported on the server side. (See http://msdn.microsoft.com/en-us/library/system.drawing.printing%28VS.80%29.aspx )

I think that the newer XPS-based printing classes (in System.Printing) are supported on the server side, e.g. in ASP.NET apps and Windows Services, but I can't prove it. And Microsoft have not answered my questions about it.

Does anyone here know?

2

The new XPS-based printing will sometimes do an internal conversion to GDI. That is for cases where the only driver available is an old-style driver, even though the app is printing with the new printing classes. See http://msdn.microsoft.com/en-us/library/ms742418.aspx . Are the new classes safe for server-side use in that situation?

  • To clarify - this is entirely about the server printing stuff. For the purposes of this discussion, there is no web browser involved at all. A server, either windows service or asp.net, needs to directly print out a document, on a printer that is attached to the server.

Thanks.

A: 

If you're trying to get the user's browser to print from server code, forget it. The best you should hope for is to send a page to the browser with some javascript code that calls window.print().

Joel Coehoorn
Added clarification above - this is not browser-related.
John Rusk
A: 

In .Net XPS support is part of WPF. The use of WPF in Windows services is unsupported (see MSDN) and therefore XPS printing with .Net, including use of System.Printing, is also unsupported for services.

The same answer applies for the 'conversion to GDI' part of the question since that process happens automatically (in the case that XPS content is being printed to a PrintQueue where the driver is not XPS, the framework automatically converts XPS content to the DDI calls expected by the driver if a GDI-based app were printing).

For server-side development (services) where XPS printing is required there are Win32 APIs available in Windows 7. Specifically, see the XPSPrint API which provides access to the XPS Print Path and supports automatic conversion for non-XPS print queues as well as the APIs available for maniplulating XPS content and working with print tickets.

Hmmm. That would imply that there is no supported way to print from managed code server-side. That sounds like a huge omission on Microsoft's part!(By the way, the reason given for the lack of support for WPF in Windows services, on the link you posted, is about WPF having "permissions to perform visual operations that involve user interaction". The printing scenario has none of those...)
John Rusk
For the record, I contacted Microsoft and they explained there are a number of reasons why System.Printing is not supported on the server. It is not just because of the possibility of user interaction (dialogs etc) - there are other reason(s) why it is not supported server side.So the only supported options for printing server side are the Win32 GDI or the new "XPS Print" API, which is available via an add-on pack for OS's prior to Windows 7. The latter is really designed to be called from C++. They recommended against using XPS Print via C#'s PInvoke.
John Rusk
A: 

As noted in my comment below, there is no supported solution for server-side printing in pure managed code.

But, Aspose have just released some code that lets you print XPS documents from managed code (successfully using PInvoke to call the XPS Print API). [For the record, I believe that Microsoft's intitial recommendation against using PInvoke to call XPS print was simply because it is a difficult API to interact with using PInvoke. But Aspose seem to have succeeded, which is good news, since it removes the need to involve separate any separate unmanaged DLL.]

All in all, the Aspose solution looks like the easiest fully-supported way to print complex documents from ASP.NET and Windows services.

Details here: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-print-a-document-on-a-server-via-the-xpsprint-api.html

John Rusk