How to Create a PowerPoint Presentation using C# and Embed a Picture to the Slide
Recently we published a post about how to use C# to programmatically create a PPT presentation. This may be useful for developers who want to create or convert presentations to other formats, or to create reports on the fly. Very useful for organizations and IT departments that need to generate PPT presentations for their employees, but also for entrepreneurs who want to make applications and projects with capabilities to generate PPT presentations from the code.
Today we will show you how easily it is to embed a picture to a PPT slide using C#. In order to do this we will use the slide.Shapes.AddPicture function.
string pictureFileName = "C:\\temp\\example.jpg"; 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"; Microsoft.Office.Interop.PowerPoint.Shape shape = slide.Shapes[2]; slide.Shapes.AddPicture(pictureFileName,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue,shape.Left, shape.Top, shape.Width, shape.Height); 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();
Here is an example about how the final output PPT looks with the picture inserted from SharePoint post.