PowerPoint 演示文稿是我们常用的办公软件之一。为了能够更加全面细致地展示要说明的内容,视频不失为一种很好的展示方式。本文将使用Java代码来演示如何插入视频到PowerPoint幻灯片,同时也将介绍提取视频以及设置视频播放模式的方法。
方式一:在E-iceblue中文官网上下载产品包,解压后在lib文件夹下找到Spire.Presentation.jar,然后手动将其导入IDEA。
方式二:在IDEA中创建Maven项目,然后在pom.xml下键入以下代码,最后点击“Import Changes”即可。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
示例一 插入视频到PowerPoint幻灯片
import com.spire.presentation.*;
import javax.imageio.ImageIO;
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 {
//加载PowerPoint示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\Users\Test1\Desktop\Sample.pptx");
//获取第一张幻灯片
ISlide slide = presentation.getSlides().get(0);
//插入视频到第一张幻灯片并设置封面图片
Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225);
IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:\Users\Test1\Desktop\video.mp4")).getAbsolutePath(), videoRect);
BufferedImage coverImage = ImageIO.read( new File("C:\Users\Test1\Desktop\image.png"));
video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));
//保存结果文档
presentation.saveToFile("output/InsertVideo.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}
效果图:
示例二 提取PowerPoint幻灯片中已有的视频
import com.spire.presentation.IShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.IVideo;
import com.spire.presentation.Presentation;
public class ExtractVideo {
public static void main(String[] args) throws Exception {
//实例化一个ppt对象并加载示例文档
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\Users\Test1\Desktop\InsertVideo.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("output/ExtractVideo"+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("C:\Users\Test1\Desktop\InsertVideo.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("output/单击播放.pptx", FileFormat.PPTX_2010);
}
}
设置效果: