在使用c#创建powerpoint文档的过程中,我们首先需要添加一个shape图形到幻灯片文档,然后在图形中添加多个段落并设置段落格式。该示例将详细描述如何使用spire.presentation 添加或删除powerpoint段落。
添加段落到幻灯片
c#
//新建一个powerpoint文档并获取第一个幻灯片
presentation presentation = new presentation();
islide slide = presentation.slides[0];
//添加一个shape到第一张幻灯片并设置其大小,颜色
iautoshape shape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(50, 70, 600, 200));
shape.fill.filltype = fillformattype.none;
shape.shapestyle.linecolor.color = color.white;
//在图形上添加第一个段落及文字
shape.textframe.paragraphs[0].textranges.append(new textrange("使用代码添加段落到幻灯片"));
//在图形上添加第二个段落及文字
string text = "一款专业的 powerpoint 组件,使用该组件,开发者可以在 .net 平台上对 powerpoint 文档进行生成、读取、写入、修改、转换和打印等操作。";
textparagraph p=new textparagraph();
p.text=text;
shape.textframe.paragraphs.append(p);
//设置段落中文字的字体与颜色
foreach (textparagraph para in shape.textframe.paragraphs)
{
foreach (textrange range in para.textranges)
{
range.latinfont = new textfont("宋体");
range.fill.filltype = fillformattype.solid;
range.fill.solidcolor.color = color.black;
}
//设置段落对齐方式、段首缩进及行距
para.alignment = textalignmenttype.left;
para.indent = 50;
para.linespacing = 150;
}
//保存文档
presentation.savetofile("result.pptx", fileformat.pptx2010);
vb.net
'新建一个powerpoint文档并获取第一个幻灯片
dim presentation as new presentation()
dim slide as islide = presentation.slides(0)
'添加一个shape到第一张幻灯片并设置其大小,颜色
dim shape as iautoshape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(50, 70, 600, 200))
shape.fill.filltype = fillformattype.none
shape.shapestyle.linecolor.color = color.white
'在图形上添加第一个段落及文字
shape.textframe.paragraphs(0).textranges.append(new textrange("使用代码添加段落到幻灯片"))
'在图形上添加第二个段落及文字
dim text as string = "一款专业的 powerpoint 组件,使用该组件,开发者可以在 .net 平台上对 powerpoint 文档进行生成、读取、写入、修改、转换和打印等操作。"
dim p as new textparagraph()
p.text = text
shape.textframe.paragraphs.append(p)
'设置段落中文字的字体与颜色
for each para as textparagraph in shape.textframe.paragraphs
for each range as textrange in para.textranges
range.latinfont = new textfont("宋体")
range.fill.filltype = fillformattype.solid
range.fill.solidcolor.color = color.black
next
'设置段落对齐方式、段首缩进及行距
para.alignment = textalignmenttype.left
para.indent = 50
para.linespacing = 150
next
'保存文档
presentation.savetofile("result.pptx", fileformat.pptx2010)
添加段落和设置段落格式效果图:
删除段落
c#
//加载文档
presentation presentation = new presentation();
presentation.loadfromfile("result.pptx",fileformat.pptx2010);
//获取第一张幻灯片
islide slide = presentation.slides[0];
//获取第一个图形
iautoshape shape = slide.shapes[0] as iautoshape;
//删除图形里的第二个段落
shape.textframe.paragraphs.removeat(1);
//保存文档
presentation.savetofile("removepara.pptx", fileformat.pptx2010);
vb.net
'加载文档
dim presentation as new presentation()
presentation.loadfromfile("result.pptx", fileformat.pptx2010)
'获取第一张幻灯片
dim slide as islide = presentation.slides(0)
'获取第一个图形
dim shape as iautoshape = trycast(slide.shapes(0), iautoshape)
'删除图形里的第二个段落
shape.textframe.paragraphs.removeat(1)
'保存文档
presentation.savetofile("removepara.pptx", fileformat.pptx2010)