How to view Word document in WPF application (CSVSTOVIEWWORDINWPF) Introduction
The Sample demonstrates how to view Word document in WPF application. WPF does not the support for view Word documents directly but some customers want to show Word document in WPF. So we can use the WPF DocumentViewer control to host fixed document such as XPS document. And we also can convert Word document to XPS Document using VSTO.
Building the Sample
Before building the sample, please make sure so you have installed Microsoft Office in your machine.
Running the Sample
Step 1. Open CSVSTOViewWordInWPF.sln and click Ctrl+f5 to run the project. You'll see the following form:
Step 2. Click "Select Word File" button to Select a existing Word document on your machine
Step 3. Click the "View Word Doc" button to View the Word document in WPF DocumentViewer control. If Word document isn ' t exist, when you click the "View word Doc", you'll get the prompt message with "The file is Invali D. Please select an existing file again. "
If Word document is existing on machine and there are no error occurs, you'll see the following form:
Using the Code
Step 1. Create WPF Application project via Visual Studio
Step 2. Add needed references to the project
Step 3. Import the needed namespace into the MainWindow.xaml.cs class.
C#
Step 4. Design WPF UI form using XAML codes
Xaml
<Grid> <Grid.RowDefinitions> <rowdefinition height= "></RowDefinition>" <rowdefinition></ rowdefinition> </Grid.RowDefinitions> <label name= "Lable1" margin= "3,6,0,0" content= "Word Document:" Verticalalignment= "Top" horizontalalignment= "left"/> < textbox name= "Txbselectedwordfile" verticalalignment= "Top" horizontalalignment= "Stretch" Margin= " 110,10,300,0 "horizontalcontentalignment=" left "/> <Button Horizontalalignment= "Right" verticalalignment= "Top" width= "content=" "Select Word File" name= "Btnselectword" Margin= "0,10,130,0" click= "BtnseLectword_click "/> <button HorizontalAlignment=" left "Margin = "3,40,0,0" verticalalignment= "Top" content= "View Word Doc" width= "" Name= "Btnviewdoc" click= "Btnviewdoc_click"/ > <documentviewer grid.row= "1" name= "Documentviewword" Verticalalignment= "Top" horizontalalignment= "left"/> </Grid>
Step 5. Handle the events in behind class.
C#
<summary> /// Select Word file //</summary> /< param name= "Sender" ></param> //<param name= "E" > </param> private void Btnselectword_click (object sender, RoutedEventArgs e) { //Initialize an openfiledialog OpenFileDialog OpenFileDialog = new OpenFileDialog (); //Set filter and restoredirectory openfiledialog.restoredirectory = True; Openfiledialog.filter = "Word documents" (*.doc;*. docx) |*.doc;*.docx "; BOOL? Result =openfiledialog.showdialog (); if ( Result==true) { if (openFileDialog.FileName.Length > 0 ) { Txbselectedwordfile.text = openfiledialog.filename; } } } //<summary> /// Convert the Word document to XPS document //</summary> & nbsp; //<param name= "wordfilename" >word document Path</param> //<param name= "xpsfilename" >xps document Path</param > //<returns></returns> Private XpsDocument Convertwordtoxps (string wordfilename, string xpsfilename) { / /Create a wordapplication and host word document Word.Application WordApp = new Microsoft.Office.Interop.Word.Application (); try { WordApp.Documents.Open (wordfilename); /To Invisible the word document wordApp.Application.Visible = false; //Minimize the opened Word document WordapP.windowstate = wdwindowstate.wdwindowstateminimize; Document doc = wordapp.activedocument; Doc. SaveAs (Xpsfilename, Wdsaveformat.wdformatxps); xpsdocument xpsdocument = new XpsDocument (Xpsfilename, FileAccess.Read); Return xpsdocument; } catch (Exception ex) { & Nbsp; MessageBox.Show ("error occurs, the error message is " + ex. ToString ()); return null; } finally { WordApp.Documents.Close (); ((_application) WordApp). Quit (wdsaveoptions.wddonotsavechanges); } } //<summary> /// View Word Document in WPF documentview control //</summary>& nbsp; //<param name= "Sender" ></param> //<param name= "E" ></param> private void Btnviewdoc_click (object sender, RoutedEventArgs e) { String worddocument = txbselectedwordfile.text; if (string. IsNullOrEmpty (worddocument) | | ! File.exists (worddocument)) { MessageBox.Show ("the File is invalid. Please select an existing file again. "); } else { string convertedxpsdoc = string. Concat (Path.gettemppath (), "\ \", Guid.NewGuid (). ToString (), ". Xps"); XpsDocument xpsdocument =convertwordtoxps (worddocument, Convertedxpsdoc); if (xpsdocument = = null) { return; } documentviewword.document = xpsdocument.getfixeddocumentsequence (); } }
Go from http://code.msdn.microsoft.com/office/CSVSTOViewWordInWPF-db347436/