How to use C# to create a PowerPoint presentation

Introduction

Whether you're giving a class, making a sales presentation, or providing project updates, in today's digital world, good communication is essential. For creating visually striking presentations, Microsoft PowerPoint continues to be one of the most effective tools.

Making presentations by hand, however, may be laborious and time-consuming. Thank goodness, developers may automate the procedure, optimize processes, and fully utilize PowerPoint thanks to the flexibility of C# (C Sharp) and the Microsoft PowerPoint Interop library. In this article, we are going to use C# to create a PowerPoint presentation.

How to use C# to create a PowerPoint presentation

  1. Create a new C# Project.
  2. Launch a fresh instance of the PowerPoint program.
  3. Make a fresh presentation and include slides, text, formatting, and other elements.
  4. The presentation should be saved as a new file.
  5. End the PowerPoint program and close the presentation.

Understanding PowerPoint Interop

Let's learn the fundamentals of PowerPoint Interop before delving into the nuances of making PowerPoint presentations with C#. Developers may create presentations, add slides, insert information, apply formatting, and more by interacting with PowerPoint apps programmatically with the help of the PowerPoint Interop library. With the help of this library, developers may customize presentations to meet their unique requirements by accessing a wealth of functions.

Make sure you have installed Visual Studio on your computer before proceeding. Make a new C# project and include the Microsoft PowerPoint Interop library reference. This reference makes it possible for your C# code to communicate with the PowerPoint program, which makes it easier to create and modify PowerPoint documents.

Create a New Visual Studio Project

Use the steps listed below to begin a new Visual Studio project.

Open the Visual Studio program. Make sure you have installed Visual Studio on your PC before using it.

Choose File, New, and finally Project.

How to use C# to create a PowerPoint presentation: Figure 1 - New Project

