How to read a PowerPoint file in C#, VB.NET in WinForms?
Syncfusion Essential Presentation is a .NET PowerPoint library used to create, open, read, edit, and convert PowerPoint presentations. The library supports reading PowerPoint presentations in C# and VB.NET from a file path or a stream. This functionality is exposed through the presentation. Open static method overloads.
Steps to read PowerPoint programmatically:
- Create a new C# console application.
- Install Syncfusion.Presentation.Base NuGet package to the console application project from nuget.org. For more information about adding a NuGet feed in Visual Studio and installing NuGet packages, refer to documentation
- Include the following namespace in the Program.cs file.
using Syncfusion.Presentation;
Imports Syncfusion.Presentation
Read a PowerPoint file from stream
The following example demonstrates how to read a PowerPoint file from a stream and save it to a physical file path.
//File stream that holds the PowerPoint presentation. FileStream fileStream = new FileStream("Sample.pptx", FileMode.Open, System.IO.FileAccess.Read); //Open the presentation. IPresentation powerpointDoc = Presentation.Open(fileStream); //Save the PowerPoint presentation to a file. powerpointDoc.Save("Resaved.pptx"); //Close the presentation. powerpointDoc.Close(); //Dispose the used memory. fileStream.Dispose();
'File stream that holds the PowerPoint presentation. Dim fileStream As New FileStream("Sample.pptx", FileMode.Open, System.IO.FileAccess.Read) 'Open the presentation. Dim powerpointDoc As IPresentation = Presentation.Open(fileStream) 'Save the PowerPoint presentation to a file. powerpointDoc.Save("Resaved.pptx") 'Close the presentation. powerpointDoc.Close() 'Dispose the used memory. fileStream.Dispose()
Read the PowerPoint file from file path
The following example demonstrates how to read a PowerPoint file from a file path and save it to a stream.
//Open the presentation. IPresentation powerpointDoc = Presentation.Open("Sample.pptx"); //Create a stream to save the PowerPoint presentation. MemoryStream stream = new MemoryStream(); //Save the PowerPoint presentation to the stream. powerpointDoc.Save(stream); //Close the presentation. powerpointDoc.Close();
'Open a presentation. Dim powerpointDoc As IPresentation = Presentation.Open("Sample.pptx") 'Create a stream to save the PowerPoint presentation. Dim stream As New MemoryStream() 'Save the PowerPoint presentation to the stream. powerpointDoc.Save(stream) 'Close the presentation. powerpointDoc.Close()
The following example demonstrates how to read a PowerPoint presentation from a file path, iterate over all shapes in the slides and print the text in console output.
//Open the presentation IPresentation powerpointDoc = Presentation.Open("Sample.pptx"); //Iterate the slides in the PowerPoint presentation file foreach (ISlide slide in powerpointDoc.Slides) { //Iterate the shapes in the slide foreach (IShape shape in slide.Shapes) { // Check whether the shape is an auto-shape. Other types can be charts, tables or SmartArt diagrams. if (shape.SlideItemType == SlideItemType.AutoShape) //Print the text to the console output System.Console.WriteLine(shape.TextBody.Text); } } //Save the PowerPoint presentation to another file. powerpointDoc.Save("Resaved.pptx"); //Close the presentation. powerpointDoc.Close(); //Wait to check the console output System.Console.Read();
'Open the presentation Dim powerpointDoc As IPresentation = Presentation.Open("Sample.pptx") 'Iterate the slides in the PowerPoint presentation file For Each slide As ISlide In powerpointDoc.Slides 'Iterate the shapes in the slide For Each shape As IShape In slide.Shapes ' Check whether the shape is an auto-shape. Other types can be charts, tables or SmartArt diagrams. If shape.SlideItemType = SlideItemType.AutoShape Then 'Print the text to the console output System.Console.WriteLine(shape.TextBody.Text) End If Next Next 'Save the PowerPoint presentation to another file. powerpointDoc.Save("Resaved.pptx") 'Close the presentation. powerpointDoc.Close() 'Wait to check the console output System.Console.Read()
You can download the working sample from Read-PowerPoint-File.Zip.
Take a moment to peruse the documentation with code examples.
Refer here to explore the rich set of Syncfusion Essential Presentation features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to read a PowerPoint file in C#, VB.NET in WinForms.
You can refer to our WinForms Presentation feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Presentation example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!