当您想在一个PPT中使用两种或两种以上的背景时,可以通过创建多个幻灯片母版来实现。在本文中,您将学习如何使用Spire.Presentation创建额外的幻灯片母版并将它们应用到不同的幻灯片。
C#
//新建PPT文档
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//插入4页幻灯片(连同默认的幻灯片,文档中共5页)
for (int i = 0; i < 4; i++)
{
ppt.Slides.Append();
}
//获取默认的母版
IMasterSlide first_master = ppt.Masters[0];
//新建并获取第二个母板
ppt.Masters.AppendSlide(first_master);
IMasterSlide second_master = ppt.Masters[1];
//为两个母版分别设置不同的背景图片
string pic1 = @"C:\Users\Administrator\Desktop\image-1.png";
string pic2 = @"C:\Users\Administrator\Desktop\image-2.png";
RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);
first_master.SlideBackground.Fill.FillType = FillFormatType.Picture;
IEmbedImage image1 = first_master.Shapes.AppendEmbedImage(ShapeType.Rectangle, pic1, rect);
first_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image1 as IImageData;
second_master.SlideBackground.Fill.FillType = FillFormatType.Picture;
IEmbedImage image2 = second_master.Shapes.AppendEmbedImage(ShapeType.Rectangle, pic2, rect);
second_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image2 as IImageData;
//在第一页应用第一个母版及版式(如果不希望出现板式,请选择板式6)
ppt.Slides[0].Layout = first_master.Layouts[1];
//在剩下的幻灯片应用第二个母版及版式
for (int i = 1; i < ppt.Slides.Count; i++)
{
ppt.Slides[i].Layout = second_master.Layouts[8];
}
//保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
VB.NET
'新建PPT文档
Dim ppt As New Presentation()
ppt.SlideSize.Type = SlideSizeType.Screen16x9
'插入4页幻灯片(连同默认的幻灯片,文档中共5页)
For i As Integer = 0 To 3
ppt.Slides.Append()
Next
'获取默认的母版
Dim first_master As IMasterSlide = ppt.Masters(0)
'新建并获取第二个母板
ppt.Masters.AppendSlide(first_master)
Dim second_master As IMasterSlide = ppt.Masters(1)
'为两个母版分别设置不同的背景图片
Dim pic1 As String = "C:\Users\Administrator\Desktop\image-1.png"
Dim pic2 As String = "C:\Users\Administrator\Desktop\image-2.png"
Dim rect As New RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height)
first_master.SlideBackground.Fill.FillType = FillFormatType.Picture
Dim image1 As IEmbedImage = first_master.Shapes.AppendEmbedImage(ShapeType.Rectangle, pic1, rect)
first_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = TryCast(image1, IImageData)
second_master.SlideBackground.Fill.FillType = FillFormatType.Picture
Dim image2 As IEmbedImage = second_master.Shapes.AppendEmbedImage(ShapeType.Rectangle, pic2, rect)
second_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = TryCast(image2, IImageData)
'在第一页应用第一个母版及版式(如果不希望出现板式,请选择板式6)
ppt.Slides(0).Layout = first_master.Layouts(1)
'在剩下的幻灯片应用第二个母版及版式
For i As Integer = 1 To ppt.Slides.Count - 1
ppt.Slides(i).Layout = second_master.Layouts(8)
Next
'保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)