In the "Create a new project" box, select your favorite programming language (C#, for example) from the left side. Next, select the "Console App" or "Console App (.NET Core)" template from the available project templates list. Please complete the "Name" form to give your project a name.

How to use C# to create a PowerPoint presentation: Figure 2

Select the location for the project's storage. Click "Create" to start working on a new Console application project.

How to use C# to create a PowerPoint presentation: Figure 3

Include a reference to the Microsoft PowerPoint Interop library in a newly created C# project. This library makes it possible for your C# code to communicate with the PowerPoint program, making it simpler to create and engage with presentations.

Creating a New Presentation C#

To create a PowerPoint presentation programmatically, launch the PowerPoint application and create a new instance, this will be used to make a simple PowerPoint document.

You may create, open, and modify presentations directly within your C# PowerPoint presentation by using Microsoft.Office.Interop.PowerPoint.Application class.

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
class Program
{
    static void Main(string[] args)
    {
            // Create an instance of PowerPoint application
            PowerPoint.Application powerpointApp = new PowerPoint.Application();
            // Create powerpoint presentation
            PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
            // Customize the presentation
            // Add slides, insert content, apply formatting, etc.
            // Add a new slide
            PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
            // Insert text into the presentation slide
            slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
            slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
            // Add an image to the slide
            slide.Shapes.AddPicture(@"sample.png",
                                    Microsoft.Office.Core.MsoTriState.msoFalse,
                                    Microsoft.Office.Core.MsoTriState.msoCTrue,
                                    100, 100, 300, 200);
            // Save and close the presentation file
            presentation.SaveAs("Presentation.pptx");
            presentation.Close();
            // Quit PowerPoint application
            powerpointApp.Quit();
            Console.ReadKey();
    }
}
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
class Program
{
    static void Main(string[] args)
    {
            // Create an instance of PowerPoint application
            PowerPoint.Application powerpointApp = new PowerPoint.Application();
            // Create powerpoint presentation
            PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
            // Customize the presentation
            // Add slides, insert content, apply formatting, etc.
            // Add a new slide
            PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
            // Insert text into the presentation slide
            slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
            slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
            // Add an image to the slide
            slide.Shapes.AddPicture(@"sample.png",
                                    Microsoft.Office.Core.MsoTriState.msoFalse,
                                    Microsoft.Office.Core.MsoTriState.msoCTrue,
                                    100, 100, 300, 200);
            // Save and close the presentation file
            presentation.SaveAs("Presentation.pptx");
            presentation.Close();
            // Quit PowerPoint application
            powerpointApp.Quit();
            Console.ReadKey();
    }
}
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Friend Class Program
	Shared Sub Main(ByVal args() As String)
			' Create an instance of PowerPoint application
			Dim powerpointApp As New PowerPoint.Application()
			' Create powerpoint presentation
			Dim presentation As PowerPoint.Presentation = powerpointApp.Presentations.Add()
			' Customize the presentation
			' Add slides, insert content, apply formatting, etc.
			' Add a new slide
			Dim slide As PowerPoint.Slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle)
			' Insert text into the presentation slide
			slide.Shapes(1).TextFrame.TextRange.Text = "Demo"
			slide.Shapes(2).TextFrame.TextRange.Text = "PowerPoint"
			' Add an image to the slide
			slide.Shapes.AddPicture("sample.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 100, 100, 300, 200)
			' Save and close the presentation file
			presentation.SaveAs("Presentation.pptx")
			presentation.Close()
			' Quit PowerPoint application
			powerpointApp.Quit()
			Console.ReadKey()
	End Sub
End Class
VB   C#

You may alter a newly constructed PPTX file presentation to make it fit your requirements. Slides, text, photos, charts, chart title, and other multimedia items may be inserted, formatting may be applied, transitions and animations may be established, and other tasks may be involved. By utilizing the features offered by the PowerPoint Interop library, you may programmatically adjust different parts of the presentation.

It's crucial to save your work and close the presentation once you've adjusted it to your satisfaction in order to free up system resources. To save the PowerPoint document to a specific file location, use the SaveAs() method; to dismiss it, use the dismiss method.

How to use C# to create a PowerPoint presentation: Figure 4

IronXL

IronXL is a feature-rich C# library designed for interacting with Excel files. A vast array of capabilities for dynamically producing, filling, and formatting Excel documents are available with Iron Software's IronXL. IronXL optimizes Excel interactions in C# and offers developers a smooth experience for Excel-related activities thanks to its user-friendly API and comprehensive documentation.

Features of IronXL

  • Broad Support for Excel: IronXL can open and work with a vast number of Excel files in a variety of Excel formats, including CVS, file, xls, and xlsx files. Thanks to IronXL's powerful parsing tools, developers may efficiently extract data from an existing Excel file using both old and modern Excel files.
  • High speed: Performance optimization is given priority by IronXL. Because Excel uses effective algorithms and memory management techniques, interactions with the program are dependable and fast. Developers can effortlessly handle large Excel files because of IronXL's optimized processing rates and decreased memory usage.
  • Simple and easy-to-use API: IronXL is appropriate for developers of all experience levels due to its easy-to-use API. IronXL offers easy-to-use methods for reading Excel files, accessing Excel workbooks, and extracting data from cells, which lowers the learning curve for C# writers and speeds up the creation and reading of existing files.

To know more about IronXL documentation refer here.

Install IronXL

Install IronXL using the command line using the following instructions.

Go to Tools -> NuGet Package management -> Package manager interface in Visual Studio.

In the console tab of the package manager, type the following code:

Install-Package IronXL.Excel
Install-Package IronXL.Excel
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronXL.Excel
VB   C#

After downloading and installing the file on the current project, it is now functional.

Create an Excel file Using IronXL

Now let's look at a real code sample that shows you how to use IronXL to write data to an Excel sheet in C#. We will demonstrate how to open a new Excel workbook, fill up a worksheet, and then save the data to a file:

using IronXL;
class Program
{
    static void Main(string[] args)
    {
            // Create a new WorkBook object
            WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
            // Add a new WorkSheet to the workbook
            WorkSheet workSheet = workBook.CreateWorkSheet("Sheet1");
            // Define sample data
            string[,] data = {
            { "Name" },
            { "Deadpool " },
            { "Wolverine"},
            { "Cyclops" }
        };
            // Populate the worksheet with data
            for (int row = 0; row < data.GetLength(0); row++)
            {
                for (int col = 0; col < data.GetLength(1); col++)
                {
                    workSheet.SetCellValue(row, col, data[row, col]);
                }
            }
            // Save the workbook to a  xlsx file
            workBook.SaveAs("Demo.xlsx");
            // Close the workbook
            workBook.Close();
    }
}
using IronXL;
class Program
{
    static void Main(string[] args)
    {
            // Create a new WorkBook object
            WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
            // Add a new WorkSheet to the workbook
            WorkSheet workSheet = workBook.CreateWorkSheet("Sheet1");
            // Define sample data
            string[,] data = {
            { "Name" },
            { "Deadpool " },
            { "Wolverine"},
            { "Cyclops" }
        };
            // Populate the worksheet with data
            for (int row = 0; row < data.GetLength(0); row++)
            {
                for (int col = 0; col < data.GetLength(1); col++)
                {
                    workSheet.SetCellValue(row, col, data[row, col]);
                }
            }
            // Save the workbook to a  xlsx file
            workBook.SaveAs("Demo.xlsx");
            // Close the workbook
            workBook.Close();
    }
}
Imports IronXL
Friend Class Program
	Shared Sub Main(ByVal args() As String)
			' Create a new WorkBook object
			Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
			' Add a new WorkSheet to the workbook
			Dim workSheet As WorkSheet = workBook.CreateWorkSheet("Sheet1")
			' Define sample data
			Dim data(,) As String = {
				{ "Name" },
				{ "Deadpool " },
				{ "Wolverine"},
				{ "Cyclops" }
			}
			' Populate the worksheet with data
			For row As Integer = 0 To data.GetLength(0) - 1
				For col As Integer = 0 To data.GetLength(1) - 1
					workSheet.SetCellValue(row, col, data(row, col))
				Next col
			Next row
			' Save the workbook to a  xlsx file
			workBook.SaveAs("Demo.xlsx")
			' Close the workbook
			workBook.Close()
	End Sub
End Class
VB   C#

Using IronXL's construct() function, we first construct a new WorkBook object in this code example and provide the required Excel file type. Creating a new worksheet inside the existing worksheet and explaining some sample data in a two-dimensional array is the next stage.

Next, we use nested loops to access and set the values of various cells in order to fill the spreadsheet with the sample data. After saving the workbook to a file called "SampleData.xlsx" using the SaveAs() function, we shut it down to save up system resources. In a similar vein, we may construct many sheets for the target file.

Below is the Excel file that was produced as an output. To know more about writing Excel files refer here.

How to use C# to create a PowerPoint presentation: Figure 5

Conclusion

Conclusively, employing C# to create PowerPoint presentations is an effective way to improve productivity, streamline processes, and provide material that is memorable. The Microsoft PowerPoint Interop library's features may be utilized by developers to automate the production and customization of presentations, resulting in time and effort savings and professional outcomes when working with PowerPoint files.

Learning this method can improve your presenting skills and help you engage your audience with visually striking slides, whether you're a presenter, educator, or business professional.

IronXL is a potent replacement for Microsoft Excel for C# developers, offering complete Excel compatibility, excellent speed, and smooth .NET Framework interaction. Excel authoring in C# is made easier with IronXL's user-friendly API and fine-grained control over Excel documents.

This makes it easier for developers to create dynamic reports, export data, and automate Excel-related processes. Excel-related chores may be made easier using IronXL, which C# developers can use to fully use Excel in their C# programs, regardless of whether they are producing Excel files for desktop, online, or mobile apps.

When IronXL initially appears, it costs $749. Additionally, consumers may receive product updates and help by paying a one-year membership fee. IronXL provides protection against unrestricted redistribution for a price. Visit this page to learn more about the approximate cost. Go here to learn more about IronSoftware.