本文将展示如何使用Spire.Presenation 组件对PowerPoint幻灯片进行合并与拆分。
将多个PowerPoint文件合并成一个PowerPoint文件。该部分将介绍两种合并方式:加载多个独立的PowerPoint文件,同时将数据合并后得到一个新的PPT文件。第二种方法将第二个幻灯片中的数据,写入到第一个幻灯片,保存为新的PowerPoint文件。
将多个PPT文件合并为一个PPT文件
C#
//创建新的Presentation示例并删除默认的空白幻灯片
Presentation ppt = new Presentation();
ppt.Slides.RemoveAt(0);
//加载需要合并的两个幻灯片文档
Presentation ppt1 = new Presentation("Sample.pptx", FileFormat.Pptx2010);
Presentation ppt2 = new Presentation("Sample2.pptx", FileFormat.Pptx2010);
//插入第一个幻灯片数据
for (int i = 0; i < ppt1.Slides.Count; i++)
{
ppt.Slides.Append(ppt1.Slides[i]);
}
//插入第二个幻灯片数据
ppt.Slides.Append(ppt2.Slides[0]);
//保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
VB.NET
'创建新的Presentation示例并删除默认的空白幻灯片
Dim ppt As New Presentation()
ppt.Slides.RemoveAt(0)
'加载需要合并的两个幻灯片文档
Dim ppt1 As New Presentation("Sample.pptx", FileFormat.Pptx2010)
Dim ppt2 As New Presentation("Sample2.pptx", FileFormat.Pptx2010)
'插入第一个幻灯片数据
For i As Integer = 0 To ppt1.Slides.Count - 1
ppt.Slides.Append(ppt1.Slides(i))
Next
'插入第二个幻灯片数据
ppt.Slides.Append(ppt2.Slides(0))
'保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)
合并前:
合并后:
复制第一个幻灯片中的数据,插入到第二个幻灯片,保存为新的幻灯片。
C#
//加载一个PowerPoint 示例文档并获取第一张Slide
Presentation ppt1 = new Presentation("Sample2.pptx", FileFormat.Pptx2010);
ISlide slide = ppt1.Slides[0];
//加载第二个PowerPoint示例文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);
//将第一个文档中获取的第一个幻灯片数据插入到第二个幻灯片数据中,并设置为第一页
int index = 0;
ppt.Slides.Insert(index, slide);
//保存文档
ppt.SaveToFile("Merge2.pptx", FileFormat.Pptx2010);
VB.NET
'加载一个PowerPoint 示例文档并获取第一张Slide
Dim ppt1 As New Presentation("Sample2.pptx", FileFormat.Pptx2010)
Dim slide As ISlide = ppt1.Slides(0)
'加载第二个PowerPoint示例文档
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010)
'将第一个文档中获取的第一个幻灯片数据插入到第二个幻灯片数据中,并设置为第一页
Dim index As Integer = 0
ppt.Slides.Insert(index, slide)
'保存文档
ppt.SaveToFile("Merge2.pptx", FileFormat.Pptx2010)
合并前:
合并后:
拆分幻灯片:该部分将介绍将PowerPoint 幻灯片按每一个幻灯片拆分或按指定幻灯片页码范围拆分。
按每一个幻灯片拆分:
C#
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
for (int i = 0; i < ppt.Slides.Count; i++)
{
Presentation newppt = new Presentation();
newppt.Slides.RemoveAt(0);
newppt.Slides.Append(ppt.Slides[i]);
newppt.SaveToFile(String.Format("Split{0}.pptx", i), FileFormat.Pptx2010);
}
VB.NET
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx")
For i As Integer = 0 To ppt.Slides.Count - 1
Dim newppt As New Presentation()
newppt.Slides.RemoveAt(0)
newppt.Slides.Append(ppt.Slides(i))
newppt.SaveToFile([String].Format("Split{0}.pptx", i), FileFormat.Pptx2010)
Next
拆分前:
拆分后:
按指定幻灯片页码范围拆分:
C#
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
Presentation ppt1 = new Presentation();
ppt1.Slides.RemoveAt(0);
for (int i = 0; i < 2; i++)
{
ppt1.Slides.Append(ppt.Slides[i]);
}
ppt1.SaveToFile(String.Format("Split1.pptx"), FileFormat.Pptx2010);
Presentation ppt2 = new Presentation();
ppt2.Slides.RemoveAt(0);
ppt2.Slides.Append(ppt.Slides[2]);
ppt2.SaveToFile(String.Format("Split2.pptx"), FileFormat.Pptx2010);
VB.NET
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx")
Dim ppt1 As New Presentation()
ppt1.Slides.RemoveAt(0)
For i As Integer = 0 To 1
ppt1.Slides.Append(ppt.Slides(i))
Next
ppt1.SaveToFile([String].Format("Split1.pptx"), FileFormat.Pptx2010)
Dim ppt2 As New Presentation()
ppt2.Slides.RemoveAt(0)
ppt2.Slides.Append(ppt.Slides(2))
ppt2.SaveToFile([String].Format("Split2.pptx"), FileFormat.Pptx2010)
拆分前:
拆分后: