How can I print Office documents from .NET in a uniform manner?

青春壹個敷衍的年華 提交于 2019-12-08 02:32:31

问题


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, .xls, .pdf etc.)?


回答1:


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



来源:https://stackoverflow.com/questions/1559481/how-can-i-print-office-documents-from-net-in-a-uniform-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!