Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C#
Article

Create a PowerPoint presentation using a template

Rate me:
Please Sign up or sign in to vote.
4.35/5 (6 votes)
10 Nov 20062 min read 94.3K   5.9K   37   14
Create a PowerPoint presentation using a template using the Office 2007 OpenXML format.

Sample Image

Image 2

Introduction

In this article, I am trying to explain the PowerPoint 2007 file format and its structure by showing how to create a presentation using a template. This is a very useful example and can be used for various purposes. In this example, I am creating slides as per the user input for text and images. The class presentation in the code is designed in a way that anyone can automate the process of generating a presentation by feeding input from any source.

Background

Office 2007 has introduced a new file format called OpenXML. All the data/setting/metadata is stored in an XML file. First, quickly create a Word/Excel/PowerPoint file and save it. Now, close the application and rename the file with the extension .zip and open the file. You can see that all the .xml files are packed in there. Microsoft has released the September CTP for .NET Framework 3.0. It has an assembly called Windowbase.dll that has the namespace System.IO.Packaging that supports the Open Office 2007 package file to perform operations like adding/deleting parts and media. You can download it from here. I have used this namespace to work with this example. For further information about Office 2007 OpenXML file format, please read this article. You can get other resources from the online MSDN.

Using the code

In the code, I have written a class presentation that you can find in Presentation.cs. This class uses the System.IO.Packaging namespace. I have the template1.pptx file that you can find in the debug\bin\Template\ folder. I kept the file there just for ease of work. This program creates a copy of the file at the location input by the user (the program asks for the location on hitting the Create presentation button on the main form). Then, for each slide, it creates a copy of slide1.xml and gives it a name in sequence, like slide2.xml etc. Then, this program finds the node where it stores the text and replaces with the text that the user has keyed in, and then adds the image to the package and creates a relationship with the recently created slide(n).xml. It creates an entry of the created slide in presentation.xml and it also creates the relationship with slidelayout(n).xml. At the end, we save the package. The package class has a Save method to compress back the file, and that is it.

C#
//open the package
package = Package.Open(filename, 
                       FileMode.Open, 
                       FileAccess.ReadWrite);

Points of Interest

This can be your first example to learn about the PowerPoint file format. If you understand the file format very well, you can change this example to meet your needs.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
.Net System Designer working in USA with IT company since Jan-2005. I am having experience in Development of office application,Database application ,Java Application, and Banking Solutions.
I am having total 7.5 years of experience in IT. I have done various project for Wireless,Mobile tech.

Comments and Discussions

 
QuestionMultiple slide with different template? Pin
Trimantra Software Solution21-Dec-16 0:13
Trimantra Software Solution21-Dec-16 0:13 
QuestionIs it required to install ms office in server side Pin
febin sarafras16-Oct-14 20:35
febin sarafras16-Oct-14 20:35 
QuestionHow can i delete a slide using the same code style Pin
Omda24069024-Apr-14 8:34
Omda24069024-Apr-14 8:34 
SuggestionCorrection Pin
Member 918313522-Aug-12 19:01
Member 918313522-Aug-12 19:01 
QuestionDo other image files work? Pin
Member 79783461-Apr-12 6:00
Member 79783461-Apr-12 6:00 
QuestionIs it possible in server side? Pin
winsrividhya11-May-10 4:07
winsrividhya11-May-10 4:07 
AnswerRe: Is it possible in server side? Pin
yusufelik11-Jan-12 3:26
yusufelik11-Jan-12 3:26 
QuestionPowerpoint in serverside Pin
ummu 27-Mar-10 19:45
ummu 27-Mar-10 19:45 
hi its very useful article,
but is it possible server side?
if possible,how to do?
GeneralIs it posible to extract images,cliparts and vidoes from PPTX file Pin
bloremohan2-Dec-08 9:03
bloremohan2-Dec-08 9:03 
GeneralRe: Is it posible to extract images,cliparts and vidoes from PPTX file Pin
yusufelik11-Jan-12 3:25
yusufelik11-Jan-12 3:25 
GeneralDynamic XML template-based PowerPointCreator in C# Pin
Elmue29-Aug-08 13:05
Elmue29-Aug-08 13:05 
GeneralDoesn't work Pin
JSBBS29-Jun-08 18:57
JSBBS29-Jun-08 18:57 
GeneralDifferent Image and media at same presentation .. Pin
MrHoSo14-Mar-08 6:57
MrHoSo14-Mar-08 6:57 

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.