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.

Wed Feb 21, 2024 3:23 pm

Problem:
When I copy a slide from presentation A and paste it into presentation B, the slide master is copied too. This is fine. However, the slide master is copied each time the slide from presentation A is pasted into presentation B. So if you copy and paste the slide 10 times, you will have 10 slide masters in presentation B. This is not correct.

slide append issue.png


Solution:
Recreate the behaviour form powerpoint if you do this manually by hand. In powerpoint, if you do this by hand, you will only have 1 slide master copied to presentation B because it must check if the slide master already exists.

To recreate the problem:
1. open both presentation A and B.
2. copy and paste the slide from presentation A into presentation B - note, ensure you select "keep source formatting"
3. copy and paste the slide 5 times. You will have 1 or 2 slide masters max.

Do the above steps by hand and by the code below and compare the difference. The powerpoint made by hand will have 2 slide masters and the powerpoint created using spire will have 3, one for each slide you append. Imaging if you append 100 slides, you will have 100 slide masters, this is not correct. This is a bug.

presentation A:
Presentation_A.zip

presentation B:
Presentation_B.zip



Spire version: This occurs in both Spire.Presentation 7.8.0 and 8.2.0

Code: Select all
Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"sample.pptx");
            var finalPpt = new Presentation();
            foreach (var slide in ppt.Slides)
            {
                var newPpt = new Presentation();
                newPpt.SlideSize.Type = ppt.SlideSize.Type;

                ISlide clonedSlide = ppt.Slides[0];
                newPpt.Slides.Insert(0, clonedSlide);

                var generatedSlide = newPpt.Slides[0];

               
                finalPpt.Slides.Append(generatedSlide);
            }

            finalPpt.SaveToFile(@"C:\Users\Maine\source\repos\ConsoleApp2\ConsoleApp2\bin\Debug\net6.0\samplenew.pptx", FileFormat.Pptx2013);

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

Thu Feb 22, 2024 6:03 am

Hello,

Thank you for reaching out.
Upon reviewing and analyzing your code as per your description, I have identified that the for loop, when executed 100 times, functions akin to merging 100 PPT files. Consequently, the resulting file will contain 101 master slides. Please find below the modified code for copying slides:
Code: Select all
 Presentation ppt = new Presentation();
 ppt.LoadFromFile(@"Presentation_A.pptx");
 var finalPpt = new Presentation();
for (int i = 0; i <ppt.Slides.Count; i++)
{
    finalPpt.Slides.Append(ppt.Slides[0]);
}

finalPpt.SaveToFile(@"samplenew-result.pptx", FileFormat.Pptx2013);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Feb 23, 2024 11:13 am

Thank you for your reply. Can you please confirm if the same problem exists in this code too:

Code: Select all
using (var templatePresentation = new Spire.Presentation.Presentation())
{
   var templateList = new Presentation();

    var templatePresentationItem = new Spire.Presentation.Presentation();
    templatePresentationItem.LoadFromFile(filePath);
    templateList = templatePresentationItem;
    templatePresentationItem.Dispose();

    templatePresentation.LoadFromFile(filePath);
   
    var presentation = new Spire.Presentation.Presentation();
    presentation.Slides.RemoveAt(0);

    for (int i = 0; i < presentation.Masters.Count; i++)
    {
        presentation.Masters.RemoveAt(i);
    }

    for (int i = 0; i < templatePresentation.Masters.Count; i++)
    {
        presentation.Masters.AppendSlide(templatePresentation.Masters[i]);
    }
    //Code to generate presentation slides
    presentation.SaveToFile(filePath, fileFormat);
    presentation.Dispose();
}

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

Mon Feb 26, 2024 2:00 am

Hello,

Thank you for your feedback.
Upon careful review of your code, I have identified that the instances "templateList" and "templatePresentationItem" are redundant. Removing the code for these two objects does not impact the program's functionality. Below is the revised code:
Code: Select all
using (var templatePresentation = new Presentation())
{

    templatePresentation.LoadFromFile(filePath);
    var presentation = new Presentation();
    presentation.Slides.RemoveAt(0);

    for (int i = 0; i < presentation.Masters.Count; i++)
    {
        presentation.Masters.RemoveAt(i);
    }

    for (int i = 0; i < templatePresentation.Masters.Count; i++)
    {
        presentation.Masters.AppendSlide(templatePresentation.Masters[i]);
    }
    presentation.SaveToFile(filePath, fileFormat);
    presentation.Dispose();
}

If you have any further questions or need clarification, please don't hesitate to reach out.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Presentation