Create PowerPoint .PPT programmatically using C#

If you are a developed and want to create your PPT presentations using C# or VB, then you can use the Office Interop libraries to code your own PPT and .pptx files programmatically. This article will show you a quick snippet to make PowerPoint files on the fly.

Create PowerPoint .PPT programmatically using C#

First, you need to prepare your environment and associate the Interop libraries. You can download Interop libraries for free, but you will need to have Office installed. This was tested on Office 2010 but it should work in 2007 as well.

Create PowerPoint .PPT programmatically using C#

Application pptApplication = new Application();

Microsoft.Office.Interop.PowerPoint.Slides slides;
Microsoft.Office.Interop.PowerPoint._Slide slide;
Microsoft.Office.Interop.PowerPoint.TextRange objText;

// Create the Presentation File
Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);

Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

// Create new Slide
slides = pptPresentation.Slides;
slide = slides.AddSlide(1, customLayout);

// Add title
objText = slide.Shapes[1].TextFrame.TextRange;
objText.Text = "FPPT.com";
objText.Font.Name = "Arial";
objText.Font.Size = 32;

objText = slide.Shapes[2].TextFrame.TextRange;
objText.Text = "Content goes here\nYou can add text\nItem 3";

slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = "This demo is created by FPPT using C# - Download free templates from http://FPPT.com";

pptPresentation.SaveAs(@"c:\temp\fppt.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
//pptPresentation.Close();
//pptApplication.Quit();

 

Now, we will explain what this snippet does.

First, we obtain the Presentation object from the Interop library, which will help us to access PowerPoint and other Microsoft Office programs. Of course to make this work you will need to have Office installed and the Interop libraries referenced in the application.

Then, we will create a Custom Layout using the pptLayoutText that has the presentation title and content. It is the default layout that we can find when we open PowerPoint.

Now we get the slides array and add a new slide object. This will be inserted as the slide #1.

Next, we get the title shape and write the presentation title.

We will do the same with the presentation content. Notice that here we are inserting a bullet list (separated by \n) using C#. This can be helpful to insert a list of items, but if you don’t want to insert a bullet list you can just insert the plain text.

Then, will add a simple comment into the speaker notes to demonstrate that it is also possible to insert notes.

And finally we will need to save the presentation as an output file, unless you want to keep it opened in your window. Notice that to choose if the window should be opened or not, it is defined in the

example add slide csharp powerpoint interop

Here is the source code snippet from Snippet.io

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

One comment on “Create PowerPoint .PPT programmatically using C#

Comments are closed.