Introduction
I needed a simple way to generate a PowerPoint presentation, consisting of slides with a title and an image. Most approaches use the Microsoft Office interop for this, but unfortunately, the application couldn't rely on Microsoft Office to be installed. However, you can download and use the OpenXML SDK 2.5 for Microsoft Office to create a PowerPoint for you, and I've done just that.
Background
This tip basically provides you with a simple library to generate PowerPoint slides consisting of a slide title and image. I cannot take credit for most of the code, however, as I've taken it from existing examples. Specifically, I've reused the code in the below articles, and re-organized them into a library for easy use:
Using the Code
In order to use the code, either include the PowerPointGenerator
project, or add a reference to:
- DocumentFormat.OpenXml.dll
- PowerPointGenerator.dll
Next, you can use the code as follows:
var powerPointGenerator = new PowerPointFactory
(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JessicaAlba.pptx"));
var imageFilePaths = Directory.GetFiles(@"..\..\..\Alba", "*.jpg").ToList();
powerPointGenerator.CreateTitleAndImageSlides(imageFilePaths);
Here, I assume that the generated PowerPoint is saved to the output folder as JessicaAlba.pptx, and it consists of one slide per JPG image in the Alba folder (part of the solution). As I do not specify the titles explicitly, the filename is used as the slide title, but you can also supply a list of titles (one for each slide).
powerPointGenerator.CreateTitleAndImageSlides
(imageFilePaths, new List<string> { "title 1", "title 2" });
Points of Interest
The OpenXML SDK also contains a tool, OpenXmlSdkTool (in c:\Program Files (x86)\Open XML SDK\V2.5\tool\OpenXmlSdkTool.exe), which converts an existing PowerPoint presentation to a class that can recreate this presentation for you (including the styles, masters, etc.). Pretty amazing, and I've used it to generate the Template.cs class in this project.
History
- 2014-06-04 Article written and first version of the code
- 2014-06-06 Source code couldn't be downloaded - fixed