0

私は最近、PowerPoint ファイルを XpsDocument として WPF に埋め込んでみました。

これは、DocumentViewer プロパティを MainWindow.xaml グリッドに埋め込む単純な WPF アプリケーションです。

<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 ドキュメントが表示されます。

ここに画像の説明を入力

私の質問は、PowerPoint を一連のスクロール可能なスライドとして表示するだけでなく、実際のスライド ショーとして表示するためにコードをさらに変更するにはどうすればよいですか? 「適切な」プレゼンテーションのように、ユーザーがマウスをクリックするたびに次のスライドに移動できるように、さらに更新したいと思います。私の問題は、XpsDocument Api の使用法に慣れていないことです。これらを使用して目的を達成する場合なのか、Xps 形式に変換されるプレゼンテーション変数の設定プロパティにあるのかわかりません。 .

4

1 に答える 1

0

興味を持っていた特定の問題を解決することができました。

詳細な説明については、このブログ投稿を参照してください。

MVVM を使用して DocumentViewer のメソッドとプロパティを制御する

このソリューションは、個々の PowerPoint スライド (xps ファイル形式に変換されたもの) が、関連するメソッドの組み合わせを呼び出すことによって、使用可能なウィンドウ スペース全体を占有できるようにするという問題に対処しますDocumentViewer

画面ボタンを押して 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インスタンス自体を取得するには?また、DocumentViewer オブジェクトのインスタンスを返すように MainWindow.xaml.cs を更新する必要があります。

using System.Windows.Controls;

名前空間 DocumentView { public 部分クラス MainWindow { private static DocumentViewer _docViewer;

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

  public static DocumentViewer GetInstance()
  {
     return _docViewer;
  }

} }

MainWindow.xamlDocumentViewPowerPointで 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>    

于 2017-01-10T10:26:20.823 に答える