Java 添加 、读取以及删除
在
程序环境:导入Spire.Presentation.jar(免费版) ; jdk1.8.0
jar文件导入效果入下:
1. 添加视频、音频文件到幻灯片
import com.spire.presentation.*;
import
2. 获取幻灯片中的视频、音频文件
import com.spire.presentation.*;
public class ExtractVideoAudio {
public static void main(String[]args) throws Exception{
//加载包含视频、音频文件的PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("result.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
IVideo video = null;
IAudio audio = null;
//遍历幻灯片中的shape,判断是否包含视频
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("提取的视频" + i + ".mp4");
}
}
//遍历幻灯片中的shape,判断是否包含音频
for(int j = 0; j< slide.getShapes().getCount(); j++)
{
IShape shape = slide.getShapes().get(j);
//保存音频
if ((shape instanceof IAudio)){
audio =(IAudio) shape;
audio.getData().saveToFile("提取的音频"+ j +".mp3");
}
}
}
}
3. 删除幻灯片中的视频、音频文件
import com.spire.presentation.*;
public class DeleteVideoAndAudio {
public static void main(String[] args) throws Exception{
//加载包含视频、音频文件的PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("result.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//遍历幻灯片中的shape,判断是否包含视频
for(int i = 0; i< slide.getShapes().getCount(); i++) {
IShape shape = slide.getShapes().get(i);
if ((shape instanceof IVideo)) {
//删除视频
IVideo video = (IVideo) shape;
slide.getShapes().remove(video);
}
}
//遍历幻灯片中的shape,判断是否包含音频
for(int j = 0; j< slide.getShapes().getCount(); j++)
{
IShape shape = slide.getShapes().get(j);
//删除音频
if ((shape instanceof IAudio)){
IAudio audio = (IAudio) shape;
slide.getShapes().remove(audio);
}
}
//保存文档
ppt.saveToFile("DeleteVideoAndAudio.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}