Insert Video in PowerPoint in Java

This article demonstrates how to insert a video file (.mp4) in a presentation slide by using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class InsertVideo {

    public static void main(String[] args) throws Exception {

        //create a Presentation object and load an example PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

        //add a shape to the first slide
        Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 100, 50);
        IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
        labelShape.getLine().setFillType(FillFormatType.NONE);
        labelShape.getFill().setFillType(FillFormatType.NONE);
        labelShape.getTextFrame().setText("Video:");
        labelShape.getTextFrame().getTextRange().setFontHeight(28);
        labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
        labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

        //append a video file to the slide and set the cover image
        Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225);
        IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:/Users/Administrator/Desktop/video.mp4")).getAbsolutePath(), videoRect);
        BufferedImage coverImage = ImageIO.read( new File("C:/Users/Administrator/Desktop/coverImage.jpg"));
        video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));

        //save to file
        presentation.saveToFile("output/InsertVideo.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Insert Video in PowerPoint in Java