EXCEL TOOLS

How to View Powerpoint Files in C#

Published April 3, 2024
Share:

Introduction

In the field of software development, providing rich and captivating user experiences requires the capacity to incorporate many media kinds into programs. Since Microsoft Office PowerPoint presentations are frequently used to communicate information, it is advantageous to incorporate a PowerPoint viewer control into a C# WinForms program. This article explains how to integrate control of that kind, enabling developers to add PowerPoint viewing features to their apps and improve them. In this article, we will create C# PowerPoint Viewer without installing MS PowerPoint Viewer.

How to View Powerpoint Files in C#

  1. Create PowerPoint Application Instance.
  2. Add the Interop reference to the project
  3. Open Presentation using the Instance.
  4. Check and Create an Output Folder for the export files.
  5. Load the create slide images into the Picture box. Use the Button to move between slides.
  6. Close Presentation and Quit Application.

Understanding the PowerPoint Viewer Control

PowerPoint presentations are used extensively in a variety of fields, including education and business. There are several advantages to incorporating a PowerPoint viewer control into a WinForms application.

  • Smooth Integration: By allowing users to see and interact with PowerPoint presentations right within the application, users may reduce the amount of time they spend switching between various programs and streamline their workflow.
  • Enhanced User Experience: The program improves user experience and efficiency by offering a recognizable interface for working with PowerPoint slides.
  • Enhanced Flexibility: For Windows developers in .NET applications, by adding navigation controls or introducing display modes, developers may tailor the viewer control to fit unique needs.

Adding References

Add references to the necessary assemblies first: To add Microsoft.Office.Interop.PowerPoint reference in your project, right-click on the project in Visual Studio, choose "Add" > "Reference," and then add them from the COM tab.

Viewer PowerPoint

The Microsoft.Office.Interop.PowerPoint namespace offers classes and methods for programmatically interacting with PowerPoint presentations. Make sure PowerPoint is installed on the system. Let's dissect and explain in the following code given in the preceding example:

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
    public partial class PowerPointViewer : Form
    {
        private Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
        private Presentation pptPresentation;
        private string outputFolder = @"output_images";
        private int slideIndex = 1;
        public PowerPointViewer()
        {
            InitializeComponent();
        }
        private void DisplaySlide()
        {
            if (!Directory.Exists(outputFolder))
                Directory.CreateDirectory(outputFolder);
            // Export the slide as an png file
            string tempHtmlFile = Path.Combine(outputFolder, "temp.png");
            pptPresentation.Slides [slideIndex].Export(tempHtmlFile, "png", 1024, 768);
            // Load the HTML file into the picture box control
            pictureBox1.ImageLocation = tempHtmlFile;
        }
        private void Next_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex < pptPresentation.Slides.Count)
            {
                slideIndex = slideIndex + 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_Load(object sender, EventArgs e)
        {
            // Load PowerPoint Presentation
            string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
            pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        // Load PowerPoint files here
            DisplaySlide();
        }
        private void Previous_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex > 1)
            {
                slideIndex = slideIndex - 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Close the PowerPoint presentation and quit the application when the form is closing
            pptPresentation.Close();
            pptApplication.Quit();
        }
    }
}
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataTableWindowsForm
{
    public partial class PowerPointViewer : Form
    {
        private Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
        private Presentation pptPresentation;
        private string outputFolder = @"output_images";
        private int slideIndex = 1;
        public PowerPointViewer()
        {
            InitializeComponent();
        }
        private void DisplaySlide()
        {
            if (!Directory.Exists(outputFolder))
                Directory.CreateDirectory(outputFolder);
            // Export the slide as an png file
            string tempHtmlFile = Path.Combine(outputFolder, "temp.png");
            pptPresentation.Slides [slideIndex].Export(tempHtmlFile, "png", 1024, 768);
            // Load the HTML file into the picture box control
            pictureBox1.ImageLocation = tempHtmlFile;
        }
        private void Next_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex < pptPresentation.Slides.Count)
            {
                slideIndex = slideIndex + 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_Load(object sender, EventArgs e)
        {
            // Load PowerPoint Presentation
            string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
            pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        // Load PowerPoint files here
            DisplaySlide();
        }
        private void Previous_Click(object sender, EventArgs e)
        {
            int currentSlideIndex = slideIndex;
            if (currentSlideIndex > 1)
            {
                slideIndex = slideIndex - 1;
                DisplaySlide();
            }
        }
        private void PowerPointViewercs_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Close the PowerPoint presentation and quit the application when the form is closing
            pptPresentation.Close();
            pptApplication.Quit();
        }
    }
}
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.PowerPoint
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace DataTableWindowsForm
	Partial Public Class PowerPointViewer
		Inherits Form

		Private pptApplication As New Microsoft.Office.Interop.PowerPoint.Application()
		Private pptPresentation As Presentation
		Private outputFolder As String = "output_images"
		Private slideIndex As Integer = 1
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub DisplaySlide()
			If Not Directory.Exists(outputFolder) Then
				Directory.CreateDirectory(outputFolder)
			End If
			' Export the slide as an png file
			Dim tempHtmlFile As String = Path.Combine(outputFolder, "temp.png")
			pptPresentation.Slides (slideIndex).Export(tempHtmlFile, "png", 1024, 768)
			' Load the HTML file into the picture box control
			pictureBox1.ImageLocation = tempHtmlFile
		End Sub
		Private Sub Next_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim currentSlideIndex As Integer = slideIndex
			If currentSlideIndex < pptPresentation.Slides.Count Then
				slideIndex = slideIndex + 1
				DisplaySlide()
			End If
		End Sub
		Private Sub PowerPointViewercs_Load(ByVal sender As Object, ByVal e As EventArgs)
			' Load PowerPoint Presentation
			Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file
			pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse)
		' Load PowerPoint files here
			DisplaySlide()
		End Sub
		Private Sub Previous_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim currentSlideIndex As Integer = slideIndex
			If currentSlideIndex > 1 Then
				slideIndex = slideIndex - 1
				DisplaySlide()
			End If
		End Sub
		Private Sub PowerPointViewercs_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
			' Close the PowerPoint presentation and quit the application when the form is closing
			pptPresentation.Close()
			pptApplication.Quit()
		End Sub
	End Class
