我最近在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文档:
我的问题是如何进一步修改我的代码以显示PowerPoint不仅仅是如图所示的一系列可滚动幻灯片,而是作为一个真正的幻灯片放映?我想进一步更新,以使用户能够在每次单击鼠标时导航到以下幻灯片 - 就像'proper'演示文稿一样 . 我的问题是我不熟悉XpsDocument Apis的用法 - 我不喜欢使用这些来实现我想要的,或者是在转换为Xps格式的表示变量的设置属性中 .
1 回答
我设法解决了我感兴趣的特定问题 .
有关详细说明,请参阅此博客文章:
Controlling DocumentViewer methods and properties using MVVM
该解决方案解决了通过调用
DocumentViewer
方法的相关组合,能够启用单个PowerPoint幻灯片(转换为xps文件格式)以占用可用窗口空间的整个问题的问题 .在按下屏幕按钮以调用RelayCommand时,观察到MainWindowViewModel.cs类中的以下DocumentViewer API调用组合起作用:
并获得
DocumentViewer
实例本身?我还需要更新MainWindow.xaml.cs以使其返回DocumentViewer对象的实例:namespace DocumentView {public partial class MainWindow {private static DocumentViewer _docViewer;
}}
DocumentViewPowerPoint
是MainWindow.xaml中DocumentViewer的名称: