Path.GetFileNameWithoutExtension(dlg.FileName), ".xps") Path.GetDirectoryName(dlg.FileName), "\\", String newXPSDocumentName = String.Concat(System.IO.
Get the selected file name and display in a TextBox Display OpenFileDialog by calling ShowDialog method Set filter for file extension and default file extensionĭlg.Filter = "Word documents (.doc)|*.doc" OpenFileDialog dlg = new Microsoft.Win32. Private void BrowseButton_Click( object sender, RoutedEventArgs e) The Browse button click event handler code looks like this where we convert a Word document to an XPS document and view that in a DocumentViewer control. The application looks like Figure 3 where the Browse button is used to browse Word documents on your machine.
Let's create a WPF Application using Visual Studio 2010 and add a TextBox, Button, and a DocumentViewer. XpsDocument xpsDoc = new XpsDocument(xpsDocName, System.IO. Version 11 or previous versions do not have WdSaveFormat.wdFormatXPS optionĭoc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS)
You must ensure you have .Dll version 12. (wordDocName) ĭocument doc = wordApplication.ActiveDocument Create a WordApplication and add Document to it Private XpsDocument ConvertWordDocToXPSDoc( string wordDocName, string xpsDocName)
/ This method takes a Word document full path and new XPS document full path and name The ConvertWordDocToXPSDoc method takes a full path of a Word document file and a new full path of a XPS document and convert the doc file to an xps file. However, you must ensure you have version 12 of the assembly added to your project as I mentioned before. The SaveAs method of the Document class available in OfficeInterop allows us to save a Word document as an XPS document. Once you have added the reference to assemblies, you must import the following namespaces to your code behind. Make sure you select Version 12 for assemblies as you see in Figure 2, otherwise your conversion will fail. You may have multiple assemblies installed on your machine. Figure 1 shows ReachFramework in the Add Reference dialog. NET Framework, select ReachFramework and select the other assemblies from the list and click the OK button. To add a reference to these assemblies, right-click on the project name and select "Add Reference" in the Solution Explorer.
Note: You must also install 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS that you can download using the following link. The first assembly, ReachFramework.dll hosts the functionality for XPS documents and the other assemblies host the functionality of Office Interop and provide Office Tools support. This conversion is possible by using Office Interop and Office Tools frameworks for working with Office documents.Īdd Reference to XPS and Office Interop Assembliesīefore we do any actual work, we must add reference for the following assemblies: We can open a Word document if we can convert a Word document to an XPS document.
The WPF DocumentViewer control is used to display fixed documents such as an XML Paper Specification (XPS) document. WPF does not support the functionality to view Microsoft Word documents but there is a work-around for this problem.