SuKu
SuKu

Reputation: 433

Extract text from group shape in pptx using Spire.Presentation for .NET

I am trying to get text from power point presentation using Spire.Presentation plugin. I am able to extract the text from simple textboxes with following code successfully. But as soon as the textboxes are grouped together then this code returns blank lines. Please help to get text when the shapes are grouped. Couldn't find any solution anywhere.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim presentation As New Presentation("Drawing.pptx", FileFormat.Pptx2010)
    Dim sb As New StringBuilder()
    For Each slide As ISlide In presentation.Slides
        For Each shape As IShape In slide.Shapes
            If TypeOf shape Is IAutoShape Then
                For Each tp As TextParagraph In TryCast(shape, IAutoShape).TextFrame.Paragraphs
                    sb.Append(tp.Text + Environment.NewLine)
                Next
            End If

        Next
    Next
    File.WriteAllText("target.txt", sb.ToString())
    Process.Start("target.txt")

End Sub

Upvotes: 0

Views: 483

Answers (1)

Dheeraj Malik
Dheeraj Malik

Reputation: 1003

You can use the following code to extract text from shape and group shape. The code is in c#, but I think you're able to transfer it to vb.net.

Presentation ppt = new Presentation(@"..\..\test document\3SlidesNoTransitions8762.pptx", FileFormat.Pptx2010);
        StringBuilder sb = new StringBuilder();
        foreach (ISlide slide in ppt.Slides)
        {
            foreach (IShape shape in slide.Shapes)
            {
                sb.AppendLine(getShapeText(shape));
            }
            File.WriteAllText("8672.txt", sb.ToString());
        }
    }
    public static string getShapeText(IShape shape)
    {
        StringBuilder sb = new StringBuilder();
        if (shape is IAutoShape)
        {
            IAutoShape ashape = shape as IAutoShape;
            if (ashape.TextFrame != null)
            {
                foreach (TextParagraph pg in ashape.TextFrame.Paragraphs)
                {
                    sb.AppendLine(pg.Text);
                }
            }
        }
        else if (shape is GroupShape)
        {
            GroupShape gs = shape as GroupShape;
            foreach (IShape s in gs.Shapes)
            {
                sb.AppendLine(getShapeText(s));
            }
        }
        return sb.ToString();
    }

Upvotes: 1

Related Questions