how to add transition for slide to powerpoint using interop or open xml sdk c#

242 views Asked by At

In my solution, I use open xml sdk and interop to work with the presentation. Please tell me if it is possible with their help to add a transition for a slide to a presentation?

1

There are 1 answers

0
Александр Климук On

found an answer to my question using interop.powerpoint:

ApplicationClass pptApplication = new ApplicationClass();

Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open($"E:\\{fileName}", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

pptPresentation.Slides[slideNumber].SlideShowTransition.AdvanceOnTime = MsoTriState.msoTrue;
pptPresentation.Slides[slideNumber].SlideShowTransition.AdvanceTime = 0.5F;

pptPresentation.Save();

using Open Xml Sdk:

using(PresentationDocument presentationDocument = PresentationDocument.Open(presentationStream, true))
{
    Presentation presentation = presentationDocument.PresentationPart.Presentation;
    
    var countSlides = CountSlides(presentationStream);
    
    SlideId penultimateSlideId = presentationDocument.PresentationPart.Presentation.SlideIdList.ChildElements[0] as SlideId;
    SlidePart penultimateSlidePart = presentationDocument.PresentationPart.GetPartById(penultimateSlideId.RelationshipId) as SlidePart;
    
    var transitions = penultimateSlidePart.Slide.Descendants<Transition>().ToList(); 
    
    if(transitions.Count == 0)
    {   
        var choiseTransition = new Transition()
        {
            Duration = "500",
            AdvanceOnClick = true,
            AdvanceAfterTime = "500",
            Speed = TransitionSpeedValues.Slow
        };
    
        AlternateContent alternateContent = new AlternateContent();
        alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
    
        AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
        alternateContentChoice.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
    
        alternateContentChoice.Append(choiseTransition);
                        
        alternateContent.Append(alternateContentChoice);
    
        penultimateSlidePart.Slide.Append(alternateContent);
    }
    
    presentation.Save();
}

screenshot of powerpoint transition