Working with Slides in PowerPoint
27 Jul 202624 minutes to read
To quickly learn how to add, remove, resize, and customize PowerPoint slides, check out this video:
Adding a slide to a PowerPoint presentation
In a PowerPoint presentation, a slide is a container for elements such as shapes, images, charts, and text boxes. The slides may inherit the formatting and layout properties from their ‘Master’ and ‘Layout’ slides.
The following code example demonstrates how to add a blank slide to the Presentation. By default, the Slides.Add() overload adds a slide using the default layout of the master.
//Creates a PowerPoint instance
IPresentation pptxDoc = Presentation.Create();
//Adds a slide to the PowerPoint presentation
ISlide slide = pptxDoc.Slides.Add();
//Saves the Presentation to the file system.
pptxDoc.Save("Sample.pptx");
//Closes the Presentation instance
pptxDoc.Close();//Creates a PowerPoint instance
IPresentation pptxDoc = Presentation.Create();
//Adds a slide to the PowerPoint presentation
ISlide slide = pptxDoc.Slides.Add();
//Saves the Presentation to the file system.
pptxDoc.Save("Sample.pptx");
//Closes the Presentation instance
pptxDoc.Close();'Creates a PowerPoint instance
Dim pptxDoc As IPresentation = Presentation.Create()
'Adds a slide to the PowerPoint presentation
Dim slide As ISlide = pptxDoc.Slides.Add()
'Saves the Presentation to the file system.
pptxDoc.Save("Sample.pptx")
'Closes the Presentation instance
pptxDoc.Close()You can download a complete working sample from GitHub.
Create a slide with predefined LayoutSlide
The .NET PowerPoint Library supports the following predefined slide layout types to create a slide as equivalent to Microsoft PowerPoint:
- Blank
- Comparison
- Content with caption
- Picture with caption
- Section header
- Title
- Title and content
- Title and vertical text
- Title only
- Two content
- Vertical title and text
The following example demonstrates how to access a slide from the predefined blank slide layout type.
//Create a new instance of PowerPoint Presentation file
IPresentation pptxDoc = Presentation.Create();
//Add a slide of blank layout type
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Save the PowerPoint file
pptxDoc.Save("Sample.pptx");
//Close the PowerPoint presentation
pptxDoc.Close();//Create a PowerPoint presentation
IPresentation pptxDoc = Presentation.Create();
//Add a slide of blank layout type
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Save the PowerPoint file
pptxDoc.Save("Sample.pptx");
//Close the PowerPoint instance
pptxDoc.Close();'Create a PowerPoint file
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide of blank layout type
Dim slide1 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Save the PowerPoint file
pptxDoc.Save("Sample.pptx")
'Close the PowerPoint instance
pptxDoc.Close()You can download a complete working sample from GitHub.
The following code example demonstrates how to add a slide with all other predefined slide layout types.
//Create a new instance of PowerPoint Presentation file
IPresentation pptxDoc = Presentation.Create();
//Add a slide of Blank type
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a slide of comparison type
ISlide slide2 = pptxDoc.Slides.Add(SlideLayoutType.Comparison);
//Add a slide of ContentWithCaption type
ISlide slide3 = pptxDoc.Slides.Add(SlideLayoutType.ContentWithCaption);
//Add a slide of PictureWithCaption type
ISlide slide4 = pptxDoc.Slides.Add(SlideLayoutType.PictureWithCaption);
//Add a slide of SectionHeader type
ISlide slide5 = pptxDoc.Slides.Add(SlideLayoutType.SectionHeader);
//Add a slide of Title type
ISlide slide6 = pptxDoc.Slides.Add(SlideLayoutType.Title);
//Add a slide of TitleAndContent type
ISlide slide7 = pptxDoc.Slides.Add(SlideLayoutType.TitleAndContent);
//Add a slide of TitleAndVerticalText type
ISlide slide8 = pptxDoc.Slides.Add(SlideLayoutType.TitleAndVerticalText);
//Add a slide of TitleOnly type
ISlide slide9 = pptxDoc.Slides.Add(SlideLayoutType.TitleOnly);
//Add a slide of TwoContent type
ISlide slide10 = pptxDoc.Slides.Add(SlideLayoutType.TwoContent);
//Add a slide of VerticalTitleAndText type
ISlide slide11 = pptxDoc.Slides.Add(SlideLayoutType.VerticalTitleAndText);
//Save the PowerPoint file
pptxDoc.Save("Sample.pptx");
//Close the PowerPoint presentation
pptxDoc.Close();//Create a PowerPoint presentation
IPresentation pptxDoc = Presentation.Create();
//Add a slide of blank layout type
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a slide of comparison type
ISlide slide2 = pptxDoc.Slides.Add(SlideLayoutType.Comparison);
//Add a slide of ContentWithCaption type
ISlide slide3 = pptxDoc.Slides.Add(SlideLayoutType.ContentWithCaption);
//Add a slide of PictureWithCaption type
ISlide slide4 = pptxDoc.Slides.Add(SlideLayoutType.PictureWithCaption);
//Add a slide of SectionHeader type
ISlide slide5 = pptxDoc.Slides.Add(SlideLayoutType.SectionHeader);
//Add a slide of Title type
ISlide slide6 = pptxDoc.Slides.Add(SlideLayoutType.Title);
//Add a slide of TitleAndContent type
ISlide slide7 = pptxDoc.Slides.Add(SlideLayoutType.TitleAndContent);
//Add a slide of TitleAndVerticalText type
ISlide slide8 = pptxDoc.Slides.Add(SlideLayoutType.TitleAndVerticalText);
//Add a slide of TitleOnly type
ISlide slide9 = pptxDoc.Slides.Add(SlideLayoutType.TitleOnly);
//Add a slide of TwoContent type
ISlide slide10 = pptxDoc.Slides.Add(SlideLayoutType.TwoContent);
//Add a slide of VerticalTitleAndText type
ISlide slide11 = pptxDoc.Slides.Add(SlideLayoutType.VerticalTitleAndText);
//Save the PowerPoint file
pptxDoc.Save("Sample.pptx");
//Close the PowerPoint instance
pptxDoc.Close();'Create a PowerPoint file
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide of blank layout type
Dim slide1 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Add a slide of comparison type
Dim slide2 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Comparison)
'Add a slide of ContentWithCaption type
Dim slide3 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.ContentWithCaption)
'Add a slide of PictureWithCaption type
Dim slide4 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.PictureWithCaption)
'Add a slide of SectionHeader type
Dim slide5 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.SectionHeader)
'Add a slide of Title type
Dim slide6 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Title)
'Add a slide of TitleAndContent type
Dim slide7 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.TitleAndContent)
'Add a slide of TitleAndVerticalText type
Dim slide8 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.TitleAndVerticalText)
'Add a slide of TitleOnly type
Dim slide9 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.TitleOnly)
'Add a slide of TwoContent type
Dim slide10 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.TwoContent)
'Add a slide of VerticalTitleAndText type
Dim slide11 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.VerticalTitleAndText)
'Save the PowerPoint file
pptxDoc.Save("Sample.pptx")
'Close the PowerPoint instance
pptxDoc.Close()You can download a complete working sample from GitHub.
Adding a Custom Layout Slide
Slide layouts are template designs for PowerPoint slides. A slide layout can contain formatting, positioning, and placeholders for a slide. There are 9 predefined layouts; custom slide layouts can also be designed.
The following code example demonstrates how to create and use a customized slide layout in a PowerPoint presentation.
//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Add a new custom layout slide to the master collection with a specific layout type and name
ILayoutSlide layoutSlide = pptxDoc.Masters[0].LayoutSlides.Add(SlideLayoutType.Blank, "CustomLayout");
//Set background of the layout slide
layoutSlide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(78, 89, 90);
//Get the stream of an image
using (FileStream pictureStream = new FileStream("Image.png", FileMode.Open))
{
//Add the picture into layout slide
layoutSlide.Shapes.AddPicture(pictureStream, 100, 100, 100, 100);
}
//Add a slide of new designed custom layout to the presentation
ISlide slide = pptxDoc.Slides.Add(layoutSlide);
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Add a new custom layout slide to the master collection with a specific layout type and name
ILayoutSlide layoutSlide = pptxDoc.Masters[0].LayoutSlides.Add(SlideLayoutType.Blank, "CustomLayout");
//Set background of the layout slide
layoutSlide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(78, 89, 90);
//Get the stream of an image
Stream pictureStream = File.Open("Image.png", FileMode.Open);
//Add the picture into layout slide
layoutSlide.Shapes.AddPicture(pictureStream, 100, 100, 100, 100);
//Add a slide of new designed custom layout to the presentation
ISlide slide = pptxDoc.Slides.Add(layoutSlide);
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();'Open the template presentation
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Add a new custom layout slide to the master collection with a specific layout type and name
Dim layoutSlide As ILayoutSlide = pptxDoc.Masters(0).LayoutSlides.Add(SlideLayoutType.Blank, "CustomLayout")
'Set background of the layout slide
layoutSlide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(78, 89, 90)
'Get the stream of an image
Dim pictureStream As Stream = File.Open("Image.png", FileMode.Open)
'Add the picture into layout slide
layoutSlide.Shapes.AddPicture(pictureStream, 100, 100, 100, 100)
'Add a slide of new designed custom layout to the presentation
Dim slide As ISlide = pptxDoc.Slides.Add(layoutSlide)
'Save the presentation
pptxDoc.Save("Output.pptx")
'Close the presentation
pptxDoc.Close()You can download a complete working sample from GitHub.
The following code example demonstrates how to add a slide with an existing slide’s layout.
//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Get the layout slide collection of the master
ILayoutSlides layoutSlides = pptxDoc.Masters[0].LayoutSlides;
ILayoutSlide slideLayout = null;
//Get each layout slide from the collection
foreach (ILayoutSlide layout in layoutSlides)
{
//Check if the layout slide has desired custom layout name
if (layout.Name == "CustomSlideLayout")
{
slideLayout = layout;
break;
}
}
//Add slide with the desired layout.
ISlide slide = pptxDoc.Slides.Add(slideLayout);
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Get the layout slide collection of the master
ILayoutSlides layoutSlides = pptxDoc.Masters[0].LayoutSlides;
ILayoutSlide slideLayout = null;
//Get each layout slide from the collection
foreach (ILayoutSlide layout in layoutSlides)
{
//Check if the layout slide has desired custom layout name
if (layout.Name == "CustomSlideLayout")
{
slideLayout = layout;
break;
}
}
//Add slide with the desired layout.
ISlide slide = pptxDoc.Slides.Add(slideLayout);
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();'Open the template presentation
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Get the layout slide collection of the master
Dim layoutSlides As ILayoutSlides = pptxDoc.Masters(0).LayoutSlides
Dim slideLayout As ILayoutSlide = Nothing
'Get each layout slide from the collection
For Each layout As ILayoutSlide In layoutSlides
'Check if the layout slide has desired custom layout name
If layout.Name = "CustomSlideLayout" Then
slideLayout = layout
Exit For
End If
Next
'Add slide with the desired layout.
Dim slide As ISlide = pptxDoc.Slides.Add(slideLayout)
'Save the presentation
pptxDoc.Save("Output.pptx")
'Close the presentation
pptxDoc.Close()You can download a complete working sample from GitHub.
Iterating slide elements
The following code example shows how to iterate through all slide elements in a PowerPoint presentation, update text, resize images, and modify charts before saving the updated presentation.
//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Template.pptx");
// Iterate through each slide in the presentation
foreach (ISlide slide in pptxDoc.Slides)
{
// Iterate through each shape in the master slide shapes.
foreach (IShape shape in slide.LayoutSlide.MasterSlide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
// Iterate through each shape in the layout slide shapes.
foreach (IShape shape in slide.LayoutSlide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
// Iterate through each shape in the slide
foreach (IShape shape in slide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
}
//Save the presentation
pptxDoc.Save("Output.pptx");
//Closes the Presentation
pptxDoc.Close();//Opens an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Template.pptx");
// Iterate through each slide in the presentation
foreach (ISlide slide in pptxDoc.Slides)
{
// Iterate through each shape in the master slide shapes.
foreach (IShape shape in slide.LayoutSlide.MasterSlide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
// Iterate through each shape in the layout slide shapes.
foreach (IShape shape in slide.LayoutSlide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
// Iterate through each shape in the slide
foreach (IShape shape in slide.Shapes)
{
// Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape);
}
}
//Saves the Presentation to the file system
pptxDoc.Save("Output.pptx");
//Closes the Presentation
pptxDoc.Close();'Opens an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Template.pptx")
' Iterate through each slide in the presentation
For Each slide As ISlide In pptxDoc.Slides
' Iterate through each shape in the master slide shapes.
For Each shape As IShape In slide.LayoutSlide.MasterSlide.Shapes
' Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape)
Next
' Iterate through each shape in the layout slide shapes.
For Each shape As IShape In slide.LayoutSlide.Shapes
' Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape)
Next
' Iterate through each shape in the slide
For Each shape As IShape In slide.Shapes
' Modify the shape properties (text, size, hyperlinks, etc.)
ModifySlideElements(shape)
Next
Next
'Saves the Presentation to the file system
pptxDoc.Save("Output.pptx")
'Closes the Presentation
pptxDoc.Close()Helper methods used by the above example
The following code example provides supporting methods for the above code.
private static void ModifySlideElements(IShape shape)
{
switch (shape.SlideItemType)
{
case SlideItemType.AutoShape:
{
// Modify text if present in the shape
if (!string.IsNullOrEmpty(shape.TextBody.Text))
{
ModifyTextPart(shape.TextBody);
}
// If shape is a rectangle, add a hyperlink
else if (shape.AutoShapeType == AutoShapeType.Rectangle)
{
shape.SetHyperlink("www.google.com");
}
break;
}
case SlideItemType.Placeholder:
{
// Modify text if present in the placeholder
if (!string.IsNullOrEmpty(shape.TextBody.Text))
{
ModifyTextPart(shape.TextBody);
}
break;
}
case SlideItemType.Picture:
{
// Resize the picture
IPicture picture = shape as IPicture;
picture.Height = 160;
picture.Width = 130;
break;
}
case SlideItemType.Table:
{
// Get the table shape
ITable table = shape as ITable;
// Iterate through rows and modify text in each cell
foreach (IRow row in table.Rows)
{
foreach (ICell cell in row.Cells)
{
ModifyTextPart(cell.TextBody);
}
}
break;
}
case SlideItemType.GroupShape:
{
// Get the group shape and iterate through child shapes
IGroupShape groupShape = shape as IGroupShape;
foreach (IShape childShape in groupShape.Shapes)
{
ModifySlideElements(childShape);
}
break;
}
case SlideItemType.Chart:
{
// Modify chart properties
IPresentationChart chart = shape as IPresentationChart;
chart.ChartTitle = "Purchase Details";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Color = OfficeKnownColors.Red;
chart.ChartTitleArea.Size = 20;
break;
}
case SlideItemType.SmartArt:
{
// Modify SmartArt content
ISmartArt smartArt = shape as ISmartArt;
//Traverse through all nodes inside SmartArt
foreach (ISmartArtNode node in smartArt.Nodes)
{
ModifyTextPart(node.TextBody);
}
break;
}
case SlideItemType.OleObject:
{
// Modify OLE object size
IOleObject oleObject = shape as IOleObject;
oleObject.Width = 300;
break;
}
}
}private static void ModifySlideElements(IShape shape)
{
switch (shape.SlideItemType)
{
case SlideItemType.AutoShape:
{
// Modify text if present in the shape
if (!string.IsNullOrEmpty(shape.TextBody.Text))
{
ModifyTextPart(shape.TextBody);
}
// If shape is a rectangle, add a hyperlink
else if (shape.AutoShapeType == AutoShapeType.Rectangle)
{
shape.SetHyperlink("www.google.com");
}
break;
}
case SlideItemType.Placeholder:
{
// Modify text if present in the placeholder
if (!string.IsNullOrEmpty(shape.TextBody.Text))
{
ModifyTextPart(shape.TextBody);
}
break;
}
case SlideItemType.Picture:
{
// Resize the picture
IPicture picture = shape as IPicture;
picture.Height = 160;
picture.Width = 130;
break;
}
case SlideItemType.Table:
{
// Get the table shape
ITable table = shape as ITable;
// Iterate through rows and modify text in each cell
foreach (IRow row in table.Rows)
{
foreach (ICell cell in row.Cells)
{
ModifyTextPart(cell.TextBody);
}
}
break;
}
case SlideItemType.GroupShape:
{
// Get the group shape and iterate through child shapes
IGroupShape groupShape = shape as IGroupShape;
foreach (IShape childShape in groupShape.Shapes)
{
ModifySlideElements(childShape);
}
break;
}
case SlideItemType.Chart:
{
// Modify chart properties
IPresentationChart chart = shape as IPresentationChart;
chart.ChartTitle = "Purchase Details";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Color = OfficeKnownColors.Red;
chart.ChartTitleArea.Size = 20;
break;
}
case SlideItemType.SmartArt:
{
// Modify SmartArt content
ISmartArt smartArt = shape as ISmartArt;
//Traverse through all nodes inside SmartArt
foreach (ISmartArtNode node in smartArt.Nodes)
{
ModifyTextPart(node.TextBody);
}
break;
}
case SlideItemType.OleObject:
{
// Modify OLE object size
IOleObject oleObject = shape as IOleObject;
oleObject.Width = 300;
break;
}
}
}Private Shared Sub ModifySlideElements(ByVal shape As IShape)
Select Case shape.SlideItemType
Case SlideItemType.AutoShape
' Modify text if present in the shape
If Not String.IsNullOrEmpty(shape.TextBody.Text) Then
ModifyTextPart(shape.TextBody)
' If shape is a rectangle, add a hyperlink
ElseIf shape.AutoShapeType = AutoShapeType.Rectangle Then
shape.SetHyperlink("www.google.com")
End If
Case SlideItemType.ConnectionShape
Dim connector As IConnector = TryCast(shape, IConnector)
' Modify the arrowhead style at the beginning of the connector line
connector.LineFormat.BeginArrowheadStyle = ArrowheadStyle.ArrowDiamond
Case SlideItemType.Placeholder
' Modify text if present in the placeholder
If Not String.IsNullOrEmpty(shape.TextBody.Text) Then
ModifyTextPart(shape.TextBody)
End If
Case SlideItemType.Picture
' Resize the picture
Dim picture As IPicture = TryCast(shape, IPicture)
picture.Height = 160
picture.Width = 130
Case SlideItemType.Table
' Get the table shape
Dim table As ITable = TryCast(shape, ITable)
' Iterate through rows and modify text in each cell
For Each row As IRow In table.Rows
For Each cell As ICell In row.Cells
ModifyTextPart(cell.TextBody)
Next
Next
Case SlideItemType.GroupShape
' Get the group shape and iterate through child shapes
Dim groupShape As IGroupShape = TryCast(shape, IGroupShape)
For Each childShape As IShape In groupShape.Shapes
ModifySlideElements(childShape)
Next
Case SlideItemType.Chart
' Modify chart properties
Dim chart As IPresentationChart = TryCast(shape, IPresentationChart)
chart.ChartTitle = "Purchase Details"
chart.ChartTitleArea.Bold = True
chart.ChartTitleArea.Color = OfficeKnownColors.Red
chart.ChartTitleArea.Size = 20
Case SlideItemType.SmartArt
' Modify SmartArt content
Dim smartArt As ISmartArt = TryCast(shape, ISmartArt)
' Traverse through all nodes inside SmartArt
For Each node As ISmartArtNode In smartArt.Nodes
ModifyTextPart(node.TextBody)
Next
Case SlideItemType.OleObject
' Modify OLE object size
Dim oleObject As IOleObject = TryCast(shape, IOleObject)
oleObject.Width = 300
End Select
End Subprivate static void ModifyTextPart(ITextBody textBody)
{
// Iterate through paragraphs in the text body
foreach (IParagraph paragraph in textBody.Paragraphs)
{
// Iterate through text parts and modify the text
foreach (ITextPart textPart in paragraph.TextParts)
{
textPart.Text = "Adventure Works";
}
}
}private static void ModifyTextPart(ITextBody textBody)
{
// Iterate through paragraphs in the text body
foreach (IParagraph paragraph in textBody.Paragraphs)
{
// Iterate through text parts and modify the text
foreach (ITextPart textPart in paragraph.TextParts)
{
textPart.Text = "Adventure Works";
}
}
}Private Sub ModifyTextPart(ByVal textBody As ITextBody)
' Iterate through paragraphs in the text body
For Each paragraph As IParagraph In textBody.Paragraphs
' Iterate through text parts and modify the text
For Each textPart As ITextPart In paragraph.TextParts
textPart.Text = "Adventure Works"
Next
Next
End SubYou can download a complete working sample from GitHub.
Cloning a slide
You can create a deep copy of a slide by cloning the slide. The cloned slide is an independent copy of its source slide. This means the changes made in the cloned slide do not affect the source slide.
//Open the template presentation
IPresentation pptxDoc = Presentation.Open("Presentation.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Creates a cloned copy of slide.
ISlide slideClone = slide.Clone();
//Adds a new text box to the cloned slide.
IShape textboxShape = slideClone.AddTextBox(0, 0, 250, 250);
//Adds a paragraph with text content to the shape.
textboxShape.TextBody.AddParagraph("Hello Presentation");
//Adds the slide to the Presentation.
pptxDoc.Slides.Add(slideClone);
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();//Opens an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Presentation.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Creates a cloned copy of slide.
ISlide slideClone = slide.Clone();
//Adds a new text box to the cloned slide.
IShape textboxShape = slideClone.AddTextBox(0, 0, 250, 250);
//Adds a paragraph with text content to the shape.
textboxShape.TextBody.AddParagraph("Hello Presentation");
//Adds the slide to the Presentation.
pptxDoc.Slides.Add(slideClone);
//Saves the Presentation to the file system.
pptxDoc.Save("Output.pptx");
//Closes the Presentation
pptxDoc.Close();'Opens an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Presentation.pptx")
'Retrieves the slide instance.
Dim slide As ISlide = pptxDoc.Slides(0)
'Creates a cloned copy of slide.
Dim slideClone As ISlide = slide.Clone()
'Adds a new text box to the cloned slide.
Dim textboxShape As IShape = slideClone.AddTextBox(0, 0, 250, 250)
'Adds a paragraph with text content to the shape.
textboxShape.TextBody.AddParagraph("Hello Presentation")
'Adds the slide to the Presentation.
pptxDoc.Slides.Add(slideClone)
'Saves the Presentation to the file system.
pptxDoc.Save("Output.pptx")
'Closes the Presentation
pptxDoc.Close()You can download a complete working sample from GitHub.
Merging slides
The Essential® Presentation provides the ability to clone slides from one Presentation to another Presentation. With this ability, you can split a large Presentation into small ones and also merge multiple presentations into one Presentation. Merging differs from cloning in that the resulting slide is added to a different (destination) presentation, and you can choose the theme applied to the merged slide by using the enum PasteOption.
Destination formatting
This PasteOption preserves the merged slide with formatting from the destination file during merging.
The following code sample explains how to merge a slide with the destination formatting.
//Opens the source Presentation
IPresentation sourcePresentation = Presentation.Open("SourcePresentation.pptx");
//Opens the destination Presentation
IPresentation destinationPresentation = Presentation.Open("DestinationPresentation.pptx");
//Clones the first slide of the source Presentation
ISlide clonedSlide = sourcePresentation.Slides[0].Clone();
//Merges the cloned slide to the destination Presentation with paste option - Destination Theme
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.UseDestinationTheme);
//Saves the destination Presentation
destinationPresentation.Save("Output.pptx");
//Closes the source presentation
sourcePresentation.Close();
//Closes the destination Presentation
destinationPresentation.Close();//Opens the source Presentation
IPresentation sourcePresentation = Presentation.Open("SourcePresentation.pptx");
//Opens the destination Presentation
IPresentation destinationPresentation = Presentation.Open("DestinationPresentation.pptx");
//Clones the first slide of the source Presentation
ISlide clonedSlide = sourcePresentation.Slides[0].Clone();
//Merges the cloned slide to the destination Presentation with paste option - Destination Theme
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.UseDestinationTheme);
//Saves the destination Presentation
destinationPresentation.Save("Output.pptx");
//Closes the source presentation
sourcePresentation.Close();
//Closes the destination Presentation
destinationPresentation.Close();'Opens the source Presentation
Dim sourcePresentation As IPresentation = Presentation.Open("SourcePresentation.pptx")
'Opens the destination Presentation
Dim destinationPresentation As IPresentation = Presentation.Open("DestinationPresentation.pptx")
'Clones the first slide of the source Presentation
Dim clonedSlide As ISlide = sourcePresentation.Slides(0).Clone()
'Merges the cloned slide to the destination Presentation with paste option - Destination Theme
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.UseDestinationTheme)
'Saves the destination Presentation
destinationPresentation.Save("Output.pptx")
'Closes the source presentation
sourcePresentation.Close()
'Closes the destination Presentation
destinationPresentation.Close()You can download a complete working sample from GitHub.
Source formatting
This PasteOption preserves the merged slide with formatting from the source file during merging.
The following code sample explains how to merge a slide with the source formatting.
//Opens the source Presentation
IPresentation sourcePresentation = Presentation.Open("SourcePresentation.pptx");
//Opens the destination Presentation
IPresentation destinationPresentation = Presentation.Open("DestinationPresentation.pptx");
//Clones the first slide of the source Presentation
ISlide clonedSlide = sourcePresentation.Slides[0].Clone();
//Merges the cloned slide to the destination Presentation with paste option - Source formatting
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.SourceFormatting);
//Saves the destination Presentation
destinationPresentation.Save("Output.pptx");
//Closes the source presentation
sourcePresentation.Close();
//Closes the destination Presentation
destinationPresentation.Close();//Opens the source Presentation
IPresentation sourcePresentation = Presentation.Open("SourcePresentation.pptx");
//Opens the destination Presentation
IPresentation destinationPresentation = Presentation.Open("DestinationPresentation.pptx");
//Clones the first slide of the source Presentation
ISlide clonedSlide = sourcePresentation.Slides[0].Clone();
//Merges the cloned slide to the destination Presentation with paste option - Source formatting
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.SourceFormatting);
//Saves the destination Presentation
destinationPresentation.Save("Output.pptx");
//Closes the source presentation
sourcePresentation.Close();
//Closes the destination Presentation
destinationPresentation.Close();'Opens the source Presentation
Dim sourcePresentation As IPresentation = Presentation.Open("SourcePresentation.pptx")
'Opens the destination Presentation
Dim destinationPresentation As IPresentation = Presentation.Open("DestinationPresentation.pptx")
'Clones the first slide of the source Presentation
Dim clonedSlide As ISlide = sourcePresentation.Slides(0).Clone()
'Merges the cloned slide to the destination Presentation with paste option - Source formatting
destinationPresentation.Slides.Add(clonedSlide, PasteOptions.SourceFormatting)
'Saves the destination Presentation
destinationPresentation.Save("Output.pptx")
'Closes the source presentation
sourcePresentation.Close()
'Closes the destination Presentation
destinationPresentation.Close()You can download a complete working sample from GitHub.
Removing a slide
The Essential® Presentation provides the ability to delete a slide by its instance or by its index position in the slide collection. The following code demonstrates how to delete a slide from a presentation.
NOTE
Calling
Slides.Remove(slide)shifts the indices of subsequent slides; therefore the index used inSlides.RemoveAt(1)refers to the new position after the previous removal.
//Opens an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Presentation1.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Removes the specified slide from the Presentation.
pptxDoc.Slides.Remove(slide);
// Removes the slide from the specified index.
pptxDoc.Slides.RemoveAt(1);
//Saves the Presentation to the file system
pptxDoc.Save("Output.pptx");
//Closes the Presentation instance
pptxDoc.Close();//Opens an existing presentation.
IPresentation pptxDoc = Presentation.Open("Presentation1.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Removes the specified slide from the Presentation.
pptxDoc.Slides.Remove(slide);
// Removes the slide from the specified index.
pptxDoc.Slides.RemoveAt(1);
//Saves the destination Presentation
pptxDoc.Save("Output.pptx");
//Closes the Presentation instance
pptxDoc.Close();'Opens an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Presentation1.pptx")
'Retrieves the slide instance.
Dim slide As ISlide = pptxDoc.Slides(0)
'Removes the specified slide from the Presentation.
pptxDoc.Slides.Remove(slide)
'Removes the slide from the specified index.
pptxDoc.Slides.RemoveAt(1)
'Saves the destination Presentation
pptxDoc.Save("Output.pptx")
'Closes the Presentation instance
pptxDoc.Close()You can download a complete working sample from GitHub.
Converting to an image
You can convert a presentation slide to an image with Essential® Presentation. The following code example converts the first slide of a PowerPoint presentation into an image and saves the image to a file.
The cross-platform sample uses the modern PresentationRenderer and ConvertToImage overload that returns a Stream. The Windows-specific sample uses the legacy ChartToImageConverter API and the ConvertToImage overload that returns an Image (for migration guidance, see the Conversion topic).
//Open the existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Initialize the PresentationRenderer to perform image conversion.
pptxDoc.PresentationRenderer = new PresentationRenderer();
//Convert the PowerPoint slide to an image as stream.
using (Stream stream = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Presentation.ExportImageFormat.Png))
{
//Save the image stream to a file.
using (FileStream fileStreamOutput = File.Create("slide1.png"))
{
stream.CopyTo(fileStreamOutput);
}
}
//Closes the Presentation instance
pptxDoc.Close();//Opens a PowerPoint presentation file
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Creates an instance of ChartToImageConverter and assigns it to the Presentation
pptxDoc.ChartToImageConverter = new ChartToImageConverter();
//Converts the first slide into image
Image image = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageType.Metafile);
//Saves the image as file
image.Save("slide1.png");
//Closes the Presentation instance
pptxDoc.Close();'Opens a PowerPoint presentation file
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Creates an instance of ChartToImageConverter and assigns it to ChartToImageConverter
pptxDoc.ChartToImageConverter = New ChartToImageConverter()
'Converts the first slide into image
Dim image As Image = pptxDoc.Slides(0).ConvertToImage(Syncfusion.Drawing.ImageType.Metafile)
'Saves the image as file
image.Save("slide1.png")
'Closes the Presentation instance
pptxDoc.Close()For more details on assemblies required for converting a slide to an image, see Conversion
NOTE
You can print the PowerPoint presentations by using its ability to convert the slides as images. For more details, refer to Printing a PowerPoint presentation
Changing Slide background
The following code example demonstrates setting the background for a slide.
//Opens an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Presentation1.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Retrieves the background instance.
IBackground background = slide.Background;
//Sets the fill type of the background to gradient.
background.Fill.FillType = FillType.Gradient;
//Retrieves the fill of the background to the IGradientFill instance.
IGradientFill gradient = background.Fill.GradientFill;
//Adds the first gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Green, 20);
//Adds the second gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Yellow, 50);
//Save the PowerPoint file
pptxDoc.Save("Output.pptx");
//Closes the Presentation
pptxDoc.Close();//Opens an existing Presentation.
IPresentation pptxDoc = Presentation.Open("Presentation1.pptx");
//Retrieves the slide instance.
ISlide slide = pptxDoc.Slides[0];
//Retrieves the background instance.
IBackground background = slide.Background;
//Sets the fill type of the background to gradient.
background.Fill.FillType = FillType.Gradient;
//Retrieves the fill of the background to the IGradientFill instance.
IGradientFill gradient = background.Fill.GradientFill;
//Adds the first gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Green, 20);
//Adds the second gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Yellow, 50);
//Saves the Presentation to the file system
pptxDoc.Save("Output.pptx");
//Closes the Presentation
pptxDoc.Close();'Opens an existing Presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Presentation1.pptx")
'Retrieves the slide instance.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieves the background instance.
Dim background As IBackground = slide.Background
'Sets the fill type of the background to gradient.
background.Fill.FillType = FillType.Gradient
'Retrieves the fill of the background to the IGradientFill instance.
Dim gradient As IGradientFill = background.Fill.GradientFill
'Adds the first gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Green, 20)
'Adds the second gradient stop of the gradient fill.
gradient.GradientStops.Add(ColorObject.Yellow, 50)
'Saves the Presentation to the file system
pptxDoc.Save("Output.pptx")
'Closes the Presentation
pptxDoc.Close()You can download a complete working sample from GitHub.
Online Demo
Try the related live demo to see these APIs in action:
- Explore how to create slides with simple text, table, and image in a PowerPoint presentation using the .NET PowerPoint Library (Presentation) in a live demo here.
See Also
You may also find the following support articles useful: