Change text in a textbox in Powerpoint slide
Asked Answered
A

1

6

I have a Powerpoint presentation which contains 3 slides. Each slide has a Textbox which is a place holder. I would like to replace the Textbox contents on one slide.

I need to know how to do this using C# and OpenXML

Thanks a ton

Airlift answered 4/5, 2012 at 5:7 Comment(1)
Did you get this working? Are you able to post the solution please?Bellabelladonna
E
3

Do this for each slide, you want to change:

ODP.ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
        foreach (ODP.Shape shape in tree.Elements<ODP.Shape>())
        {
            // Run through all the paragraphs in the document
            foreach (ODD.Paragraph paragraph in shape.Descendants().OfType<ODD.Paragraph>())
            {
                foreach (ODD.Run run in paragraph.Elements<ODD.Run>())
                {
                    if (run.Text.InnerText.Contains("PLACEHOLDER"))
                    {
                        run.Text = new ODD.Text("Your new text");
                    }
                }
            }
        }

Keep in mind, that if your template's placeholders contain spaces, this may create two individual run elements. So instead of one run element with run.Text of "Place holder", you might get one run with run.text of "Place" and another one with run.Text "holder".

Eristic answered 9/12, 2013 at 15:17 Comment(3)
i have a scenario , that i just have paragraph ,no child elements inside it . i trying adding run and text element inside it still NOT WORKING . Any help is appreciatedSclerite
When I do this, and save the file, the changed text is not there. If I inspect run.Text in the debugger it shows the updated text.Darnall
You should really define your usings. I found through trial-and-error that ODP.Shape means DocumentFormat.OpenXml.Presentation.Shape, not DocumentFormat.OpenXml.Drawing.Shape.Cashew

© 2022 - 2024 — McMap. All rights reserved.