spire.presentation支持操作常规图形(矩形、三角形、圆形等),智能图形(smartart),图表类图形(chart)等,本文主要介绍如何在幻灯片中添加和删除常规图形。
添加图形
c#
//新建一个powerpoint文档
presentation ppt = new presentation();
ppt.slidesize.type = slidesizetype.screen16x9;
//获取第一张幻灯片
islide slide = ppt.slides[0];
//添加一个矩形并填充渐变颜色
iautoshape shape1 = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(25, 50, 300, 30));
shape1.fill.filltype = fillformattype.gradient;
shape1.fill.gradient.gradientstops.append(0, color.black);
shape1.fill.gradient.gradientstops.append(1, color.azure);
shape1.line.filltype = fillformattype.none;
//在矩形中添加格式化文本
shape1.appendtextframe("本教程展示了如何添加图形到幻灯片");
textrange textrange = (shape1 as iautoshape).textframe.textrange;
textrange.fontheight = 13;
textrange.latinfont = new textfont("arial");
textrange.fill.filltype = fillformattype.solid;
textrange.fill.solidcolor.color = color.black;
//添加一个椭圆并填充图片
iautoshape shape2 = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(25, 150, 100, 100));
string picpath = @"c:\users\jack\desktop\flag.png";
shape2.fill.filltype = fillformattype.picture;
shape2.fill.picturefill.picture.url = picpath;
shape2.fill.picturefill.filltype = picturefilltype.stretch;
shape2.line.filltype = fillformattype.none;
//添加一个三角形并填充固定颜色
iautoshape shape3 = slide.shapes.appendshape(shapetype.triangle, new rectanglef(200, 150, 100, 100));
shape3.fill.filltype = fillformattype.solid;
shape3.fill.solidcolor.color = color.cornflowerblue;
shape3.line.filltype = fillformattype.solid;
shape3.line.solidfillcolor.color = color.black;
//添加一个带箭头的直线
iautoshape shape4 = slide.shapes.appendshape(shapetype.line, new rectanglef(375, 150, 100, 100));
shape4.shapestyle.linecolor.color = color.red;
shape4.line.lineendtype = lineendtype.stealtharrow;
shape4.rotation =-90 ;
//保存文档
ppt.savetofile("添加图形.pptx", fileformat.pptx2010);
vb.net
'新建一个powerpoint文档
dim ppt as new presentation()
ppt.slidesize.type = slidesizetype.screen16x9
'获取第一张幻灯片
dim slide as islide = ppt.slides(0)
'添加一个矩形并填充渐变颜色
dim shape1 as iautoshape = slide.shapes.appendshape(shapetype.rectangle, new rectanglef(25, 50, 300, 30))
shape1.fill.filltype = fillformattype.gradient
shape1.fill.gradient.gradientstops.append(0, color.black)
shape1.fill.gradient.gradientstops.append(1, color.azure)
shape1.line.filltype = fillformattype.none
'在矩形中添加格式化文本
shape1.appendtextframe("本教程展示了如何添加图形到幻灯片")
dim textrange as textrange = trycast(shape1, iautoshape).textframe.textrange
textrange.fontheight = 13
textrange.latinfont = new textfont("arial")
textrange.fill.filltype = fillformattype.solid
textrange.fill.solidcolor.color = color.black
'添加一个椭圆并填充图片
dim shape2 as iautoshape = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(25, 150, 100, 100))
dim picpath as string = "c:\users\jack\desktop\flag.png"
shape2.fill.filltype = fillformattype.picture
shape2.fill.picturefill.picture.url = picpath
shape2.fill.picturefill.filltype = picturefilltype.stretch
shape2.line.filltype = fillformattype.none
'添加一个三角形并填充固定颜色
dim shape3 as iautoshape = slide.shapes.appendshape(shapetype.triangle, new rectanglef(200, 150, 100, 100))
shape3.fill.filltype = fillformattype.solid
shape3.fill.solidcolor.color = color.cornflowerblue
shape3.line.filltype = fillformattype.solid
shape3.line.solidfillcolor.color = color.black
'添加一个带箭头的直线
dim shape4 as iautoshape = slide.shapes.appendshape(shapetype.line, new rectanglef(375, 150, 100, 100))
shape4.shapestyle.linecolor.color = color.red
shape4.line.lineendtype = lineendtype.stealtharrow
shape4.rotation = -90
'保存文档
ppt.savetofile("添加图形.pptx", fileformat.pptx2010)
删除图形
c#
//初始化一个presentation实例
presentation ppt = new presentation();
//加载一个powerpoint文档
ppt.loadfromfile("添加图形.pptx");
//获取第一张幻灯片
islide slide = ppt.slides[0];
//删除幻灯片上的第三个图形
slide.shapes.removeat(2);
//保存文档
ppt.savetofile("删除图形.pptx", fileformat.pptx2010);
vb.net
'初始化一个presentation实例
dim ppt as new presentation()
'加载一个powerpoint文档
ppt.loadfromfile("添加图形.pptx")
'获取第一张幻灯片
dim slide as islide = ppt.slides(0)
'删除幻灯片上的第三个图形
slide.shapes.removeat(2)
'保存文档
ppt.savetofile("删除图形.pptx", fileformat.pptx2010)