Create Multiple Slide Maters and Apply Them to Individual Slides in C#, VB.NET

When you want to use multiple themes in one presentation, you’ll need multiple slide masters. In this article, you will learn how create additional slide masters and apply them to different slides, by using Spire.Presentation with C# and VB.NET.

Step 1: Create a PowerPoint document and insert four slides to it. There are five slides in total including the default slide.

Presentation ppt = new Presentation();
for (int i = 0; i < 4; i++)
{
    ppt.Slides.Append();
}

Step 2: Get the first default slide master.

IMasterSlide first_master = ppt.Masters[0];

Step 3: Append another slide master.

ppt.Masters.AppendSlide(first_master);
IMasterSlide second_master = ppt.Masters[1];

Step 4: Set different background image for the two slide masters.

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;

Step 5: Apply the first master with layout to the first slide.

ppt.Slides[0].Layout = first_master.Layouts[1];

Step 6: Apply the second master with layout to other slides.

for (int i = 1; i < ppt.Slides.Count; i++)
{
    ppt.Slides[i].Layout = second_master.Layouts[8];
}

Step 7: Save the file.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);

Output:

Create Multiple Slide Maters and Apply Them to Individual Slides in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
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;

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]
Dim ppt As Presentation = New Presentation()
ppt.SlideSize.Type = SlideSizeType.Screen16x9
For i As Integer = 0 To 4 - 1
    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 RectangleF =  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 = image1 as 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 = image2 as IImageData
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)