从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)
复制效果: