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

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 -

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.