How can I print Office documents from .NET in a uniform manner?
Using Microsoft interOp objects, I can programmatically print a Word or Excel document.
Is there any simpler and uniform way to programmatically print all types of documents (.txt, .doc, .开发者_StackOverflow社区xls, .pdf etc.)?
Microsoft uses DDE by default to print from office products. Go to Tools/Folder Options/File Types, then lookup .XLS for example and click 'Advanced'. You will see a list of actions, one of which is 'Print'. Inside this is the DDE command that Excel uses to print then close your document, and one of these commands exists for each product in the Office suite. Unfortunately DDE uses the SendMessage APIs, but there was a library here that wraps those functions up for you:
http://ndde.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=4828
It's no longer being updates but should still do what you require if you only want a generic way of printing the documents.
If you're using Interop and already have theinstance of the application open, then just call the appropriate PrintOut function. e.g. for Word, it's:
object nullobj = Missing.Value;
doc.PrintOut(ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
Clearly, both interop and DDE are far from ideal solutions, but Office is still based upon the same Win32 core that it was in the 90's.
Once they recode it in managed code we will have a nice common set of interfaces for creating, printing and viewing office documents!
Cheers, Jason
精彩评论