I have PPT file on desktop named as “slide.ppt”. I want to read all slides of this PPT file in my ReadSlide function as below
public void ReadSlide(){
}
How can I read all slide from a PPT file in my C# code?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use as below
public void ReadSlide(){
string filePath= @"C:UsersUserNameSlide.pptx";
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
string presentation_textforParent = "";
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_textforParent += text + " ";
}
}
}
}
Method 2
If it was an PPTX, you could read it using OpenXML. Since you specifically asked for PPT, it is a little harder.
You shouldn’t use Automation / Interop for sure, since it isn’t supported in a server environment.
That means you have to use third-party tools to read the PPT. If you google for them, you will see a long list of them. I have never worked with it, but Aspose seems to do the job very well.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0