问题
Is it possible to automate the following: referencing MS Word Viewer to open a document programatically and then print it? C# ideally
I'm guessing if it is possible to open it then more than likely it will be possible to print it.
I've tried adding a reference to COM Object in Visual Studio .. MS Office 11 / 12 Object Library but MS Word Library isnt listed? Any ideas?
I haven't got Office 200x installed
cheers
回答1:
We did it by using the Word Interop assembly. This requires Word to be installed (launches a WINWORD process behind the scenese) and the interop allows you to interact with it in your code.
As far as I know, that is the only way to do it.
回答2:
Try Aspose.Words, it's designed to allow for Office automation without any dependencies on having Word installed. It provides a nice API to open the document then perform a range of actions such as print, export to pdf and many other outcomes.
回答3:
maybe like this:
class Program
{
static void Main(string[] args)
{
PrintDocument(@"C:\test.docx", 2);
Console.ReadKey();
}
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private static void PrintDocument(string name, int copies)
{
var process = System.Diagnostics.Process.Start(new ProcessStartInfo
{
FileName = name,
UseShellExecute = true
});
process.WaitForInputIdle();
SetForegroundWindow(process.MainWindowHandle);
SendKeys.SendWait("^p"); // send CTRL+P
SendKeys.SendWait(copies.ToString()); // send number of copies
SendKeys.SendWait("~"); // send ENTER
// -- or send all in one
//SendKeys.SendWait(string.Format("^p{0}~", copies));
}
}
回答4:
Following code will open up Word View with the file you pass to it.
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("WORDVIEW.exe", fileName.ToString());
System.Diagnostics.Process.Start(info);
Try to mess around with the arguments as well to pass a command line print (I do not know if you can).
And yes after exhausting every avenue there is no way I have found to Interop with the Microsoft Viewer which is very frustrating.
回答5:
Are referring to the free Microsoft Word Viewer, which allows you to view Word documents without actually having Word installed? If so, I don't believe there is a way to automate the viewer since it doesn't install the Word COM automation libraries, which is what you would need.
回答6:
This is how to use word automation services
Using the Interop assemblies is always a bad idea if it's run on the server Word Automation Services
That uses SharePoint which not everyone has. You also deliver the file to a web page via a WebRequestMethod
and print the page to a cute pdf writer or another driver. Just send the bytes of the file with a mime type. You would print in the page load of the asp.net web page.
来源:https://stackoverflow.com/questions/1291305/ms-word-viewer-c-sharp-net-automation