Link to home
Start Free TrialLog in
Avatar of rito1
rito1

asked on

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

Hi All,

I have the following simplified code which is part of my asp.net 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.

Thanks,

Rit
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.FeedData(_slideTemplate.GetStream(FileMode.Open));
        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
        newSlidePart.Slide.Save();

        //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) };
        slideIdList.Append(newSlideId);

    }
    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

test-template.pptx
ASKER CERTIFIED SOLUTION
Avatar of regevha
regevha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial