Skip to content

Commit f3ed347

Browse files
authored
Merge pull request #338 from mkaszewiak/addTransitionToTheSlide
Add transition to the slide
2 parents 17d5477 + 46f6b5c commit f3ed347

File tree

8 files changed

+412
-4
lines changed

8 files changed

+412
-4
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
3+
api_name:
4+
- Microsoft.Office.DocumentFormat.OpenXML.Packaging
5+
api_type:
6+
- schema
7+
ms.assetid: 5471f369-ad02-41c3-a5d3-ebaf618d185a
8+
title: 'How to: Add transitions between slides in a presentation'
9+
ms.suite: office
10+
11+
ms.author: o365devx
12+
author: o365devx
13+
ms.topic: conceptual
14+
ms.date: 04/03/2025
15+
ms.localizationpriority: medium
16+
---
17+
18+
# Add Transitions between slides in a presentation
19+
20+
This topic shows how to use the classes in the Open XML SDK to
21+
add transition between all slides in a presentation programmatically.
22+
23+
## Getting a Presentation Object
24+
25+
In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument> class represents a
26+
presentation document package. To work with a presentation document,
27+
first create an instance of the `PresentationDocument` class, and then work with
28+
that instance. To create the class instance from the document, call the
29+
<xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open*> method, that uses a file path, and a
30+
Boolean value as the second parameter to specify whether a document is
31+
editable. To open a document for read/write, specify the value `true` for this parameter as shown in the following
32+
`using` statement. In this code, the file parameter, is a string that represents the path for the file from which you want to open the document.
33+
34+
### [C#](#tab/cs-1)
35+
[!code-csharp[](../../samples/presentation/add_transition/cs/Program.cs#snippet1)]
36+
37+
### [Visual Basic](#tab/vb-1)
38+
[!code-vb[](../../samples/presentation/add_transition/vb/Program.vb#snippet1)]
39+
***
40+
41+
[!include[Using Statement](../includes/presentation/using-statement.md)] `ppt`.
42+
43+
## The Structure of the Transition
44+
45+
Transition element `<transition>` specifies the kind of slide transition that should be used to transition to the current slide from the
46+
previous slide. That is, the transition information is stored on the slide that appears after the transition is
47+
complete.
48+
49+
The following table lists the attributes of the Transition along
50+
with the description of each.
51+
52+
| Attribute | Description |
53+
|---|---|
54+
| advClick (Advance on Click) | Specifies whether a mouse click advances the slide or not. If this attribute is not specified then a value of true is assumed. |
55+
| advTm (Advance after time) | Specifies the time, in milliseconds, after which the transition should start. This setting can be used in conjunction with the advClick attribute. If this attribute is not specified then it is assumed that no auto-advance occurs. |
56+
| spd (Transition Speed) |Specifies the transition speed that is to be used when transitioning from the current slide to the next. |
57+
58+
[*Example*: Consider the following example
59+
60+
```xml
61+
<p:transition spd="slow" advClick="1" advTm="3000">
62+
<p:randomBar dir="horz"/>
63+
</p:transition>
64+
```
65+
In the above example, the transition speed `<speed>` is set to slow (available options: slow, med, fast). Advance on Click `<advClick>` is set to true, and Advance after time `<advTm>` is set to 3000 milliseconds. The Random Bar child element `<randomBar>` describes the randomBar slide transition effect, which uses a set of randomly placed horizontal `<dir="horz">` or vertical `<dir="vert">` bars on the slide that continue to be added until the new slide is fully shown. *end example*]
66+
67+
A full list of Transition's child elements can be viewed here: <xref:DocumentFormat.OpenXml.Presentation.Transition>
68+
69+
## The Structure of the Alternate Content
70+
71+
Office Open XML defines a mechanism for the storage of content that is not defined by the ISO/IEC 29500 Office Open XML specification, such as extensions developed by future software applications that leverage the Office Open XML formats. This mechanism allows for the storage of a series of alternative representations of content, from which the consuming application can use the first alternative whose requirements are met.
72+
73+
Consider an application that creates a new transition object intended to specify the duration of the transition. This functionality is not defined in the Office Open XML specification. Using an AlternateContent block as follows allows specifying the duration `<p14:dur>` in milliseconds.
74+
75+
[*Example*:
76+
```xml
77+
<mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
78+
xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main">
79+
<mc:Choice Requires="p14">
80+
<p:transition spd="slow" p14:dur="2000" advClick="1" advTm="3000">
81+
<p:randomBar/>
82+
</p:transition>
83+
</mc:Choice>
84+
<mc:Fallback>
85+
<p:transition spd="slow" advClick="1" advTm="3000">
86+
<p:randomBar/>
87+
</p:transition>
88+
</mc:Fallback>
89+
</mc:AlternateContent>
90+
```
91+
92+
The Choice element in the above example requires the <xref:DocumentFormat.OpenXml.Linq.P14.dur*> attribute to specify the duration of the transition, and the Fallback element allows clients that do not support this namespace to see an appropriate alternative representation. *end example*]
93+
94+
More details on the P14 class can be found here: <xref:DocumentFormat.OpenXml.Linq.P14>
95+
96+
## How the Sample Code Works ##
97+
After opening the presentation file for read/write access in the using statement, the code gets the presentation part from the presentation document. Then, it retrieves the relationship IDs of all slides in the presentation and gets the slides part from the relationship ID. The code then checks if there are no existing transitions set on the slides and replaces them with a new RandomBarTransition.
98+
99+
### [C#](#tab/cs-2)
100+
[!code-csharp[](../../samples/presentation/add_transition/cs/Program.cs#snippet2)]
101+
102+
### [Visual Basic](#tab/vb-2)
103+
[!code-vb[](../../samples/presentation/add_transition/vb/Program.vb#snippet2)]
104+
***
105+
106+
If there are currently no transitions on the slide, code creates new transition. In both cases as a fallback transition,
107+
RandomBarTransition is used but without `P14:dur`(duration) to allow grater support for clients that aren't supporting this namespace
108+
109+
### [C#](#tab/cs-3)
110+
[!code-csharp[](../../samples/presentation/add_transition/cs/Program.cs#snippet3)]
111+
112+
### [Visual Basic](#tab/vb-3)
113+
[!code-vb[](../../samples/presentation/add_transition/vb/Program.vb#snippet3)]
114+
***
115+
116+
## Sample Code
117+
118+
Following is the complete sample code that you can use to add RandomBarTransition to all slides.
119+
120+
### [C#](#tab/cs)
121+
[!code-csharp[](../../samples/presentation/add_transition/cs/Program.cs#snippet0)]
122+
123+
### [Visual Basic](#tab/vb)
124+
[!code-vb[](../../samples/presentation/add_transition/vb/Program.vb#snippet0)]
125+
***
126+
127+
## See also
128+
129+
- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk)
130+
131+
132+
133+

docs/presentation/overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ This section provides how-to topics for working with presentation documents usin
5454
- [Open a presentation document for read-only access](how-to-open-a-presentation-document-for-read-only-access.md)
5555

5656
- [Retrieve the number of slides in a presentation document](how-to-retrieve-the-number-of-slides-in-a-presentation-document.md)
57+
58+
- [Add a transition to a slides in a presentation](how-to-add-transitions-between-slides-in-a-presentation.md)
5759

5860
- [Working with animation](working-with-animation.md)
5961

docs/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
href: presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md
8181
- name: Structure of a PresentationML document
8282
href: presentation/structure-of-a-presentationml-document.md
83+
- name: Add a transition to a slide in a presentation
84+
href: presentation/how-to-add-transitions-between-slides-in-a-presentation.md
8385
- name: Working with animation
8486
href: presentation/working-with-animation.md
8587
- name: Working with comments
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using DocumentFormat.OpenXml;
2+
using DocumentFormat.OpenXml.Presentation;
3+
using DocumentFormat.OpenXml.Packaging;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System;
7+
8+
// <Snippet0>
9+
AddTransmitionToSlides(args[0]);
10+
static void AddTransmitionToSlides(string filePath)
11+
{
12+
// <Snippet1>
13+
using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
14+
// </Snippet1>
15+
{
16+
17+
// Check if the presentation part and slide list are available
18+
if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
19+
{
20+
throw new NullReferenceException("Presentation part is empty or there are no slides");
21+
}
22+
23+
// Get the presentation part
24+
PresentationPart presentationPart = presentationDocument.PresentationPart;
25+
26+
// Get the list of slide IDs
27+
OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;
28+
29+
// <Snippet2>
30+
// Define the transition start time and duration in milliseconds
31+
string startTransitionAfterMs = "3000", durationMs = "2000";
32+
33+
// Set to true if you want to advance to the next slide on mouse click
34+
bool advanceOnClick = true;
35+
36+
// Iterate through each slide ID to get slides parts
37+
foreach (SlideId slideId in slidesIds)
38+
{
39+
// Get the relationship ID of the slide
40+
string? relId = slideId!.RelationshipId!.ToString();
41+
42+
if (relId == null)
43+
{
44+
throw new NullReferenceException("RelationshipId not found");
45+
}
46+
47+
// Get the slide part using the relationship ID
48+
SlidePart? slidePart = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId);
49+
50+
// Remove existing transitions if any
51+
if (slidePart.Slide.Transition != null)
52+
{
53+
slidePart.Slide.Transition.Remove();
54+
}
55+
56+
// Check if there are any AlternateContent elements
57+
if (slidePart!.Slide.Descendants<AlternateContent>().ToList().Count > 0)
58+
{
59+
// Get all AlternateContent elements
60+
List<AlternateContent> alternateContents = [.. slidePart.Slide.Descendants<AlternateContent>()];
61+
foreach (AlternateContent alternateContent in alternateContents)
62+
{
63+
// Remove transitions in AlternateContentChoice within AlternateContent
64+
List<OpenXmlElement> childElements = alternateContent.ChildElements.ToList();
65+
66+
foreach (OpenXmlElement element in childElements)
67+
{
68+
List<Transition> transitions = element.Descendants<Transition>().ToList();
69+
foreach (Transition transition in transitions)
70+
{
71+
transition.Remove();
72+
}
73+
}
74+
// Add new transitions to AlternateContentChoice and AlternateContentFallback
75+
alternateContent!.GetFirstChild<AlternateContentChoice>();
76+
Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
77+
Transition fallbackTransition = new Transition(new RandomBarTransition()) {AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
78+
alternateContent!.GetFirstChild<AlternateContentChoice>()!.Append(choiceTransition);
79+
alternateContent!.GetFirstChild<AlternateContentFallback>()!.Append(fallbackTransition);
80+
}
81+
}
82+
// </Snippet2>
83+
84+
// <Snippet3>
85+
// Add transition if there is none
86+
else
87+
{
88+
// Check if there is a transition appended to the slide and set it to null
89+
if (slidePart.Slide.Transition != null)
90+
{
91+
slidePart.Slide.Transition = null;
92+
}
93+
// Create a new AlternateContent element
94+
AlternateContent alternateContent = new AlternateContent();
95+
alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
96+
97+
// Create a new AlternateContentChoice element and add the transition
98+
AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
99+
Transition choiceTransition = new Transition(new RandomBarTransition()) { Duration = durationMs, AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
100+
Transition fallbackTransition = new Transition(new RandomBarTransition()) { AdvanceAfterTime = startTransitionAfterMs, AdvanceOnClick = advanceOnClick, Speed = TransitionSpeedValues.Slow };
101+
alternateContentChoice.Append(choiceTransition);
102+
103+
// Create a new AlternateContentFallback element and add the transition
104+
AlternateContentFallback alternateContentFallback = new AlternateContentFallback(fallbackTransition);
105+
alternateContentFallback.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
106+
alternateContentFallback.AddNamespaceDeclaration("p16", "http://schemas.microsoft.com/office/powerpoint/2015/main");
107+
alternateContentFallback.AddNamespaceDeclaration("adec", "http://schemas.microsoft.com/office/drawing/2017/decorative");
108+
alternateContentFallback.AddNamespaceDeclaration("a16", "http://schemas.microsoft.com/office/drawing/2014/main");
109+
110+
// Append the AlternateContentChoice and AlternateContentFallback to the AlternateContent
111+
alternateContent.Append(alternateContentChoice);
112+
alternateContent.Append(alternateContentFallback);
113+
slidePart.Slide.Append(alternateContent);
114+
}
115+
// </Snippet3>
116+
}
117+
}
118+
}
119+
// </Snippet0>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Project Sdk="Microsoft.NET.Sdk"/>

0 commit comments

Comments
 (0)