End Namespace
VB   C#

First, we import the required namespaces. File operations are handled by System.IO, System.Windows.Forms offers WinForms features, whereas Microsoft.Office classes for interacting with PowerPoint are included in PowerPoint. Within the PowerPointViewerApp namespace, we define a WinForms Form class called PowerPointViewer. The primary user interface for the view PPT application will be this form.

It is announced that references to the PowerPoint application and presentation would be stored in two private fields, pptApplication, and pptPresentation. By using InitializeComponent(), the constructor sets up the form's visual components that were defined by the designer and initializes the form. When the form loads, the InitializeComponent() function is called. The PowerPoint application (pptApplication) is initialized and made accessible at this point.

Next, we launch the PowerPoint file that pptFilePath specifies. Using pptApplication.Presentations, we open the PowerPoint file by providing its path (pptFilePath) to the Open() function. A Presentation object, which represents the opened presentation, is returned by the procedure.

To show the first slide of the PowerPoint presentation, we use the DisplaySlide() function. The first slide's index is 1. The slide that is displayed in the Picture box control is the result of this function. We use the Export() function to export the selected slide as a PNG file. The system's temporary folder is where the exported PNG file is momentarily stored. Using the ImageLocation() method, we load the exported PNG file into the PictureBox control. These techniques take care of flipping between slides.

The navigation buttons btnPrevious_Click and btnNext_Click take you to the previous and next slides, respectively. Using pptPresentation, we are able to obtain the index of slide in the slideIndex variable. We call DisplaySlide() with the index of the previous or next slide, respectively, if the current slide index is within the valid range of 1 to the entire number of slides.

When the form closes, the Close() function is called. Here, we make sure that resource management is done correctly by releasing system resources by exiting the PowerPoint program and terminating the PowerPoint application using the Quit() method.

