如何在不打开另一个窗口的情况下将 PowerPoint 演示文稿嵌入到 WPF 应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15725908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I embed a PowerPoint presentation into a WPF application without opening another window?
提问by Brandon Dusseau
Currently I have a WPF application in C#, but I'm finding it to be incredibly difficult to find any useful ways to embed a PowerPoint presentation into my window.
目前我在 C# 中有一个 WPF 应用程序,但我发现很难找到任何有用的方法将 PowerPoint 演示文稿嵌入到我的窗口中。
One solution I found here: Embedding a Powerpoint show into a C# application
我在这里找到的一个解决方案:Embedding a Powerpoint show into a C# application
This solution created the problem of having PowerPoint run in another window, but just display its UI within the WPF application. This meant that when the WPF window was focused, the PowerPoint presentation was not, and stopped playing. There was also the problem of PowerPoint crashing when the window was closed.
此解决方案产生了让 PowerPoint 在另一个窗口中运行的问题,但只是在 WPF 应用程序中显示其 UI。这意味着当 WPF 窗口聚焦时,PowerPoint 演示文稿没有聚焦并停止播放。还有关闭窗口时PowerPoint崩溃的问题。
Another solution I found was here: http://www.codeproject.com/Articles/118676/Embedding-PowerPoint-presentation-player-into-a-WP
我发现的另一个解决方案在这里:http: //www.codeproject.com/Articles/118676/Embedding-PowerPoint-presentation-player-into-a-WP
The solution was popular, but I found it difficult to work with. I don't know any Win32 programming, OR C++, so I found it extremely hard to modify. I managed to get it to stop displaying a second copy of the PowerPoint (an intended function in the original project), but I've not yet found a way to automatically open the PowerPoint presentation.
该解决方案很受欢迎,但我发现它很难使用。我不懂任何 Win32 编程或 C++,所以我发现修改起来非常困难。我设法让它停止显示 PowerPoint 的第二个副本(原始项目中的预期功能),但我还没有找到自动打开 PowerPoint 演示文稿的方法。
So what I need is a way to cleanly open the PowerPoint presentation automatically and in the background (I don't want the PowerPoint UI to be displayed at any point), and to allow it to run automatically (and not respond to input) while the application is running. It would be wonderful if I could keep it within C# and WPF, and not have to deal with Win32 and C++.
所以我需要的是一种在后台自动和干净地打开 PowerPoint 演示文稿的方法(我不希望在任何时候显示 PowerPoint UI),并允许它自动运行(并且不响应输入)同时应用程序正在运行。如果我能将它保留在 C# 和 WPF 中,而不必处理 Win32 和 C++,那就太好了。
Is this possible? At this point I'm really regretting this project simply because of the PowerPoint integration headaches.
这可能吗?在这一点上,我真的因为 PowerPoint 集成问题而对这个项目感到遗憾。
回答by Joulukuusi
You could convert your presentation to a video format on-the-fly:
您可以即时将演示文稿转换为视频格式:
// not tested as I don't have the Office 2010, but should work
private string GetVideoFromPpt(string filename)
{
var app = new PowerPoint.Application();
var presentation = app.Presentations.Open(filename, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
var wmvfile = Guid.NewGuid().ToString() + ".wmv";
var fullpath = Path.GetTempPath() + filename;
try
{
presentation.CreateVideo(wmvfile);
presentation.SaveCopyAs(fullpath, PowerPoint.PpSaveAsFileType.ppSaveAsWMV, MsoTriState.msoCTrue);
}
catch (COMException ex)
{
wmvfile = null;
}
finally
{
app.Quit();
}
return wmvfile;
}
And then you would play it with MediaElement:
然后你会玩它MediaElement:
<MediaElement Name="player" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
public void PlayPresentation(string filename)
{
var wmvfile = GetVideoFromPpt(filename);
player.Source = new Uri(wmvfile);
player.Play();
}
Don't forget to File.Delete(wmvfile)when you're done playing the video!
File.Delete(wmvfile)播放完视频后不要忘记!
回答by mbreske
Launching the presentation via the command line with the /s flag will play the slideshow without launching the splash screen.
通过带有 /s 标志的命令行启动演示文稿将在不启动启动屏幕的情况下播放幻灯片。
powerpnt.exe /s c:\path\to\your\presentation.pptx
I would try that in concert with some of the WPF embed solutions you've mentioned or taking a look at this approach.
我会尝试结合您提到的一些 WPF 嵌入解决方案或查看此方法。
I know little about WPF, so hopefully someone can provide a better answer incorporating all these pieces.
我对 WPF 知之甚少,所以希望有人可以提供包含所有这些部分的更好答案。
回答by Mike Fuchs
I do not really like it, and I'm not sure this applies to your situation. You would need to have access to the presentation, so much's for sure. It israther easy and leightweight however.
我不太喜欢它,我不确定这是否适用于您的情况。您需要访问演示文稿,这是肯定的。这是相当容易,但是leightweight。
My basic idea was to somehow embed the powerpoint presentation in html and just use the webbrowser control to display it. There seems to be a number of waysto do that.
我的基本想法是以某种方式将 powerpoint 演示文稿嵌入到 html 中,然后仅使用 webbrowser 控件来显示它。似乎有很多方法可以做到这一点。
I decided to try and save the presentation directly as html, which turned out to be possible (at least for PP2010), albeit the layout could be nicer. Another approach (e.g. the google docs) might produce something nicer. I took the following from this link.
我决定尝试将演示文稿直接保存为 html,结果证明这是可能的(至少对于 PP2010),尽管布局可能更好。另一种方法(例如谷歌文档)可能会产生更好的东西。我从这个链接中获取了以下内容。
- In PowerPoint 2010, open the presentation that you want to export to HTML
- Press Alt+F11.
- Press Ctrl+G.
In the Immediate pane, type the following, and then press Enter:
ActivePresentation.SaveAs "<Drive>:\users\<username>\desktop\<filename>.htm", ppSaveAsHTML, msoFalseNoteTo save by using the Single File Web Page (*.mht; *.mhtml) file format, replace htm at the end of the file name with mht, and replace ppSaveAsHTML with ppSaveAsWebArchive.
- 在 PowerPoint 2010 中,打开要导出为 HTML 的演示文稿
- 按 Alt+F11。
- 按 Ctrl+G。
在“立即”窗格中,键入以下内容,然后按 Enter:
ActivePresentation.SaveAs "<Drive>:\users\<username>\desktop\<filename>.htm", ppSaveAsHTML, msoFalse注意要使用单个文件网页保存(*的.mht; * .mhtml)文件格式,在与文件名末尾更换HTM的MHT,并将其替换ppSaveAsHTML ppSaveAsWebArchive。
If you export it in htm, you get a lot of additional files, in mht it's just a single file so that suited me better. I'm pretty sure it would also be possible to automate this step in code if you need to go generic with your solution.
如果你在 htm 中导出它,你会得到很多额外的文件,在 mht 中它只是一个文件,所以更适合我。如果您需要通用的解决方案,我很确定也可以在代码中自动执行此步骤。
To display the html file in a webbrowser control is the easy part, I uploaded it to my dropbox for convenience and just set the source (I'll leave it up there for a few days if you want to have a look at it directly).
在 webbrowser 控件中显示 html 文件是最简单的部分,为了方便起见,我将它上传到我的 Dropbox 并设置源(如果你想直接看它,我会把它放在那里几天) .
As for starting the slideshow immediately, I'll have to look into it some more.
至于立即开始幻灯片放映,我得再研究一下。
<WebBrowser x:Name="webbrowser" Source="https://dl.dropbox.com/u/27614341/test.mht"/>


回答by Haritha
There is a WPF control called DocumentViewer.
有一个名为DocumentViewer.
- First the pptx should be converted to .xps file format.
- Then Bind it to the
Documentproperty of the DocumentViewer.
- 首先应将 pptx 转换为 .xps 文件格式。
- 然后将其绑定到
DocumentDocumentViewer的属性。
Hereis the link to convert office documents (including pptx) to XPS in C sharp.
这是在pptxCSharp中将Office文档(包括)转换为XPS的链接。
XAML
XAML
<DocumentViewer Name="myDocumentViewer" Margin="0,0,0,59">
</DocumentViewer>
Bind to the Documentproperty of the control (Note that ConvertPptxDocToXPSDocis the method to convert pptx to xps)
绑定到Document控件的属性(注意ConvertPptxDocToXPSDoc是pptx转xps的方法)
myDocumentViewer.Document = this.ConvertPptxDocToXPSDoc(this.FileName, this.newXPSDocumentName).GetFixedDocumentSequence();

