diff --git a/PowerPoint-Presentation/Extract_Text_From_PPTX/.NET/Extract_Text_From_PPTX/Program.cs b/PowerPoint-Presentation/Extract_Text_From_PPTX/.NET/Extract_Text_From_PPTX/Program.cs index 8dfa0698..de5c675b 100644 --- a/PowerPoint-Presentation/Extract_Text_From_PPTX/.NET/Extract_Text_From_PPTX/Program.cs +++ b/PowerPoint-Presentation/Extract_Text_From_PPTX/.NET/Extract_Text_From_PPTX/Program.cs @@ -1,128 +1,120 @@ using Syncfusion.Presentation; using System.Text; -namespace Extract_Text_From_PPTX +//Load the PowerPoint presentation +using(IPresentation presentation = Presentation.Open(Path.GetFullPath(@"Data/Input.pptx"))) +{ + string extractedText = ExtractDocumentText(presentation); + //Write the text collection to a text file + System.IO.File.WriteAllText(Path.GetFullPath(@"../../../Output/Sample.txt"), extractedText); +} + +static string ExtractDocumentText(IPresentation presentation) { - class Program + //Text collection to store the extracted text + StringBuilder textBuilder = new StringBuilder(); + // Extract text from all slides + for (int i = 0; i < presentation.Slides.Count; i++) { + ISlide slide = presentation.Slides[i]; + textBuilder.AppendLine($"--- Slide {i + 1} ---"); - static void Main(string[] args) - { - //Load the PowerPoint presentation - IPresentation presentation = Presentation.Open(Path.GetFullPath(@"Data/Input.pptx")); - //Text collection to store the extracted text - StringBuilder textBuilder = new StringBuilder(); - // Extract text from all slides - for (int i = 0; i < presentation.Slides.Count; i++) - { - ISlide slide = presentation.Slides[i]; - textBuilder.AppendLine($"--- Slide {i + 1} ---"); - - // Extract text from all shapes in the slide - ExtractText(slide.Shapes as IShapes, textBuilder); - - // Extract text from the slide notes body - if (slide.NotesSlide?.NotesTextBody != null) - { - foreach (IParagraph paragraph in slide.NotesSlide.NotesTextBody.Paragraphs) - { - textBuilder.AppendLine(paragraph.Text); - } - } - - // Extract text from the slide notes shapes - if (slide.NotesSlide?.Shapes != null) - { - ExtractText(slide.NotesSlide.Shapes as IShapes, textBuilder); - } + // Extract text from all shapes in the slide + ExtractText(slide.Shapes as IShapes, textBuilder); - // Extract text from the layout slide shapes - if (slide.LayoutSlide?.Shapes != null) - { - ExtractText(slide.LayoutSlide.Shapes as IShapes, textBuilder, true); - - // Extract text from the master slide shapes - if (slide.LayoutSlide.MasterSlide?.Shapes != null) - { - ExtractText(slide.LayoutSlide.MasterSlide.Shapes as IShapes, textBuilder, true); - } - } - - textBuilder.AppendLine(); - } - string extractedText = textBuilder.ToString(); - //Write the text collection to a text file - System.IO.File.WriteAllText(Path.GetFullPath(@"Output/Sample.txt"), extractedText); - //Dispose the presentation instance - presentation.Close(); - } - public static void ExtractText(IShapes shapes, StringBuilder textBuilder, bool ignorePlaceHolder = false) + // Extract text from the slide notes body + if (slide.NotesSlide?.NotesTextBody != null) { - foreach (IShape shape in shapes) + foreach (IParagraph paragraph in slide.NotesSlide.NotesTextBody.Paragraphs) { - if (shape is ITable) - ExtractTextInTable(shape, textBuilder); - else if (shape is ISmartArt) - ExtractTextInSmartArt(shape, textBuilder); - else if (shape is IGroupShape) - ExtractText((shape as IGroupShape).Shapes, textBuilder, ignorePlaceHolder); - else - ExtractTextInShape(shape, textBuilder, ignorePlaceHolder); + textBuilder.AppendLine(paragraph.Text); } } - public static void ExtractTextInSmartArt(IShape shape, StringBuilder textBuilder) + // Extract text from the slide notes shapes + if (slide.NotesSlide?.Shapes != null) { - ISmartArt smartArt = shape as ISmartArt; - if (smartArt == null) - return; - - foreach (ISmartArtNode node in smartArt.Nodes) - { - ExtractTextInSmartArtNode(node, textBuilder); - } + ExtractText(slide.NotesSlide.Shapes as IShapes, textBuilder); } - public static void ExtractTextInShape(IShape shape, StringBuilder textBuilder, bool ignorePlaceHolder) + + // Extract text from the layout slide shapes + if (slide.LayoutSlide?.Shapes != null) { - if (shape.TextBody == null || (ignorePlaceHolder && (shape as ISlideItem).SlideItemType == SlideItemType.Placeholder)) - return; + ExtractText(slide.LayoutSlide.Shapes as IShapes, textBuilder, true); - foreach (IParagraph paragraph in shape.TextBody.Paragraphs) + // Extract text from the master slide shapes + if (slide.LayoutSlide.MasterSlide?.Shapes != null) { - textBuilder.AppendLine(paragraph.Text); + ExtractText(slide.LayoutSlide.MasterSlide.Shapes as IShapes, textBuilder, true); } } + textBuilder.AppendLine(); + } + return textBuilder.ToString(); +} - public static void ExtractTextInTable(IShape shape, StringBuilder textBuilder) - { - ITable table = shape as ITable; - if (table == null) - return; +static void ExtractText(IShapes shapes, StringBuilder textBuilder, bool ignorePlaceHolder = false) +{ + foreach (IShape shape in shapes) + { + if (shape is ITable) + ExtractTextInTable(shape, textBuilder); + else if (shape is ISmartArt) + ExtractTextInSmartArt(shape, textBuilder); + else if (shape is IGroupShape) + ExtractText((shape as IGroupShape).Shapes, textBuilder, ignorePlaceHolder); + else + ExtractTextInShape(shape, textBuilder, ignorePlaceHolder); + } +} +static void ExtractTextInSmartArt(IShape shape, StringBuilder textBuilder) +{ + ISmartArt smartArt = shape as ISmartArt; + if (smartArt == null) + return; - foreach (IRow row in table.Rows) - { - foreach (ICell cell in row.Cells) - { - textBuilder.AppendLine(cell.TextBody.Text); - } - } - } + foreach (ISmartArtNode node in smartArt.Nodes) + { + ExtractTextInSmartArtNode(node, textBuilder); + } +} +static void ExtractTextInShape(IShape shape, StringBuilder textBuilder, bool ignorePlaceHolder) +{ + if (shape.TextBody == null || (ignorePlaceHolder && (shape as ISlideItem).SlideItemType == SlideItemType.Placeholder)) + return; - public static void ExtractTextInSmartArtNode(ISmartArtNode node, StringBuilder textBuilder) - { - if (node.TextBody != null) - { - foreach (IParagraph paragraph in node.TextBody.Paragraphs) - { - textBuilder.AppendLine(paragraph.Text); - } - } + foreach (IParagraph paragraph in shape.TextBody.Paragraphs) + { + textBuilder.AppendLine(paragraph.Text); + } +} +static void ExtractTextInTable(IShape shape, StringBuilder textBuilder) +{ + ITable table = shape as ITable; + if (table == null) + return; - // Recursively extract text from child nodes - foreach (ISmartArtNode childNode in node.ChildNodes) - { - ExtractTextInSmartArtNode(childNode, textBuilder); - } + foreach (IRow row in table.Rows) + { + foreach (ICell cell in row.Cells) + { + textBuilder.AppendLine(cell.TextBody.Text); } } } +static void ExtractTextInSmartArtNode(ISmartArtNode node, StringBuilder textBuilder) +{ + if (node.TextBody != null) + { + foreach (IParagraph paragraph in node.TextBody.Paragraphs) + { + textBuilder.AppendLine(paragraph.Text); + } + } + + // Recursively extract text from child nodes + foreach (ISmartArtNode childNode in node.ChildNodes) + { + ExtractTextInSmartArtNode(childNode, textBuilder); + } +}