Create PowerPoint From Template and Replacing text in Placeholder - Not Working

Posted on 2011-10-14
Last Modified: 2012-05-12
Hi All,

I have the following simplified code which is part of my website, which does the following:

1. On the click event of btnCreate to copies my template pptx file (text_template.pptx) and creates ouput.pptx.

2. Open presentation and get the initial slide

3. Add new slide via the method AddNewsSlide()

4. Then I TRY and replace the text within my placeholder called #Title# but its being ignored at this point.

I end up with a new PowerPoint presentation with 2 slides but my placeholder hasn't changed.

Can anyone see what I could be doing wrong? I have also attached test_template.pptx too.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;

public partial class Default2 : System.Web.UI.Page
    private void AddNewSlide(PresentationPart parent, SlidePart _slideTemplate)
        var newSlidePart = parent.AddNewPart<SlidePart>("newSlide1");

        //copy the contents of the template slide to the new slide and attach the appropriate layout
        newSlidePart.AddPart(_slideTemplate.SlideLayoutPart, _slideTemplate.GetIdOfPart(_slideTemplate.SlideLayoutPart));
        //Alter the placeholder text in new slide
        SetPlaceholder(newSlidePart, "#Title#", "TESTING123");

        //save the changes to the slide

        //need to assign an id to the new slide and add it to the slideIdList
        //first figure out the largest existing id
        DocumentFormat.OpenXml.Presentation.SlideIdList slideIdList = parent.Presentation.SlideIdList;
        uint maxSlideId = 1;

        foreach (DocumentFormat.OpenXml.Presentation.SlideId slideId in slideIdList.ChildElements)
            if (slideId.Id > maxSlideId) maxSlideId = slideId.Id;

        //assign an id and add the new slide at the end of the list
        DocumentFormat.OpenXml.Presentation.SlideId newSlideId = new DocumentFormat.OpenXml.Presentation.SlideId { Id = ++maxSlideId, RelationshipId = parent.GetIdOfPart(newSlidePart) };

    private void SetPlaceholder(SlidePart slidePart, string placeholder, string value)
        List<Text> textListExif1 = slidePart.Slide.Descendants<Text>().Where(t => t.Text.Equals(placeholder)).ToList();
        foreach (Text text in textListExif1)
            text.Text = value;

    protected void btnCreate_Click(object sender, EventArgs e)
        string slideName = @"C:\websiteroot\powerpoint_creation\output\output.pptx";
        //copy the document which contains the style definition we want use in generated table
        File.Copy(@"C:\websiteroot\powerpoint_creation\template\test_template.pptx", slideName, true);

        using (PresentationDocument presentationDocument = PresentationDocument.Open(slideName, true))
            //Get the first slide from presentation
            SlidePart intitalSlide = presentationDocument.PresentationPart.SlideParts.First();

            AddNewSlide(presentationDocument.PresentationPart, intitalSlide);


Open in new window

Question by:rito1
    1 Comment
    LVL 6

    Accepted Solution

    I do not know what you did wrong. I found the following article in the OpenXml Developer blog and I hope the information there will help you solve the problem -

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction In all recent versions of PowerPoint it is possible to trigger animations. This means the animation takes place when a certain shape is clicked. This allows you to run animation “on demand” and outwith the normal sequence of mouse cl…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    This video teaches viewers how to add simple and professional themes to their slides.
    The viewer will learn how to edit text. This includes Font, Spacing, Resizing, Color, and other special text options.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now