PowerPoint 演示文稿是我們常用的辦公軟體之一。為了能夠更加全面細緻地展示要說明的內容,視頻不失為一種很好的展示方式。本文將使用Java代碼來演示如何插入視頻到PowerPoint幻燈片,同時也將介紹提取視頻以及設置視頻播放模式的方法。
使用工具:Free Spire.Presentation for Java
測試環境:JDK 1.8.0、Intellij IDEA 2019
Jar包導入:
方式一:在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);
}
}
設置效果:

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/249927.html