本文介绍使用spire.presentation for .net来替换ppt已插入的视频、音频文件,在替换原有视频、音频文件时,可设置替换原有视频封面、音频形状颜色等。
c#
using spire.presentation;
using spire.presentation.collections;
using spire.presentation.drawing;
using system.drawing;
using system.io;
namespace replacevideo
{
class program
{
static void main(string[] args)
{
//加载ppt文档
presentation ppt = new presentation();
ppt.loadfromfile("test.pptx");
//获取视频、音频文件集合
videocollection videos = ppt.videos;
wavaudiocollection audios = ppt.wavaudios;
//遍历所有幻灯片
foreach (islide slide in ppt.slides)
{
//遍历幻灯片中的所有形状
foreach (shape shape in slide.shapes)
{
//替换视频文件
if (shape is ivideo)
{
ivideo video = shape as ivideo;
byte[] bts = file.readallbytes("newvideo.mp4");
videodata videodata = videos.append(bts);
video.embeddedvideodata = videodata;
image image = image.fromfile("jds.png");//加载图片
iimagedata coverimage = ppt.images.append(image);
video.embedimage = coverimage;//设置视频封面图片
}
//替换音频文件
if(shape is iaudio)
{
iaudio audio = shape as iaudio;
byte[] bts1 = file.readallbytes("newaudio.mp3");
iaudiodata audiodata = audios.append(bts1);
audio.data = audiodata;
audio.fill.solidcolor.color = color.greenyellow;
}
}
}
//保存文档
ppt.savetofile("result.pptx", fileformat.pptx2013);
system.diagnostics.process.start("result.pptx");
}
}
}
vb.net
imports spire.presentation
imports spire.presentation.collections
imports spire.presentation.drawing
imports system.drawing
imports system.io
namespace replacevideo
class program
private shared sub main(args as string())
'加载ppt文档
dim ppt as new presentation()
ppt.loadfromfile("test.pptx")
'获取视频、音频文件集合
dim videos as videocollection = ppt.videos
dim audios as wavaudiocollection = ppt.wavaudios
'遍历所有幻灯片
for each slide as islide in ppt.slides
'遍历幻灯片中的所有形状
for each shape as shape in slide.shapes
'替换视频文件
if typeof shape is ivideo then
dim video as ivideo = trycast(shape, ivideo)
dim bts as byte() = file.readallbytes("newvideo.mp4")
dim videodata as videodata = videos.append(bts)
video.embeddedvideodata = videodata
dim image__1 as image = image.fromfile("jds.png")
'加载图片
dim coverimage as iimagedata = ppt.images.append(image__1)
'设置视频封面图片
video.embedimage = coverimage
end if
'替换音频文件
if typeof shape is iaudio then
dim audio as iaudio = trycast(shape, iaudio)
dim bts1 as byte() = file.readallbytes("newaudio.mp3")
dim audiodata as iaudiodata = audios.append(bts1)
audio.data = audiodata
audio.fill.solidcolor.color = color.greenyellow
end if
next
next
'保存文档
ppt.savetofile("result.pptx", fileformat.pptx2013)
system.diagnostics.process.start("result.pptx")
end sub
end class
end namespace
替换效果: