每个PowerPoint演示文稿都有一个幻灯片母版,它包含幻灯片的所有样式。通过选择幻灯片母版,然后采用主题,添加背景图片或更改配色方案,可以快速改变整个演示文稿的外观。
在本文中,您将学习如何获取和修改现有演示文稿中的幻灯片母版。
测试文档:
C#
//加载PDF文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx");
//获取文档中的母版
IMasterSlide masterSlide = ppt.Masters[0];
//在母版上添加背景图片
string backgroundPic = "background.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;
//更改母版主题颜色
masterSlide.Theme.ColorScheme.Accent1.Color = Color.Red;
masterSlide.Theme.ColorScheme.Accent2.Color = Color.RosyBrown;
masterSlide.Theme.ColorScheme.Accent3.Color = Color.Ivory;
masterSlide.Theme.ColorScheme.Accent4.Color = Color.Lavender;
masterSlide.Theme.ColorScheme.Accent5.Color = Color.Black;
//在母版上添加图片
string logo = "logo.png";
IEmbedImage imageShape = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logo, new RectangleF(40, 40, 240, 65));
imageShape.Line.FillFormat.FillType = FillFormatType.None;
//保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
VB.NET
'加载PDF文档
Dim ppt As New Presentation()
ppt.LoadFromFile("sample.pptx")
'获取文档中的母版
Dim masterSlide As IMasterSlide = ppt.Masters(0)
'在母版上添加背景图片
Dim backgroundPic As String = "background.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)
'更改母版主题颜色
masterSlide.Theme.ColorScheme.Accent1.Color = Color.Red
masterSlide.Theme.ColorScheme.Accent2.Color = Color.RosyBrown
masterSlide.Theme.ColorScheme.Accent3.Color = Color.Ivory
masterSlide.Theme.ColorScheme.Accent4.Color = Color.Lavender
masterSlide.Theme.ColorScheme.Accent5.Color = Color.Black
'在母版上添加图片
Dim logo As String = "logo.png"
Dim imageShape As IEmbedImage = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logo, New RectangleF(40, 40, 240, 65))
imageShape.Line.FillFormat.FillType = FillFormatType.None
'保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)
结果文档: