Friday, March 19, 2010

Viewing Powerpoint documents from Silverlight 3.0

I recently had a request to create a link to a User Guide for one of our products. "Great" I said, expecting an HTML page, or some text I could put into a Silverlight page.

What I got was a 10 page Powerpoint presentation with graphics. Silverlight 3 can't natively show a PPT file, so how could I do this?

First, I decided to create a "User Guide" hyperlink that would bring me out of Silverlight, and into an aspx page, here is the click event:
Uri SourceUri = new Uri(HtmlPage.Document.DocumentUri, Application.Current.Host.Source.ToString().Substring(0, Application.Current.Host.Source.ToString().IndexOf("ClientBin") - 1) + "/MSEUserGuide.aspx");
      HtmlPage.Window.Navigate(SourceUri, "_blank"); 
Next, I would use the ASP.net AJAX SlideShow control, Joe Stagner has a nice video tutorial on using the control that I found helpful.

This required a webservice with a list of images, but how to get the images from a PPT file?  Easy.  Open up the PPT file and choose "Save As" with your favourite image type.  I chose .gif and voila - 10 images nicely numbered from 1 to 10.  I then created a webservice to read in the images:
[WebMethod]
    public AjaxControlToolkit.Slide[] GetSlides()
    {
      List slides = new List();

      for (int i = 1; i <= 10; i++)
      {
        string fileName = string.Format("PowerPoint/Slide{0}.GIF", i.ToString());
        slides.Add(new AjaxControlToolkit.Slide(fileName, string.Format("Slide {0}", i.ToString()), string.Format("Slide {0}", i.ToString())));
      }

      return slides.ToArray();
    }
  }
Finally, here is the XAML code from my MSEUserGuide.aspx page (this is the newly created page I navigate to from the click even):
  


That's it! Seems like a complex way to show a simple PPT, but it was pretty quick to create and now you don't have to deal with the document any more which in my opinion is an advantage.

Alternatives to this would be to purchase a 3rd Party product to display the native PPT, or to wait for Silverlight 4 which I believe simplifies this.

No comments: