Share via


Microsoft Office Document Viewer in WPF

Introduction

​Microsoft Office Documents like Word , Excel, PowerPoint are very difficult to View in WPF. I have seen a lot of posts related to viewing office files in forums. So to be straight forward there is no direct method to view office documents in WPF. Office files should be converted to a suitable format so that it could be viewed in WPF. Here are the steps to view an office Document in WPF

Steps

1.) Create a WPF 4.0 Application and Place a Document Viewer.

<Window x:Class="DocumentViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Office Files Viewer" Height="700" Width="1200">
    <Grid>
           <DocumentViewer   Name="documentViewer1" />
    </Grid>

2.) Add the Following Com Components in your References

  • Microsoft Word 14.0 Object Library
  • Microsoft Excel 14.0 Object Library
  • Microsoft PowerPoint 14.0 Object Library

Ensure that you are having version 12.0 or Above. Which means Microsoft word 2007 or higher versions.

3.) The next step is to convert the Office Documents into a suitable format which are XPS Documents. Before that add the following dll to your Reference

  • ReachFramework.dll 

4.) To convert the Office Documents to XPS Documents. Attach the OfficetoXps.cs class file to your project. You can download the class files and the whole project from my skydrive.

5.) Next step is to invoke the class and convert the office files to XPS and View in the Document Viewer. Using the code Below.

public void openFile()
{
System.Windows.Xps.Packaging.XpsDocument xpsDoc; /*Creating a Xps Documnet*/
Microsoft.Win32.OpenFileDialog dlg = new  Microsoft.Win32.OpenFileDialog(); /* Open File Dialog to open .docx Files */
 string xpsFilePath=String.Empty;
 // Set filter for file extension and default file extension 
 dlg.DefaultExt = ".txt";
 dlg.Filter = "Office Files(*.docx;*.doc;*.xlsx;*.xls;*.pptx;*.ppt)|*.docx;*.doc;*.xlsx;*.xls;*.pptx;*.ppt";
 
 // Display OpenFileDialog by calling ShowDialog method 
 Nullable<bool> result = dlg.ShowDialog();
 if (result == true)
 {
     string filename = dlg.FileName;
     xpsFilePath = System.Environment.CurrentDirectory + "\\" + dlg.SafeFileName + ".xps";
 }
 
 var convertResults = OfficeToXps.ConvertToXps(SourceUrl.Text, ref  xpsFilePath);
 switch (convertResults.Result)
 {
     case ConversionResult.OK:
        /* Creating a XPS Document in Read Write Mode*/
        xpsDoc = new  System.Windows.Xps.Packaging.XpsDocument(xpsFilePath, FileAccess.ReadWrite);
         /*Open in a Document Viewer*/
         documentViewer1.Document = xpsDoc.GetFixedDocumentSequence();
         break;
 
     case ConversionResult.InvalidFilePath:
         // Handle bad file path or file missing
         break;
     case ConversionResult.UnexpectedError:
         // This should only happen if the code is modified poorly
         break;
     case ConversionResult.ErrorUnableToInitializeOfficeApp:
         // Handle Office 2007 (Word | Excel | PowerPoint) not installed
         break;
     case ConversionResult.ErrorUnableToOpenOfficeFile:
         // Handle source file being locked or invalid permissions
         break;
     case ConversionResult.ErrorUnableToAccessOfficeInterop:
         // Handle Office 2007 (Word | Excel | PowerPoint) not installed
         break;
     case ConversionResult.ErrorUnableToExportToXps:
         // Handle Microsoft Save As PDF or XPS Add-In missing for 2007
         break;
 }
}

Download the Project from My Skydrive:
https://skydrive.live.com/?cid=841ACEE3077E04E5&id=841ACEE3077E04E5%21138