本文将介绍如何使用Spire.Presentation for Java组件在PowerPoint文档中插入视频、提取视频以及设置视频的播放模式。
插入视频
import java.awt.Rectangle;
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
public class InsertRadio {
public static void main(String[] args) throws Exception {
//实例化一个Presentation对象
Presentation ppt = new Presentation();
ppt.loadFromFile("E:\\演示文稿.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//插入视频到第一张幻灯片
slide.getShapes().appendVideoMedia("E:\\视频.mp4", new Rectangle(300, 150, 300, 150));
//保存文件
ppt.saveToFile("E:\\插入视频.pptx", FileFormat.PPTX_2010);
}
}
提取视频
import com.spire.presentation.IShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.IVideo;
import com.spire.presentation.Presentation;
public class ExportVideo {
public static void main(String[] args) throws Exception {
//实例化一个ppt对象并加载示例文档
Presentation ppt = new Presentation();
ppt.loadFromFile("E:\\插入视频.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
IVideo video = null;
for(int i = 0; i< slide.getShapes().getCount(); i++)
{
IShape shape = slide.getShapes().get(i);
if ((shape instanceof IVideo)) {
//保存视频
video = (IVideo) shape;
video.getEmbeddedVideoData().saveToFile("E:\\视频\\提取视频"+i+".mp4");
}
}
}
}
设置视频播放模式
import com.spire.presentation.FileFormat;
import com.spire.presentation.IShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.IVideo;
import com.spire.presentation.Presentation;
import com.spire.presentation.VideoPlayMode;
public class PlayType {
public static void main(String[] args) throws Exception {
//实例化一个presentation对象并加载示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("E:\\插入视频.pptx");
//获取第一张幻灯片
ISlide slide = presentation.getSlides().get(0);
IVideo video = null;
for(int i = 0; i< slide.getShapes().getCount(); i++)
{
IShape shape = slide.getShapes().get(i);
if ((shape instanceof IVideo)) {
video = (IVideo) shape;
//设置视频的播放模式为自动播放
//video.setPlayMode(VideoPlayMode.AUTO);
//设置视频的播放模式为单击时播放
video.setPlayMode(VideoPlayMode.ON_CLICK);
}
}
//保存PPT文档
presentation.saveToFile("E:\\单击播放.pptx", FileFormat.PPTX_2010);
}
}