How can I embed a PowerPoint presentation into a W

2020-02-09 11:24发布

问题:

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.

One solution I found here: 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.

Another solution I found was here: 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.

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

Is this possible? At this point I'm really regretting this project simply because of the PowerPoint integration headaches.

回答1:

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



回答2:

Launching the presentation via the command line with the /s flag will play the slideshow without launching the splash screen.

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.

I know little about WPF, so hopefully someone can provide a better answer incorporating all these pieces.



回答3:

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 is rather easy and leightweight however.

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 ways to do that.

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.

  • 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, msoFalse

    Note To 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.

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.

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

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



回答4:

There is a WPF control called DocumentViewer.

  1. First the pptx should be converted to .xps file format.
  2. Then Bind it to the Document property of the DocumentViewer.

Here is the link to convert office documents (including pptx) to XPS in C sharp.

XAML

<DocumentViewer Name="myDocumentViewer" Margin="0,0,0,59">

</DocumentViewer>

Bind to the Document property of the control (Note that ConvertPptxDocToXPSDoc is the method to convert pptx to xps)

 myDocumentViewer.Document = this.ConvertPptxDocToXPSDoc(this.FileName, this.newXPSDocumentName).GetFixedDocumentSequence();