How to Add Layout Slides in PowerPoint Presentation inside .NET Apps
In This technical tip, we will learn how .NET developers can add Layout slides in presentation using Aspose.Slides inside their .NET applications. There are cases when there is missing Layout slide in presentation and once can now add the Layout Slides in presentation. Each slide has unique Id and Layout slides are maintained inside presentation Masters. One can access the Layout slide either by Type or by Name. Aspose.Slides for .NET allows developers to add new Layout slides in presentation.
//your code here...//Adding Slides to the Presentation
//[C# Code Sample]
//Instantiate Presentation class that represents the presentation file
public static void AddLayoutSlide()
{
string n = @"Test.pptx";
//Instantiate Presentation class that represents the presentation file
using (Presentation p = new Presentation(n))
{
// Try to search by layout slide type
IMasterLayoutSlideCollection layoutSlides = p.Masters[0].LayoutSlides;
ILayoutSlide layoutSlide =
layoutSlides.GetByType(SlideLayoutType.TitleAndObject) ??
layoutSlides.GetByType(SlideLayoutType.Title);
if (layoutSlide == null)
{
// The situation when a presentation doesn't contain some type of layouts.
// Technographics.pptx presentation only contains Blank and Custom layout types.
// But layout slides with Custom types has different slide names,
// like "Title", "Title and Content", etc. And it is possible to use these
// names for layout slide selection.
// Also it is possible to use the set of placeholder shape types. For example,
// Title slide should have only Title pleceholder type, etc.
foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
{
if (titleAndObjectLayoutSlide.Name == "Title and Object")
{
layoutSlide = titleAndObjectLayoutSlide;
break;
}
}
if (layoutSlide == null)
{
foreach (ILayoutSlide titleLayoutSlide in layoutSlides)
{
if (titleLayoutSlide.Name == "Title")
{
layoutSlide = titleLayoutSlide;
break;
}
}
if (layoutSlide == null)
{
layoutSlide = layoutSlides.GetByType(SlideLayoutType.Blank);
if (layoutSlide == null)
{
layoutSlide = layoutSlides.Add(SlideLayoutType.TitleAndObject, "Title and Object");
}
}
}
}
//Adding empty slide with added layout slide
p.Slides.InsertEmptySlide(0, layoutSlide);
//Save presentation
p.Save(n + "_output.pptx", SaveFormat.Pptx);
}
}
//[VB.NET Code Sample]
Public Shared Sub AddLayoutSlide()
Dim n As String = "Technographics.pptx"
'Instantiate Presentation class that represents the presentation file
Using p As New Presentation(n)
' Try to search by layout slide type
Dim layoutSlides As IMasterLayoutSlideCollection = p.Masters(0).LayoutSlides
Dim layoutSlide As ILayoutSlide = If(layoutSlides.GetByType(SlideLayoutType.TitleAndObject), layoutSlides.GetByType(SlideLayoutType.Title))
If layoutSlide Is Nothing Then
' The situation when a presentation doesn't contain some type of layouts.
' Technographics.pptx presentation only contains Blank and Custom layout types.
' But layout slides with Custom types has different slide names,
' like "Title", "Title and Content", etc. And it is possible to use these
' names for layout slide selection.
' Also it is possible to use the set of placeholder shape types. For example,
' Title slide should have only Title pleceholder type, etc.
For Each titleAndObjectLayoutSlide As ILayoutSlide In layoutSlides
If titleAndObjectLayoutSlide.Name = "Title and Object" Then
layoutSlide = titleAndObjectLayoutSlide
Exit For
End If
Next
If layoutSlide Is Nothing Then
For Each titleLayoutSlide As ILayoutSlide In layoutSlides
If titleLayoutSlide.Name = "Title" Then
layoutSlide = titleLayoutSlide
Exit For
End If
Next
If layoutSlide Is Nothing Then
layoutSlide = layoutSlides.GetByType(SlideLayoutType.Blank)
If layoutSlide Is Nothing Then
layoutSlide = layoutSlides.Add(SlideLayoutType.TitleAndObject, "Title and Object")
End If
End If
End If
End If
' Adding new slide with added layout
p.Slides.InsertEmptySlide(0, layoutSlide)
' Save presentation
p.Save(n & "_output.pptx", SaveFormat.Pptx)
End Using
End Sub
Language: C# | User: Sheraz Khan | Created: Dec 3, 2014 | Tags: add Layout slides in presentation Layout Slide collection reate an instance of Presentation Access the Master Slide collection find existing Layout slides dd a new Layout slide .NET PowerPoint API PowerPoint .NET Component