Home > Code > C# > How to Add Layout Slides in PowerPoint Presentation inside .NET Apps

How to Add Layout Slides in PowerPoint Presentation inside .NET Apps

by sher azam   on Dec 03, 2014   Category: C#   |  Views: 1948    |  Points: 25   |  Gold 


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. To add a Layout Slide, please follow the steps below:

• Create an instance of Presentation class
• Access the Master Slide collection
• Try to find existing Layout slides to see if the required one is already available in Layout Slide collection or not
• Add a new Layout slide if the desired layout is unavailable
• Add an empty slide with newly added Layout slide
• Finally, write the presentation file using the Presentation object

//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



Overview: Aspose.Slides for .NET

Aspose.Slides is a .NET component to read, write and modify a PowerPoint document without using MS PowerPoint. PowerPoint versions from 97-2007 and all three PowerPoint formats: PPT, POT, PPS are also supported. Now you can create, access, copy, clone, edit and delete slides in your presentations. Other features include PPT/PPTX printing, PPT to XPS format, saving PowerPoint slides into PDF, adding & modifying audio & video frames, using shapes like rectangles or ellipses and saving presentations in SVG format, streams or images.

- Homepage of Aspose.Slides for .NET: http://www.aspose.com/.net/powerpoint-component.aspx

- Download Aspose.Slides for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.slides-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts