Spire.Presentation as a powerful PowerPoint library, enables developers to create SmartArt, add text to SmartArt and format SmartArt in a flexible way (Reference: How to Create and Format SmartArt in PowerPoint in C#, VB.NET). In this article, we will further introduce how to extract text from SmartArt in PowerPoint in C# and VB.NET by using Spire.Presentation for .NET.
Please look at the appearance of the sample PPT file first:
Detail steps and code snippets are as below:
Step 1: Create a new instance of Presentation class and load the sample PPT file.
Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx");
Step 2: Create a new instance of StringBulider class, traverse through all the slides of the PPT file, find the SmartArt shapes, and then extract text from SmartArt shape Nodes and append to the StringBuilder object.
StringBuilder st = 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 ISmartArt) { ISmartArt smartArt = ppt.Slides[i].Shapes[j] as ISmartArt; for (int k = 0; k < smartArt.Nodes.Count; k++) { st.Append(smartArt.Nodes[k].TextFrame.Text); } } } }
Step 3: Create a new text file and write the extracted text to the text file.
File.WriteAllText("Result.txt", st.ToString());
The result text file:
Full codes:
using System.IO; using System.Text; using Spire.Presentation; using Spire.Presentation.Diagrams; namespace Extract_text_from_SmartArt_in_PPT { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("Sample.pptx"); StringBuilder st = 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 ISmartArt) { ISmartArt smartArt = ppt.Slides[i].Shapes[j] as ISmartArt; for (int k = 0; k < smartArt.Nodes.Count; k++) { st.Append(smartArt.Nodes[k].TextFrame.Text); } } } } File.WriteAllText("Result.txt", st.ToString()); } } }
Imports System.IO Imports System.Text Imports Spire.Presentation Imports Spire.Presentation.Diagrams Namespace Extract_text_from_SmartArt_in_PPT Class Program Private Shared Sub Main(args As String()) Dim ppt As New Presentation() ppt.LoadFromFile("Sample.pptx") Dim st As New StringBuilder() For i As Integer = 0 To ppt.Slides.Count - 1 For j As Integer = 0 To ppt.Slides(i).Shapes.Count - 1 If TypeOf ppt.Slides(i).Shapes(j) Is ISmartArt Then Dim smartArt As ISmartArt = TryCast(ppt.Slides(i).Shapes(j), ISmartArt) For k As Integer = 0 To smartArt.Nodes.Count - 1 st.Append(smartArt.Nodes(k).TextFrame.Text) Next End If Next Next File.WriteAllText("Result.txt", st.ToString()) End Sub End Class End Namespace