title: Export Word to XPS c#
body: <p>As part of an application i need a window  where the users can get a preview of office documents in a way that is clear to them that it is read-only. Also, the window is is only part of the screen so opening a full version of an Office app with ribbon an all takes a little to much space. The users will be browsing through a lot of documents so it has to be fast, speed is the key.</p>
  
  <p>I found out this, with a lot of help from google and this site and others:
  it shows the document in read only; check.
  It send the xpsfil back to a windows form to be viewed without taking up space on the screen than it has to; check.
  Speed; ohh no. It is so slow.
  Does anybod know of a better way of viewing office documents fast and in a way that is clearly read-only to the users?
  Help much appriciated</p>
  
  <pre><code>    private readonly string TEMP = Environment.ExpandEnvironmentVariables("%tmp%");
      private  object nullObject = Type.Missing;
  
  
      private string ConvertWordtoXps(string wordDocName)
      {
          Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
          wordApp.Documents.Open(wordDocName, ConfirmConversions: false, ReadOnly: true);
          string xpsFileName = String.Concat(TEMP, "\\", Path.GetFileNameWithoutExtension(wordDocName), ".xps");
  
          try
          {
              //wordApp.ActiveDocument.ExportAsFixedFormat(xpsFileName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentContent, false, true, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, false, nullObject);
              wordApp.ActiveDocument.SaveAs2(xpsFileName, FileFormat: WdSaveFormat.wdFormatXPS);
              return xpsFileName;
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }
          finally
          {
              ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(SaveChanges: false, OriginalFormat: nullObject, RouteDocument: nullObject);
          }
          return null;
  
      }
  </code></pre>