本文将介绍怎么使用spire.presentation 实现获取和设置powerpoint中幻灯片的标题。
获取标题
c#
//加载文档
presentation ppt = new presentation();
ppt.loadfromfile(diag.filename);
list shapelist = new list();
//遍历每页幻灯片,获取标题
foreach (islide slide in ppt.slides)
{
foreach (ishape shape in slide.shapes)
{
if (shape.placeholder != null)
{
switch (shape.placeholder.type)
{
case placeholdertype.title:
shapelist.add(shape);
break;
case placeholdertype.centeredtitle:
shapelist.add(shape);
break;
case placeholdertype.subtitle:
shapelist.add(shape);
break;
}
}
}
}
//将标题写入到字符串变量
stringbuilder stringbuilder = new stringbuilder();
for (int i = 0; i < shapelist.count; i )
{
iautoshape shape1 = shapelist[i] as iautoshape;
stringbuilder.appendline(shape1.textframe.text);
}
//将字符串内容写入到txt文件
file.writealltext("titles.txt", stringbuilder.tostring());
vb.net
'加载文档
dim ppt as new presentation()
ppt.loadfromfile(diag.filename)
dim shapelist as new list()
'遍历每页幻灯片,获取标题
for each slide as islide in ppt.slides
for each shape as ishape in slide.shapes
if shape.placeholder isnot nothing then
select case shape.placeholder.type
case placeholdertype.title
shapelist.add(shape)
exit select
case placeholdertype.centeredtitle
shapelist.add(shape)
exit select
case placeholdertype.subtitle
shapelist.add(shape)
exit select
end select
end if
next
next
'将标题写入到字符串变量
dim stringbuilder as new stringbuilder()
for i as integer = 0 to shapelist.count - 1
dim shape1 as iautoshape = trycast(shapelist(i), iautoshape)
stringbuilder.appendline(shape1.textframe.text)
next
'将字符串内容写入到txt文件
file.writealltext("titles.txt", stringbuilder.tostring())
效果如下:
更改标题
依旧使用同一个原文档。
c#
//加载文档
presentation ppt = new presentation();
ppt.loadfromfile(diag.filename);
//更改第一张幻灯片中的主标题和副标题
foreach (ishape shape in ppt.slides[0].shapes)
{
if (shape.placeholder != null)
{
switch (shape.placeholder.type)
{
//找到主标题,更改内容
case placeholdertype.title:
case placeholdertype.centeredtitle:
(shape as iautoshape).textframe.text = "更改后的主标题";
break;
//找到副标题,更改内容
case placeholdertype.subtitle:
(shape as iautoshape).textframe.text = "更改后的副标题";
break;
}
}
}
//保存新文档到本地
ppt.savetofile("changetitle.pptx", fileformat.pptx2010);
vb.net
'加载文档
dim ppt as new presentation()
ppt.loadfromfile(diag.filename)
'更改第一张幻灯片中的主标题和副标题
for each shape as ishape in ppt.slides(0).shapes
if shape.placeholder isnot nothing then
select case shape.placeholder.type
'找到主标题,更改内容
case placeholdertype.title, placeholdertype.centeredtitle
trycast(shape, iautoshape).textframe.text = "更改后的主标题"
exit select
'找到副标题,更改内容
case placeholdertype.subtitle
trycast(shape, iautoshape).textframe.text = "更改后的副标题"
exit select
end select
end if
next
'保存新文档到本地
ppt.savetofile("changetitle.pptx", fileformat.pptx2010)
效果如下: