[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-14
1
Medium Priority
?
3,262 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:rito1
1 Comment
 
LVL 6

Accepted Solution

by:
regevha earned 2000 total points
ID: 36970989
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 - http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/09/06/7429.aspx
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video teaches viewers how to fit pictures into slides, crop and remove backgrounds, and alter photos to look more professional.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Suggested Courses

830 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