|
| 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 | + |
0 commit comments