当您希望所有幻灯片都包含相同的背景、图片(如公司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)
如下图所示,在第二张幻灯片上自动应用了母版样式: