Embedding XpsDocument PowerPoint slide shows into a WPF DocumentViewer

981 views Asked by At

I've recently had a go at embedding a PowerPoint file as an XpsDocument in WPF.

It is a simple WPF application in which I embed a DocumentViewer property into my MainWindow.xaml grid:

<Window x:Class="PowerPoint2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PowerPoint2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <DocumentViewer
        Name="DocumentviewPowerPoint"
        VerticalAlignment="Top"
        HorizontalAlignment="Left" />
</Grid>

To create the document bound to "DocumentviewPowerPoint" I convert the PowerPoint file that has been opened into Xps format and bind this variable to the XAML property mentioned earlier:

using System;
using System.IO;
using System.Windows;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using Application = Microsoft.Office.Interop.PowerPoint.Application;

namespace PowerPoint2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            const string powerPointFile = @"c:\temp\ppt.pptx";
            var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".pptx";
            var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);

            DocumentviewPowerPoint.Document = xpsDocument.GetFixedDocumentSequence();            
        }

        private static XpsDocument ConvertPowerPointToXps(string pptFilename, string xpsFilename)
        {
            var pptApp = new Application();

            var presentation = pptApp.Presentations.Open(pptFilename, MsoTriState.msoTrue, MsoTriState.msoFalse,
            MsoTriState.msoFalse);

            try
            {           
                presentation.ExportAsFixedFormat(xpsFilename, PpFixedFormatType.ppFixedFormatTypeXPS);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to export to XPS format: " + ex);
            }
            finally
            {
                presentation.Close();
                pptApp.Quit();
            }

            return new XpsDocument(xpsFilename, FileAccess.Read);
        }
    }
}

This all works well enough when running the program, showing the Xps document embedded into the WPF:

enter image description here

My question is how can I further modify my code in order to display the PowerPoint not just as a series of scrollable slides as shown, but as an actual slide show? I would like to make further updates to enable the user to navigate to the following slide on each mouse click - like a 'proper' presentation. My problem is that I am unfamiliar with the usage of the XpsDocument Apis - I don't know if it's a case of using these to achieve what I want or is it in the settings properties of the presentation variable that gets converted to the Xps format.

1

There are 1 answers

0
AndyUK On

I managed to solve the particular problem I was interested in.

Please refer to this blog posting for a detailed explanation:

Controlling DocumentViewer methods and properties using MVVM

The solution addresses the problem of being able to enable individual PowerPoint slides (converted to xps file format) to occupy the WHOLE of the available windows space by invoking the relevant combination of DocumentViewer methods.

On pressing the screen button to invoke the RelayCommand, the following combination of DocumentViewer API calls in the MainWindowViewModel.cs class was observed to work:

public ICommand Command
  {
     get
     {
        return _command ?? (_command = new RelayCommand(
            x =>
            {
               DocumentViewer = MainWindow.GetInstance();

               const string powerPointFile = @"c:\temp\ppt.pptx";
               var xpsFile = Path.GetTempPath() + Guid.NewGuid() + ".xps";
               var xpsDocument = ConvertPowerPointToXps(powerPointFile, xpsFile);

               FixedFixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
               DocumentViewer.Document = FixedFixedDocumentSequence;

               DocumentViewer.GoToPage(1);
               DocumentViewer.FitToMaxPagesAcross(1);
               WindowState = WindowState.Maximized;
               DocumentViewer.FitToMaxPagesAcross(1);
            }));
     }
  }

And to obtain the DocumentViewer instance itself? I also need to update the MainWindow.xaml.cs to get it to return the instance of the DocumentViewer object:

using System.Windows.Controls;

namespace DocumentView { public partial class MainWindow { private static DocumentViewer _docViewer;

  public MainWindow()
  {
     InitializeComponent();
     _docViewer = DocumentViewPowerPoint;
  }

  public static DocumentViewer GetInstance()
  {
     return _docViewer;
  }

} }

Where DocumentViewPowerPoint is the name given to the DocumentViewer in the MainWindow.xaml:

<Window x:Class="DocumentView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DocumentView"
    mc:Ignorable="d"
    WindowState="{Binding WindowState, Mode=TwoWay}"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>

    <DocumentViewer 
        Grid.Row="0"
        Document="{Binding FixedFixedDocumentSequence}"
        Name="DocumentViewPowerPoint"
        VerticalAlignment="Top"
        HorizontalAlignment="Left" />

    <Button
        Grid.Row="1"
        Command="{Binding Command}"
        Width="70" Height="30" Content="Press" />
</Grid>