Get all the text in all slides in a presentation

  • OpenXML SDK
  • Spire.Presentation
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter a presentation file name without extension: ");
            string fileName = Console.ReadLine();
            string file = @"..\..\Documents\" + fileName + ".pptx";
            int numberOfSlides = CountSlides(file);
            System.Console.WriteLine("Number of slides = {0}", numberOfSlides);
            string slideText;
            for (int i = 0; i < numberOfSlides; i++)
            {
                GetSlideIdAndText(out slideText, file, i);
                System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText);
            }
            System.Console.ReadKey();
        }
        public static int CountSlides(string presentationFile)
        {
            // Open the presentation as read-only.
            using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
            {
                // Pass the presentation to the next CountSlides method
                // and return the slide count.
                return CountSlides(presentationDocument);
            }
        }

        // Count the slides in the presentation.
        public static int CountSlides(PresentationDocument presentationDocument)
        {
            // Check for a null document object.
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            int slidesCount = 0;

            // Get the presentation part of document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;
            // Get the slide count from the SlideParts.
            if (presentationPart != null)
            {
                slidesCount = presentationPart.SlideParts.Count();
            }
            // Return the slide count to the previous method.
            return slidesCount;
        }

        public static void GetSlideIdAndText(out string sldText, string docName, int index)
        {
            using (PresentationDocument ppt = PresentationDocument.Open(docName, false))
            {
                // Get the relationship ID of the first slide.
                PresentationPart part = ppt.PresentationPart;
                OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;

                string relId = (slideIds[index] as SlideId).RelationshipId;

                // Get the slide part from the relationship ID.
                SlidePart slide = (SlidePart)part.GetPartById(relId);

                // Build a StringBuilder object.
                StringBuilder paragraphText = new StringBuilder();

                // Get the inner text of the slide:
                IEnumerable texts = slide.Slide.Descendants();
                foreach (A.Text text in texts)
                {
                    paragraphText.Append(text.Text);
                }
                sldText = paragraphText.ToString();
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            string file = @"..\..\Documents\Myppt9.pptx";
            int numberOfSlides = CountSlides(file);
            System.Console.WriteLine("Number of slides = {0}", numberOfSlides);
            string slideText;
            for (int i = 0; i < numberOfSlides; i++)
            {
                slideText = GetSlideText(file, i);
                System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText);
            }
            System.Console.ReadKey();
        }
        public static int CountSlides(string presentationFile)
        {
            //Initialize an instances of Presentation class 
            using (Presentation presentation = new Presentation(presentationFile, FileFormat.Pptx2010))
            {
                return presentation.Slides.Count;
            }
        }
        public static string GetSlideText(string docName, int index)
        {
            string sldText = "";
            //Initialize an instances of Presentation class 
            using (Presentation presentation = new Presentation(docName, FileFormat.Pptx2010))
            {
                //Get the slide
                ISlide sld = presentation.Slides[index];

                //Iterate through shapes to find the text
                foreach (Shape shp in sld.Shapes)
                {
                    //get the text of each placeholder
                    sldText += ((IAutoShape)shp).TextFrame.Text;
                }

            }
            return sldText;
        }
    }