PowerPoint file

How to View Powerpoint Files in C#: Figure 1 - PowerPoint file demo.pptx

Output: PowerPoint Viewer

How to View Powerpoint Files in C#: Figure 2 - PowerPointViwercs

IronXL

Excel file manipulation in C# is made easier using the popular .NET Excel Library IronXL. It's a versatile tool fit for a variety of applications thanks to its vast feature set for reading, generating, and modifying Excel files.

Below, I'll go over some of IronXL's key attributes:

Easy Access and High speed

  • With IronXL, developers can read data from current Excel files and write data to new or existing Excel files rapidly. This addresses getting access to the workbook and worksheet properties like cell values, formulas, and formatting.

Extensive Excel Support

  • Developers may import data from databases and CSV files, among other sources, into Excel spreadsheets by using IronXL. Similarly, data from Excel files may be exported to a variety of formats, including CSV, HTML, XML, and PDF.

Flexibility

  • With IronXL, developers may dynamically add, modify, and delete worksheets from Excel files. This allows for flexibility in data format and organization according to application requirements.

Accurate Modifications

  • Excel spreadsheets may have individual cells accurately modified thanks to IronXL. Developers may set cell values, formulas, styles, formatting, and other features programmatically.

To learn more about the documentation refer here.

Install IronXL

Before continuing, let's first install IronXL using the NuGet Package Manager Console:

Install-Package IronXL.Excel

IronXL may be utilized in our C# project after installation.

Using IronXL for Excel Operations

Let's look at a hypothetical scenario in which we want to read data from an Excel file using IronXL. Here's a little example of how to achieve this:

using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets [0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets [0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
Imports Microsoft.VisualBasic
Imports IronXL
Imports System
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Path to the Excel file
		Dim excelFilePath As String = "sample.xlsx"
		' Load the Excel file
		Dim workbook As WorkBook = WorkBook.Load(excelFilePath)
		' Access the first worksheet
		Dim worksheet As WorkSheet = workbook.WorkSheets (0)
		' Iterate through rows and columns to read data
		For Each row In worksheet.Rows
			For Each cell In row
				Console.Write(cell.Value & vbTab)
			Next cell
			Console.WriteLine()
		Next row
	End Sub
End Class
VB   C#

We start by adding the necessary namespaces. The IronXL namespace contains the classes and methods that are provided by the IronXL library. The path to sample.xlsx, the Excel file we want to read, is given. Utilising WorkBook, the Excel file is loaded. The Workbook object supplied by the Load() method represents the Excel workbook. We may access the workbook's first worksheet by using workbook.WorkSheets [0]. We go over the rows and columns of the worksheet using stacked for each loops. We send each cell's value to the console.

How to View Powerpoint Files in C#: Figure 3 - IronXL Output

To learn more about the IronXL code examples refer here.

Conclusion

Several software applications require the conversion of PowerPoint presentations to images using C#. The process might be finished rather rapidly whether or not Microsoft.Office.Interop.PowerPoint namespace is used. You can easily incorporate PowerPoint to image conversion into your C# programs with the aid of this article's code samples, opening up a world of possibilities for information alteration and delivery.

IronXL offers a simple and efficient method to do Excel operations in C# without requiring Excel to be installed on the target system or relying on the Interop library. With its comprehensive feature set and user-friendly API, IronXL is a handy tool for developers working with Excel data in C# applications. It simplifies tasks like reading, writing, and editing Excel files. For Excel-related development projects, IronXL provides a stable solution that boosts productivity and flexibility, whether you're processing data, producing reports, or automating spreadsheet tasks.

A free Community Edition with restrictions for non-commercial usage was made available by IronXL. Paid versions start at $749 and can be obtained through subscription or perpetual-based licensing schemes. They are more fully functioning, and offer more features, and support. IronXL also offers a free-trial license. For comprehensive and current information about licensing please visit the license page. Visit this website to learn more about Iron Software products.

NEXT >
How to Use Create PowerPoint from a Template in C#

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,068,832 View Licenses >