Apply a Slide Master to a Presentation in C#, VB.NET

Every PowerPoint presentation has a slide master which contains all the styles for your slides. You can quickly change the look of your entire presentation by selecting the slide master, and then adopting a theme, adding a background picture or changing the color scheme.

In this article, you will learn how to access and customize the slide master in an existing presentation.

Source File:

Apply a Slide Master to a Presentation in 

C#, VB.NET

Detail steps:

Step 1: Load the source file.

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"sample.pptx");

Step 2: Get the first slide master from the presentation.

IMasterSlide masterSlide = ppt.Masters[0];

Step 3: Customize the background of the slide master.

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;

Step 4: Change the color scheme.

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;

Step 5: Add an image to the slide master. If you want, you can add any other document elements to slide master so that they can display on each slide.

string logo = "logo.png";
IEmbedImage imageShape = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logo, new RectangleF(40, 40, 240, 65));
imageShape.Line.FillFormat.FillType = FillFormatType.None;

Step 6: Save the document.

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

Result:

Apply a Slide Master to a Presentation in C#, VB.NET

Full code:

[C#]
Presentation ppt = new Presentation();
ppt.LoadFromFile(@"sample.pptx");

IMasterSlide masterSlide = ppt.Masters[0];
string backgroundPic = "background.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;
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;
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]
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("sample.pptx")
Dim masterSlide As IMasterSlide = ppt.Masters(0)
Dim backgroundPic As String = "background.png"
Dim logo As String = "logo.png"
Dim rect As RectangleF =  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 = 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
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)