Click here to Skip to main content
15,886,422 members
Articles / Programming Languages / C# 4.0
Tip/Trick

A Simple Library to Generate PowerPoint Presentations

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
5 Jun 2014CPOL2 min read 22.8K   1K   10   4
Generate a PowerPoint presentation, consisting of slides with a title and an image, without requiring Microsoft Office

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.

Image 1

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:

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

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRun Slideshow using OpenXML Pin
Wasim Hassan3-Jan-19 23:45
Wasim Hassan3-Jan-19 23:45 
QuestionCannot download your sample project Pin
fredatcodeproject6-Jun-14 5:52
professionalfredatcodeproject6-Jun-14 5:52 
AnswerRe: Cannot download your sample project Pin
Erik Vullings6-Jun-14 12:35
Erik Vullings6-Jun-14 12:35 
GeneralRe: Cannot download your sample project Pin
fredatcodeproject7-Jun-14 11:52
professionalfredatcodeproject7-Jun-14 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.