How to read Microsoft PowerPoint document contents using C#/.NET?

How to read Microsoft PowerPoint document contents using C#/.NET?


Recently we learnt how to read Microsoft Word and Microsoft Excel document contents (text only) using the interop APIs exposed by Microsoft. Now, what about reading the text content from PowerPoint slides? This can be achievable using another interop assembly file.

 

Today we will discuss how to extract the texts available in PPT files using 'Microsoft.Office.Interop.PowerPoint.dll'. Code has been shared for your easy reference.

 

How to read Microsoft PowerPoint document contents using C#/.NET? (www.kunal-chowdhury.com)

 

First, you need to create the instance of PowerPoint application. Then open the presentation, that you want to read, by calling the 'pptPresentations.Open' method as shown in the below code snippet. Next, you need to iterate through the slides available in the presentation file and find out the shapes where TextFrame is available. TextFrame holds the text content of each slides. Now retrieve the TextRange out of the TextFrame to extract the text content.

 

You would like to read:


How to read Microsoft Word document contents using C#/.NET?

How to read Microsoft Excel document contents using C#/.NET?

How to read Microsoft PowerPoint document contents using C#/.NET?


 

Here's the complete source code for you to use, but please make sure to properly release the COM objects at the place where it is mentioned:

 

   1:  public static string GetTextFromPowerPoint(string filePath)
   2:  {
   3:      if (string.IsNullOrEmpty(filePath))
   4:      {
   5:          throw new ArgumentNullException("filePath");
   6:      }
   7:   
   8:      if (!File.Exists(filePath))
   9:      {
  10:          throw new FileNotFoundException("Could not find file", filePath);
  11:      }
  12:   
  13:      var stringBuilder = new StringBuilder();
  14:   
  15:      try
  16:      {
  17:          PowerPoint.Application pptApp = new PowerPoint.Application();
  18:          PowerPoint.Presentations pptPresentations = pptApp.Presentations;
  19:          PowerPoint.Presentation pptPresentation = pptPresentations.Open(filePath,
MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
  20:          PowerPoint.Slides pptSlides = pptPresentation.Slides;
  21:   
  22:          if (pptSlides != null)
  23:          {
  24:              var slidesCount = pptSlides.Count;
  25:              if (slidesCount > 0)
  26:              {
  27:                  for (int slideIndex = 1; slideIndex <= slidesCount; slideIndex++)
  28:                  {
  29:                      var slide = pptSlides[slideIndex];
  30:                      foreach (PowerPoint.Shape textShape in slide.Shapes)
  31:                      {
  32:                          if (textShape.HasTextFrame == MsoTriState.msoTrue && 
textShape.TextFrame.HasText == MsoTriState.msoTrue)
  33:                          {
  34:                              PowerPoint.TextRange pptTextRange = textShape.TextFrame.TextRange;
  35:                              if (pptTextRange != null && pptTextRange.Length > 0)
  36:                              {
  37:                                  stringBuilder.Append(" " + pptTextRange.Text);
  38:                                  ReleaseComObject(pptTextRange);
  39:                              }
  40:                          }
  41:   
  42:                          ReleaseComObject(textShape);
  43:                      }
  44:   
  45:                      ReleaseComObject(slide);
  46:                  }
  47:              }
  48:          }
  49:   
  50:          ReleaseComObject(pptSlides);
  51:          ReleaseComObject(pptPresentation);
  52:          ReleaseComObject(pptPresentations);
  53:          ReleaseComObject(pptApp);
  54:      }
  55:      catch (Exception ex)
  56:      {
  57:          // handle exceptions, if any
  58:      }
  59:   
  60:      return stringBuilder.ToString();
  61:  }

 

Was it helpful? Do let me know if you have any queries. Stay tuned for more articles and subscribe to my feed to get all the latest updates.

 

 



If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

4 comments

 
© 2008-2017 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top