本文将展示如何使用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)
拆分前:
拆分后: