Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Fri Apr 04, 2025 6:08 pm

I want to find out which `slide master` and which `slide layout` within it, the `slide` it belongs to.

Also, is there a way to grab the `slide master` and it's `slide layout` by `name` and `index`? or is it one of the other?

boosted_d16
 
Posts: 50
Joined: Wed Apr 19, 2023 11:41 am

Tue Apr 08, 2025 9:25 am

Hello,
Thanks for your inquiry.
Please refer to the code below for testing. If there is any misunderstanding, please write back to us with more details about your needs. Thanks in advance.
Code: Select all
Presentation presentation = new Presentation();

// Load the document from disk
presentation.LoadFromFile(@"test.pptx");

IMasterSlide masterSlide = presentation.Masters[0];
int layoutCount = masterSlide.Layouts.Count;

for (int layoutIndex = 0; layoutIndex < layoutCount; layoutIndex++)
{
    ILayout layout = masterSlide.Layouts[layoutIndex];
    string layoutName = layout.Name;

    ISlide[] dependentSlides = layout.GetDependingSlides();
    if (dependentSlides.Length > 0)
    {
        ISlide firstDependentSlide = dependentSlides[0];
        int slideIndex = presentation.Slides.IndexOf(firstDependentSlide);
        Console.WriteLine($"Slide {slideIndex} belongs to Layout: {layoutName}");
    }
}
Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Tue Apr 08, 2025 10:23 am

2 follow up questions:

1. how can I find out which `slide master index` a `slide` belongs to? Lets say my pptx has 5 slidemasters, each slidemaster with 10 layouts.
2. you are looping over the slide layouts to find the slides. Is there a way to do the reverse? something like `pptx.slide[0].layoutname`? So find the name of the slide layout from the slide.

boosted_d16
 
Posts: 50
Joined: Wed Apr 19, 2023 11:41 am

Wed Apr 09, 2025 3:27 am

Hello,

Thanks for your reply.
Sorry, Spire.Presentation doesn't support traversing slides to find the corresponding Layout and Master yet. Do you want to get the following results? If yes, please refer to the code test below. If not, please reply to tell us more details about your needs.
Slide 1 -> Master: 0, Layout Index: 0, Layout Name: Title Slide
Slide 2 -> Master: 1, Layout Index: 3, Layout Name: Two Content

Code: Select all
// Create a new Presentation object
Presentation presentation = new Presentation();

// Load the PowerPoint file from disk
presentation.LoadFromFile(@"test.pptx");

// Create a list to store slide information (index, master index, layout index, layout name)
var slideInfoList = new List<(int SlideIndex, int MasterIndex, int LayoutIndex, string LayoutName)>();

// Loop through all master slides
for (int masterIndex = 0; masterIndex < presentation.Masters.Count; masterIndex++)
{
    // Get the current master slide
    IMasterSlide masterSlide = presentation.Masters[masterIndex];

    // Loop through all layouts in this master slide
    for (int layoutIndex = 0; layoutIndex < masterSlide.Layouts.Count; layoutIndex++)
    {
        // Get the current layout
        ILayout layout = masterSlide.Layouts[layoutIndex];

        // Get layout name
        string layoutName = layout.Name;

        // Get all slides that use this layout
        ISlide[] dependentSlides = layout.GetDependingSlides();

        // Store information for each dependent slide
        foreach (ISlide slide in dependentSlides)
        {
            int slideIndex = presentation.Slides.IndexOf(slide);
            slideInfoList.Add((slideIndex, masterIndex, layoutIndex, layoutName));
        }
    }
}

// Sort the slides by their index (ascending order)
var sortedSlideInfo = slideInfoList.OrderBy(x => x.SlideIndex).ToList();

// Output the sorted results
foreach (var info in sortedSlideInfo)
{
    // Note: SlideIndex+1 to display 1-based numbering (like PowerPoint UI)
    Console.WriteLine($"Slide {info.SlideIndex + 1} -> Master: {info.MasterIndex}, Layout Index: {info.LayoutIndex}, Layout Name: {info.LayoutName}");
}
Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Presentation

cron