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);
}
}
}
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
Instantiate the Presentation: Load your PPTX file using the Presentation class.
Access the Slide: Use the slide index to access a specific slide. In this case, Slides[0] accesses the first slide.
Iterate Through Shapes: Loop through all shapes in the slide to find those that contain text.
Modify Text: If a shape has a text frame, you can read its current text and modify it as needed.
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!
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.
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;
}
}
}
}