Extract Text from PowerPoint Presentation to WordPad in C#, VB.NET

As we all know, PowerPoint Presentation may include a lot of media files and images so it's obvious the size can reach up to hundreds of MB. In such a case, you may run into situations where you want to extract text from PowerPoint Presentation to other applications like Microsoft Word or WordPad to reduce its size. This article is aimed to resolve this problem via Spire.Presentation in C#, VB.NET.

To begin with, download Spire.Presentation for .NET, create or open a .NET class application in Visual Studio 2005 or above versions, add Spire.Presentation.dll to your .NET project assemblies. Then, you are able to extract text from PowerPoint Presentation to WordPad with below code snippet.

Step 1: Create a new instance of Presentation and load the sample PowerPoint file.

Presentation presentation = new Presentation("sample.pptx", FileFormat.Pptx2010);

Step 2: Initialize a new instance of StringBuilder class, append extracted text from Presentation to StringBuilder.

 StringBuilder sb = new StringBuilder();

 for (int i = 0; i < ppt.Slides.Count;i++ )
        {
            for (int j = 0; j < ppt.Slides[i].Shapes.Count;j++ )
            { 
                if (ppt.Slides[i].Shapes[j] is IAutoShape)
                {
                    IAutoShape shape=ppt.Slides[i].Shapes[j] as IAutoShape;
                    if (shape.TextFrame != null)
                    {
                        foreach (TextParagraph tp in shape.TextFrame.Paragraphs)
                        {
                            sb.Append(tp.Text + Environment.NewLine);
                        }
                    }
                   
                }
            }
        }

Step 3: Create a new .txt file and write in the specified string text.

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

Result:

Extract text from PPT slide to WordPad

Full Code:

[C#]
        Presentation presentation = new Presentation("sample.pptx", FileFormat.Pptx2010); 
            StringBuilder sb = new StringBuilder();
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape is IAutoShape)
                    {
                        foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                        {
                            sb.Append(tp.Text + Environment.NewLine);
                        }
                    }

                }

            }
            File.WriteAllText("target.txt", sb.ToString());
            Process.Start("target.txt");
[VB.NET]
Dim presentation As New Presentation("sample.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")