Click here to Skip to main content
15,887,596 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Create PowerPoint presentation using PowerPoint template

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Apr 2012CPOL 32.1K   4   2
Convert *.potx file to *.pptx file.

Introduction

The article tells how to create PowerPoint presentation using PowerPoint template and Office OpenXML SDK 2.0. I assume the users have basic knowledge about Office OpenXML. 

Background  

To do so, first we need an existing PowerPoint template (*.potx file).  

Using the code 

  1. Create a simple .potx file.
  2. Open Microsoft PowerPoint Presentation. The left panel contains a default slide, delete it. On the File menu, click Save As. In the File name box, type a name for your template, and then, in the Save as type box, select PowerPoint Template(*.potx). Let’s name this template as SimplePresentationTemplate.potx.

  3. Copy this template into a MemoryStream.
  4. C#
    MemoryStream templateStream = null;             
    using (Stream stream = File.Open("SimplePresentationTemplate.potx", FileMode.Open, FileAccess.Read)) 
    { 
        templateStream = new MemoryStream((int)stream.Length); 
        stream.Copy(templateStream);  
        templateStream.Position = 0L; 
    }

    stream.Copy(templateStream) copies the FileStream into the MemoryStream. This is an extension method. 

    C#
    public static void Copy(this Stream source, Stream target) 
    { 
        if (source != null) 
        { 
            MemoryStream mstream = source as MemoryStream; 
            if (mstream != null) mstream.WriteTo(target); 
            else 
            { 
                byte[] buffer = new byte[2048]; // this array length is sufficient for simple files 
                int length = buffer.Length, size; 
                while ((size = source.Read(buffer, 0, length)) != 0) 
                    target.Write(buffer, 0, size); 
            } 
        } 
    }
  5. Create presentation file using the stream.
  6. C#
    using (PresentationDocument prstDoc = PresentationDocument.Open(templateStream, true)) 
    { 
            prstDoc.ChangeDocumentType(DocumentFormat.OpenXml.PresentationDocumentType.Presentation);
            PresentationPart presPart = prstDoc.PresentationPart; 
            presPart.PresentationPropertiesPart.AddExternalRelationship(
              "http://schemas.openxmlformats.org/officeDocument/2006/" + 
              "relationships/attachedTemplate", 
              new Uri(("SimplePresentationTemplate.potx", UriKind.Absolute));
            presPart.Presentation.Save(); 
    }
  7. Save the memory stream into a file:
  8. C#
    File.WriteAllBytes("PresentationFile1.pptx", presentationStream.ToArray());

That’s it.. The presentation file PresentationFile1.pptx got created without altering the template file. 

License

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


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

Comments and Discussions

 
QuestionpresentationStream? Pin
martonx11-Dec-13 21:15
martonx11-Dec-13 21:15 
Questionunable to get the presProps.xml in my generated PPTX document Pin
catherine91-Dec-12 4:02
catherine91-Dec-12 4:02 

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.