当您希望所有幻灯片都包含相同的背景、图片(如公司logo)及文字(如公司名称)时,您可以在幻灯片母版上设置这些内容,母版的样式将自动应用到所有幻灯片中。本文介绍如何使用spire.presentation创建自定义的幻灯片母版。
c#
//创建ppt文档,指定幻灯片大小
presentation ppt = new presentation();
ppt.slidesize.type = slidesizetype.screen16x9;
//获取第一张母版
imasterslide masterslide = ppt.masters[0];
//获取图片地址
string backgroundpic = "background-1.png";
string logo = "logo.png";
//设置母版背景
rectanglef rect = new rectanglef(0, 0, ppt.slidesize.size.width, ppt.slidesize.size.height);
masterslide.slidebackground.fill.filltype = fillformattype.picture;
iembedimage image = masterslide.shapes.appendembedimage(shapetype.rectangle, backgroundpic, rect);
masterslide.slidebackground.fill.picturefill.picture.embedimage = image as iimagedata;
//添加图片(公司logo)到母版
iembedimage imageshape = masterslide.shapes.appendembedimage(shapetype.rectangle, logo, new rectanglef(40, 40, 200, 60));
imageshape.line.fillformat.filltype = fillformattype.none;
//添加文字(公司名称)到母版
iautoshape textshape = masterslide.shapes.appendshape(shapetype.rectangle, new rectanglef(ppt.slidesize.size.width-200, ppt.slidesize.size.height-60, 200, 30));
textshape.textframe.text = "成都冰蓝科技有限公司";
textshape.textframe.textrange.fontheight = 15f;
textshape.textframe.textrange.fill.filltype = fillformattype.solid;
textshape.textframe.textrange.fill.solidcolor.color = color.royalblue;
textshape.textframe.textrange.paragraph.alignment = textalignmenttype.center;
textshape.fill.filltype = fillformattype.none;
textshape.line.filltype = fillformattype.none;
//添加一张幻灯片
ppt.slides.append();
//保存文档
ppt.savetofile("result.pptx", fileformat.pptx2013);
vb.net
'创建ppt文档,指定幻灯片大小
dim ppt as new presentation()
ppt.slidesize.type = slidesizetype.screen16x9
'获取第一张母版
dim masterslide as imasterslide = ppt.masters(0)
'获取图片地址
dim backgroundpic as string = "background-1.png"
dim logo as string = "logo.png"
'设置母版背景
dim rect as new rectanglef(0, 0, ppt.slidesize.size.width, ppt.slidesize.size.height)
masterslide.slidebackground.fill.filltype = fillformattype.picture
dim image as iembedimage = masterslide.shapes.appendembedimage(shapetype.rectangle, backgroundpic, rect)
masterslide.slidebackground.fill.picturefill.picture.embedimage = trycast(image, iimagedata)
'添加图片(公司logo)到母版
dim imageshape as iembedimage = masterslide.shapes.appendembedimage(shapetype.rectangle, logo, new rectanglef(40, 40, 200, 60))
imageshape.line.fillformat.filltype = fillformattype.none
'添加文字(公司名称)到母版
dim textshape as iautoshape = masterslide.shapes.appendshape(shapetype.rectangle, new rectanglef(ppt.slidesize.size.width - 200, ppt.slidesize.size.height - 60, 200, 30))
textshape.textframe.text = "成都冰蓝科技有限公司"
textshape.textframe.textrange.fontheight = 15f
textshape.textframe.textrange.fill.filltype = fillformattype.solid
textshape.textframe.textrange.fill.solidcolor.color = color.royalblue
textshape.textframe.textrange.paragraph.alignment = textalignmenttype.center
textshape.fill.filltype = fillformattype.none
textshape.line.filltype = fillformattype.none
'添加一张幻灯片
ppt.slides.append()
'保存文档
ppt.savetofile("result.pptx", fileformat.pptx2013)
如下图所示,在第二张幻灯片上自动应用了母版样式: