In this topic, we will exhibit how to add video in Presentation using C# along with the steps to set up the environment to use the example code. You can use the application in any .NET configured environments inside Linux, Windows or macOS and with no dependence any third-party software or an Interop library to embed video in PPTX using C#.
Steps to Insert Video in Presentation using C#
- Configure the application to install Aspose.Slides for .NET package from NuGet to add a video frame
- Create an empty presentation using the Presentation class object to embed a video inside the presentation
- Access the first slide inside the presentation slides to add a video frame
- Load the video file and add inside the presentation slide video frame
- Set the video frame properties for volume and auto play
- Save the presentation with an embedded video on the disk
By using the above steps in C# inserting video in PowerPoint presentation can be easily managed with the help of simple API calls, whereby the process is initiated by creating a default presentation using the Presentation class and getting access to first slide inside the presentation. Then the source video file is loaded from the disk and added inside the video frame shape for the slide. Finally, the video frame properties for auto playing and audio level are set before saving the presentation with an embedded video on the disk.
Code to Insert Video in Presentation using C#
using System.IO; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace TestSlides | |
{ | |
public class InsertVideo | |
{ | |
public static void AddVideo() | |
{ | |
string filesPath = @"/Users/Documents/KnowledgeBase/TestData/"; | |
//Set the API license to insert the video inside the presentation | |
License license = new License(); | |
license.SetLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Create a new presentation to add new video inside the slide | |
Presentation SrcPresWithVideo = new Presentation(); | |
//Access the slide to include the video | |
ISlide VideoSlide = SrcPresWithVideo.Slides[0]; | |
// Embed the video inside the presentation media collection | |
IVideo Video = SrcPresWithVideo.Videos.AddVideo(new FileStream(filesPath+ "SampleVideo.mp4", FileMode.Open)); | |
// Insert the Video Frame inside the slide | |
IVideoFrame VideoFrm = VideoSlide.Shapes.AddVideoFrame(0, 0, 720, 540, Video); | |
// Embed the video inside the Video Frame | |
VideoFrm.EmbeddedVideo = Video; | |
// Set the options of play mode and volume of the video | |
VideoFrm.PlayMode = VideoPlayModePreset.Auto; | |
VideoFrm.Volume = AudioVolumeMode.Loud; | |
// Save the presentation with video on the disk | |
SrcPresWithVideo.Save("VideoFrame_out.pptx", SaveFormat.Pptx); | |
} | |
} | |
} |
In C# saving MP4 video in PPT presentation can be easily managed using a very simple API calls as specified in above example. We have used IVideoFrame that also let you set the properties like rewind mode, play mode, play video in a loop and hiding a video to name a few. You can also use an existing presentation to embed a video inside the presentation slide. Once the video will be embedded the presentation can either be saved on the disk or inside a memory stream for web based application use as well.
This tutorial focuses on how to insert video in Presentation using C#. If you want to learn about hiding a slide inside the presentation, refer to the article on how to hide a slide in presentation using C#.