We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

PowerPoint.Presentation C# Is there any way to reference existing text box shapes on a slide other than guessing indexes

We have a large set of slide layouts that are available when we create a new PowerPoint deck, for Tile Slides, dividers, Title and Pictures, etc. 

I am generating a deck that will use 4 or 5 of these different layouts. What I am trying to figure out is what is the best way to reference these pre-existing text boxes that don't have any real text in them yet.

So far, the best I could come up with was start with a deck that had slide1 as layout1, slide2 as layout2, etc. If I want to add new slide with layout 2, I would just clone slide2, manipulate the clone, and then add it to the presentation. The reason I am doing this is because the only way that I have found to be able to edit a placeholder text box is if it already has filler text in it. Is there anyway to know which text box I am editing without having to guess at indexes.

I was playing with this code just to see i I could correlate at least the first shape name with some text. The results seem to be all over the place. 

foreach (var slide in presentation.Slides.ToList())

            {

                Console.WriteLine($"Slide number is {slide.SlideNumber}");

                var newSlide = slide.Clone();

                var shape = (IShape)newSlide.Shapes[0];

                shape.TextBody.Paragraphs[0].Text = $"MadeByCode {shape.ShapeName}";

                Console.WriteLine($"Shape Name {shape.ShapeName} Shape Text {shape.TextBody.Paragraphs[0].Text}");

                presentation.Slides.Add(newSlide);

            }


3 Replies 1 reply marked as answer

AN Anto Nihil Sahaya Raj Syncfusion Team April 27, 2023 04:47 PM UTC

Hi Andy,

From the given details, we have found that your end requirement is to access the existing text box shapes on a slide without guessing indexes and modify the text box’s content. To achieve your requirement, we suggest you to iterate all the shapes in a cloned slide and identify the particular shape in a cloned slide using the ShapeName API. Then modify the contents in shape. Please refer below code snippet to achieve your requirement.


using (IPresentation pptxDoc = Presentation.Open("Template.pptx"))

{

    foreach (var slide in pptxDoc.Slides.ToList())

    {

        // Check the particular slide.

        if (slide.SlideNumber == 1)

        {

           //Clone the existing slide

           var newSlide = slide.Clone();

           //Iterate each shape of the slide.

           foreach (IShape shape in newSlide.Shapes)

           {

              //Identify the particular shape using its name.

              if (shape.ShapeName == "TextBox 3")

              {

                 //Iterate through each paragraph of the shape.

                 foreach (IParagraph paragraph in shape.TextBody.Paragraphs)

                 {

                    //Iterate through each textpart of the paragraph.

                    foreach (ITextPart textPart in paragraph.TextParts)

                    {

                       //Replace the text.

                       if (textPart.Text == "Presentation")

                          textPart.Text = "PowerPoint";

                     }

 

                 }

              }

           }

           pptxDoc.Slides.Add(newSlide);

        }

    }

    pptxDoc.Save("Result.pptx");

}


Please refer below link to know more about working with shapes,
https://help.syncfusion.com/file-formats/presentation/working-with-shapes

Regards,
Anto Nihil S


Marked as answer

AS Andy Schneider April 27, 2023 05:43 PM UTC

Thanks for the help. In addition to this, it is also helpful to go in an name the shapes ahead of time in the deck. You can do this using the "Selection Pane"



AN Anto Nihil Sahaya Raj Syncfusion Team May 1, 2023 01:54 PM UTC

You're welcome, Andy.

Thanks for the suggestion also. Now we are closing this forum.


Loader.
Up arrow icon