Manage PowerPoint Presentations Programmatically
By Steve C. Orr
After reviewing Aspose s hit products for generating Excel and Word documents, it seemed only natural they d develop an equivalent product for use with Microsoft PowerPoint presentations (see my reviews of Aspose.Excel and Aspose.Word).
Using Aspose.PowerPoint you can create, open, and manipulate PowerPoint presentations programmatically. You might not need to do such a task every day, but when the need arises there really aren t any alternatives (Aspose has really cornered the market here). From within a Windows application you might be able to get away with COM automation to manipulate PowerPoint presentations, but attempting COM automation with Office from within a Web application is fraught with peril. Even if you were to get it to work reliably, the performance would be terrible.
Aspose.PowerPoint was developed in pure C#. PowerPoint presentation files are manipulated directly without the need for Microsoft PowerPoint to be installed on the server. This efficient design is a boon for applications that need to be highly scalable. This product also works well with their separately priced Aspose.PDF for exporting presentations to Adobe Acrobat s standard PDF format.
I see this product as being great for standardizing presentations at an enterprise or departmental level. I envision ASP.NET applications that accept input from employees and generate basic PowerPoint presentations with all the standard company logos, fonts, etc. perfectly placed. I envision Web applications that accept uploaded presentations for storage and automated standardization.
Try Before You Buy
Start by downloading the free trial of Aspose.PowerPoint. After the standard installation routine, you ll find links to the API documentation, sample code, and getting started documentation. The API documentation is thorough, although I wish it contained some code samples to demonstrate how the various objects are used. At least sample code is provided separately. The console application samples are adequate, but I didn t have much luck getting their Web application samples to work after following the related set-up instructions. The getting started documentation is somewhat sparse and could use some sprucing up.
The demo is fully functional, so there will be no limits to your tinkering. The only nag notice is placed in the center of each generated presentation. Once you ve purchased Aspose.PowerPoint it takes only a couple lines of code to acknowledge that fact so the demo notices won t be plastered on top of the presentation:
Dim license As License = New License
After a moderate learning curve that involved examining the demo code and perusing the API documentation it became fairly obvious how the object model is structured. There are no controls to drop on forms but then again, such niceties aren t needed.
Create a new ASP.NET Web application and add a reference to Aspose.PowerPoint. For convenience, I suggest you add an Imports statement (a using statement in C#) to Aspose.PowerPoint. To open an existing PowerPoint presentation you ll need a few lines of boilerplate code to open the file and read it into a Presentation object:
Dim fs As System.IO.FileStream = _
New System.IO.FileStream("c:\mypath\myfile.ppt", _
Dim MyPres As Presentation = New Presentation(fs)
The real work can begin after you have the Presentation object instantiated; that is, operating on various slides and the content within. Initially, most of the properties residing within the slides collection appear to be read-only. However, with some probing you ll find that many of these properties are actually containers that have read/write access inside. The following code loops through every slide in the presentation and outputs their template formats to the current Web page:
Dim slides As Slides = MyPres.Slides
For i As Integer = 0 To slides.Count - 1
Response.Write(MyPres.Slides(i).Layout.ToString + "
Likewise, you can loop through the pictures contained within a presentation and work with them, too. The following code does just that, saving each embedded image out to individual files:
For i As Integer = 0 To MyPres.Pictures.Count - 1
Dim MyImage As Drawing.Image = MyPres.Pictures(i).Image
MyImage.Save("c:\mypath\picture" & i.ToString & ".jpg")
Adding a new picture to a slide requires only a few lines of code, such as this:
'Add new picture to the presentation
Dim picid As Integer = _
MyPres.Pictures.Add(New Picture(MyPres, _
'Assign new picture to the background of the first slide
MyPres.Slides(0).FollowMasterBackground = false
MyPres.Slides(0).Background.FillFormat.PictureId = picid
You can standardize common presentation properties to meet company standards. You can set the author, company name, lists of keywords, etc:
MyPresentation.Company = "Acme Inc."
MyPresentation.Comments = "http://www.acmecorp.net/"
MyPresentation.LastAuthor = "PowerPoint Automation Software"
MyPres.Keywords = "Acme, Automation, etc."
One way to go about using the custom class library exposed by Aspose.PowerPoint is to first ignore it. Instead, open PowerPoint and build a rough template of what you want your final document to look like. For example, set up some blank slides with the different types of formatting that you ll want to use when outputting the final content. You can set up static text, colors, tables, fonts, images, etc. in advance.
To create a new PowerPoint presentation at run time, simply open an empty (or templated) presentation file, insert the desired contents, and save it to the output stream.
To add a new slide at run time, simply clone the desired template slide (from the current presentation file or from a different presentation file) and fill in the dynamic content. Alternatively, you can call the Presentation.AddEmptySlide method.
Figure 1 shows a simple example that opens an existing PowerPoint presentation and copies one of the slides. The code goes on to insert some custom text into the slide before sending the finished product to the output stream, making it available to the end user.
' Open template & instantiate the presentation object
Dim fs As System.IO.FileStream = _
New System.IO.FileStream("c:\mypath\MyTemplate.ppt", _
Dim MyPres As Presentation = New Presentation(fs)
' Copy an existing slide
Dim MySlide As Slide = MyPres.CloneSlide(MyPres.Slides(0),0)
'insert custom text into the slide
Dim tf As TextFrame = MySlide.Shapes(3).TextFrame
tf.Paragraphs(0).Portions(0).Text = "My Custom Text"
' Prepare output stream to receive a PowerPoint Presentation
Me.Response.ContentType = "application/vnd.ms-powerpoint"
Dim st As System.IO.Stream = Me.Response.OutputStream
' Send the finished presentation to the page output stream
Figure 1: This code opens a PowerPoint presentation file, copies an existing slide, adds custom text to it, then renders the newly revised PowerPoint presentation file to the browser.
Good Value, Good Support
This reliable, solidly-built product has free online forum support that is checked regularly by Aspose staff. In addition, Aspose releases regular updates to the product that usually include free feature enhancements. So if you find a bug or a missing feature, let Aspose know through their online forums and don t be surprised to find your request quickly addressed in one of their frequent updates. Their online blog always contains up-to-date notes about recent product updates and the feature enhancements they contain.
Figure 2: You may choose to create one or more basic PowerPoint slides that Aspose.PowerPoint will use as a template. This makes it easy to fill in the blanks at run time with all the necessary fonts, colors, etc. chosen in advance.
Figure 3: Using Aspose.PowerPoint you can use code to insert text, pictures, tables, and more at run time.
There are two editions of Aspose.PowerPoint available: Professional and Enterprise (singe-developer and site licenses are available for both editions). Prices start at US$399 for the single-developer Professional license. The pricier Enterprise version supports advanced table-creation functionality and the ability to create five different kinds of slides from scratch. The price seems fair, especially considering this is really the only game in town for creating PowerPoint presentations in an ASP.NET Web application.
Steve C. Orr is an MCSD and a Microsoft MVP in ASP.NET. He s an independent consultant who develops software solutions for many leading companies in the Seattle area. When he s not busy designing software systems or writing about it, he can often be found loitering at local user groups and habitually lurking in the ASP.NET newsgroup. Find out more about him at http://SteveOrr.net or e-mail him at mailto:[email protected].
Price: Starts at US$399.