How to Read and Modify Text by Slide Index in a Presentation in C#?

Hi,

I have found the following code in documentation which reads text from the complete PPT file (all slides). However, I want to read each piece of text from a specific slide using index, i.e. Slides[0], and modify that text (set text) programmatically. Would you please guide me how to do that? Thanks.

C# .NET:

//Instatiate Presentation class that represents a PPTX file
Presentation pptxPresentation = new Presentation("demo.pptx");

//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true); 

//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
{
    //Loop through paragraphs in current ITextFrame
    foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
    {
        //Loop through portions in the current IParagraph
        foreach (IPortion port in para.Portions)
        {
            //Display text in the current portion
            Console.WriteLine(port.Text); 
        }
    }
}

@uax99

To read and modify text from a specific slide in a PowerPoint presentation using Aspose.Slides, you can follow these steps. Below is a C# example that demonstrates how to access a specific slide by its index, read the text, and modify it.

C# Example

// Instantiate Presentation class that represents a PPTX file
Presentation pptxPresentation = new Presentation("demo.pptx");

// Access the specific slide by index (e.g., first slide)
ISlide slide = pptxPresentation.Slides[0];

// Iterate through shapes in the slide
foreach (IShape shape in slide.Shapes)
{
    // Check if the shape has a text frame
    if (shape is IAutoShape autoShape && autoShape.TextFrame != null)
    {
        // Display the current text
        Console.WriteLine(autoShape.TextFrame.Text);

        // Modify the text
        autoShape.TextFrame.Text = "This is the modified text.";
    }
}

// Save the modified presentation
pptxPresentation.Save("modified_demo.pptx", SaveFormat.Pptx);

Explanation

  1. Instantiate the Presentation: Load your PPTX file using the Presentation class.
  2. Access the Slide: Use the slide index to access a specific slide. In this case, Slides[0] accesses the first slide.
  3. Iterate Through Shapes: Loop through all shapes in the slide to find those that contain text.
  4. Modify Text: If a shape has a text frame, you can read its current text and modify it as needed.
  5. Save Changes: Finally, save the modified presentation to a new file.

This approach allows you to programmatically read and modify text on a specific slide in your PowerPoint presentation.

If you have any further questions or need additional assistance, feel free to ask!

Thank you.

What is the difference between Shapes and TextBoxes and TextFrames? Can we access each piece of text on a slide using Shape collection? Or we need to iterate through TextFrames and TextBoxes also?

@uax99,
The SlideUtil.GetAllTextFrames method you found finds all PowerPoint shapes that contain text frames. The IShape interface is the base interface for many shapes. Not every shape can contain a text frame. For example, IAutoShape, inherited from IShape, can contain a text frame, but IChart cannot. Text boxes in PowerPoint documents are autoshapes.

You can access each piece of text on a slide using the IShape collection as shown above.

Thanks @andrey.potapov ,

What about the following code? Will this work for all text pieces?

//Instatiate Presentation class that represents a PPTX file
Presentation pptxPresentation = new Presentation("demo.ppt");

foreach (var slide in pptxPresentation.Slides)
{
    ITextFrame[] TextFrames = SlideUtil.GetAllTextBoxes(slide);

    for (int i = 0; i < TextFrames.Length; i++)
    {
        //Loop through paragraphs in current ITextFrame
        foreach (IParagraph para in TextFrames[i].Paragraphs)
        {
            //Loop through portions in the current IParagraph
            foreach (IPortion port in para.Portions)
            {
                //Display text in the current portion
                string text = port.Text;
                port.Text = "[Updated]" + text;
            }
        }
    }
}

@uax99,
Yes, the SlideUtil.GetAllTextBoxes method allows you to get all text frames from a slide.

1 Like