首页 文章

将XpsDocument PowerPoint幻灯片放入WPF DocumentViewer中

提问于
浏览
0

我最近在WPF中嵌入了一个PowerPoint文件作为XpsDocument .

这是一个简单的WPF应用程序,我在其中将DocumentViewer属性嵌入到我的MainWindow.xaml网格中:

<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>

要创建绑定到“DocumentviewPowerPoint”的文档,我将已打开的PowerPoint文件转换为Xps格式,并将此变量绑定到前面提到的XAML属性:

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);
        }
    }
}

这一切在运行程序时运行良好,显示嵌入到WPF中的Xps文档:

enter image description here

我的问题是如何进一步修改我的代码以显示PowerPoint不仅仅是如图所示的一系列可滚动幻灯片,而是作为一个真正的幻灯片放映?我想进一步更新,以使用户能够在每次单击鼠标时导航到以下幻灯片 - 就像'proper'演示文稿一样 . 我的问题是我不熟悉XpsDocument Apis的用法 - 我不喜欢使用这些来实现我想要的,或者是在转换为Xps格式的表示变量的设置属性中 .

1 回答

  • 0

    我设法解决了我感兴趣的特定问题 .

    有关详细说明,请参阅此博客文章:

    Controlling DocumentViewer methods and properties using MVVM

    该解决方案解决了通过调用 DocumentViewer 方法的相关组合,能够启用单个PowerPoint幻灯片(转换为xps文件格式)以占用可用窗口空间的整个问题的问题 .

    在按下屏幕按钮以调用RelayCommand时,观察到MainWindowViewModel.cs类中的以下DocumentViewer API调用组合起作用:

    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);
                }));
         }
      }
    

    并获得 DocumentViewer 实例本身?我还需要更新MainWindow.xaml.cs以使其返回DocumentViewer对象的实例:

    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;
      }
    

    }}

    DocumentViewPowerPoint 是MainWindow.xaml中DocumentViewer的名称:

    <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>
    

相关问题