从spire.presentation 4.1.16版本开始,支持复制幻灯片中的形状,因此本篇文章将介绍如何复制形状的方法。包括复制幻灯片中的指定形状、所有形状。
复制指定形状
c#
//创建presentation对象,加载测试文档
presentation presentation = new presentation();
presentation.loadfromfile("test.pptx");
//指定形状所在幻灯片(第一张幻灯片)
islide sourceslide = presentation.slides[0];
//指定目标幻灯片(第二张幻灯片)
islide targetslide = presentation.slides[1];
//将第一张幻灯片中的第1个形状复制到第二张幻灯片
targetslide.shapes.addshape((shape)sourceslide.shapes[0]);
//保存文档
presentation.savetofile("result.pptx", fileformat.pptx2013);
vb.net
'创建presentation对象,加载测试文档
dim presentation as new presentation()
presentation.loadfromfile("test.pptx")
'指定形状所在幻灯片(第一张幻灯片)
dim sourceslide as islide = presentation.slides(0)
'指定目标幻灯片(第二张幻灯片)
dim targetslide as islide = presentation.slides(1)
'将第一张幻灯片中的第1个形状复制到第二张幻灯片
targetslide.shapes.addshape(directcast(sourceslide.shapes(0), shape))
'保存文档
presentation.savetofile("result.pptx", fileformat.pptx2013)
复制效果:
复制所有形状
c#
//创建presentation对象,并加载测试文档
presentation ppt = new presentation();
ppt.loadfromfile("sample.pptx");
//将指定幻灯片中的所有形状复制到目标幻灯片
for (int i = 0; i < ppt.slides.count - 1; i )
{
islide sourceslide = ppt.slides[i];
islide targetslide = ppt.slides[ppt.slides.count - 1];
for (int j = 0; j < sourceslide.shapes.count; j )
{
targetslide.shapes.addshape((shape)sourceslide.shapes[j]);
}
}
//保存文档
ppt.savetofile("output.pptx", fileformat.pptx2010);
vb.net
'创建presentation对象,并加载测试文档
dim ppt as new presentation()
ppt.loadfromfile("sample.pptx")
'将指定幻灯片中的所有形状复制到目标幻灯片
for i as integer = 0 to ppt.slides.count - 2
dim sourceslide as islide = ppt.slides(i)
dim targetslide as islide = ppt.slides(ppt.slides.count - 1)
for j as integer = 0 to sourceslide.shapes.count - 1
targetslide.shapes.addshape(directcast(sourceslide.shapes(j), shape))
next
next
'保存文档
ppt.savetofile("output.pptx", fileformat.pptx2010)
复制效果: