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?
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}");
}
}
// 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}");
}