|
| 1 | +--- |
| 2 | + |
| 3 | +api_name: |
| 4 | +- Microsoft.Office.DocumentFormat.OpenXML.Packaging |
| 5 | +api_type: |
| 6 | +- schema |
| 7 | +ms.assetid: 536c94b5-dd25-4173-ad6a-b72b95dd7f31 |
| 8 | +title: 'How to: Add a video to a slide 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 a video to a slide in a presentation |
| 19 | + |
| 20 | +This topic shows how to use the classes in the Open XML SDK for |
| 21 | +Office to add a video to the first slide in a presentation |
| 22 | +programmatically. |
| 23 | + |
| 24 | +## Getting a Presentation Object |
| 25 | + |
| 26 | +In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument> class represents a |
| 27 | +presentation document package. To work with a presentation document, |
| 28 | +first create an instance of the **PresentationDocument** class, and then work with |
| 29 | +that instance. To create the class instance from the document call the |
| 30 | +<xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open*> method that uses a file path, and a |
| 31 | +Boolean value as the second parameter to specify whether a document is |
| 32 | +editable. To open a document for read/write, specify the value `true` for this parameter as shown in the following |
| 33 | +`using` statement. In this code, the file |
| 34 | +parameter is a string that represents the path for the file from which |
| 35 | +you want to open the document. |
| 36 | + |
| 37 | +### [C#](#tab/cs-1) |
| 38 | +[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet1)] |
| 39 | + |
| 40 | +### [Visual Basic](#tab/vb-1) |
| 41 | +[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet1)] |
| 42 | +*** |
| 43 | + |
| 44 | + |
| 45 | +[!include[Using Statement](../includes/presentation/using-statement.md)] `ppt`. |
| 46 | + |
| 47 | + |
| 48 | +## The Structure of the Video From File |
| 49 | + |
| 50 | +The PresentationML document consists of a number of parts, among which is the Picture (`<pic/>`) element. |
| 51 | + |
| 52 | +The following text from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification introduces the overall form of a `PresentationML` package. |
| 53 | + |
| 54 | +Video File (`<videoFile/>`) specifies the presence of a video file. It is defined within the non-visual properties of an object. The video shall be attached to an object as this is how it is represented within the document. The actual playing of the video however is done within the timing node list that is specified under the timing element. |
| 55 | + |
| 56 | +Consider the following `Picture` object that has a video attached to it. |
| 57 | + |
| 58 | +```xml |
| 59 | +<p:pic> |
| 60 | + <p:nvPicPr> |
| 61 | + <p:cNvPr id="7" name="Rectangle 6"> |
| 62 | + <a:hlinkClick r:id="" action="ppaction://media"/> |
| 63 | + </p:cNvPr> |
| 64 | + <p:cNvPicPr> |
| 65 | + <a:picLocks noRot="1"/> |
| 66 | + </p:cNvPicPr> |
| 67 | + <p:nvPr> |
| 68 | + <a:videoFile r:link="rId1"/> |
| 69 | + </p:nvPr> |
| 70 | + </p:nvPicPr> |
| 71 | +</p:pic> |
| 72 | +``` |
| 73 | + |
| 74 | +In the above example, we see that there is a single videoFile element attached to this picture. This picture is placed within the document just as a normal picture or shape would be. The id of this picture, namely 7 in this case, is used to refer to this videoFile element from within the timing node list. The Linked relationship id is used to retrieve the actual video file for playback purposes. |
| 75 | + |
| 76 | +© [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)] |
| 77 | + |
| 78 | +The following XML Schema fragment defines the contents of videoFile. |
| 79 | + |
| 80 | +```xml |
| 81 | +<xsd:complexType name="CT_TLMediaNodeVideo"> |
| 82 | + <xsd:sequence> |
| 83 | + <xsd:element name="cMediaNode" type="CT_TLCommonMediaNodeData" minOccurs="1" maxOccurs="1"/> |
| 84 | + </xsd:sequence> |
| 85 | + <xsd:attribute name="fullScrn" type="xsd:boolean" use="optional" default="false"/> |
| 86 | +</xsd:complexType> |
| 87 | +``` |
| 88 | + |
| 89 | +## How the Sample Code Works |
| 90 | + |
| 91 | +After opening the presentation file for read/write access in the `using` statement, the code gets the presentation |
| 92 | +part from the presentation document. Then it gets the relationship ID of |
| 93 | +the last slide, and gets the slide part from the relationship ID. |
| 94 | + |
| 95 | + |
| 96 | +### [C#](#tab/cs-2) |
| 97 | +[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet2)] |
| 98 | + |
| 99 | +### [Visual Basic](#tab/vb-2) |
| 100 | +[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet2)] |
| 101 | +*** |
| 102 | + |
| 103 | +The code first creates a media data part for the video file to be added. With the video file stream open, it feeds the media data part object. Next, video and media relationship references are added to the slide using the provided embedId for future reference to the video file and mediaEmbedId for media reference. |
| 104 | + |
| 105 | +An image part is then added with a sample picture to be used as a placeholder for the video. A picture object is created with various elements, such as Non-Visual Drawing Properties (`<cNvPr/>`), which specify non-visual canvas properties. This allows for additional information that does not affect the appearance of the picture to be stored. The `<videoFile/>` element, explained above, is also included. The HyperLinkOnClick (`<hlinkClick/>`) element specifies the on-click hyperlink information to be applied to a run of text or image. When the hyperlink text or image is clicked, the link is fetched. Non-Visual Picture Drawing Properties (`<cNvPicPr/>`) specify the non-visual properties for the picture canvas. For a detailed explanation of the elements used, please refer to [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] |
| 106 | + |
| 107 | +### [C#](#tab/cs-3) |
| 108 | +[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet3)] |
| 109 | + |
| 110 | +### [Visual Basic](#tab/vb-3) |
| 111 | +[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet3)] |
| 112 | +*** |
| 113 | + |
| 114 | +Next Media(CT_Media) element is created with use of previously referenced mediaEmbedId(Embedded Picture Reference). The Blip element is also added; this element specifies the existence of an image (binary large image or picture) and contains a reference to the image data. Blip's Embed attribute is used to specify a placeholder image in the Image Part created previously. |
| 115 | + |
| 116 | +### [C#](#tab/cs-4) |
| 117 | +[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet4)] |
| 118 | + |
| 119 | +### [Visual Basic](#tab/vb-4) |
| 120 | +[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet4)] |
| 121 | +*** |
| 122 | + |
| 123 | +All other elements such Offset(`<off/>`), Stretch(`<stretch/>`), FillRectangle(`<fillRect/>`), are appended to the ShapeProperties(`<spPr/>`) and ShapeProperties are appended to the Picture element(`<pic/>`). Finally the picture element that incudes video is added to the ShapeTree(`<sp/>`) of the slide. |
| 124 | + |
| 125 | +Following is the complete sample code that you can use to add video to the slide. |
| 126 | + |
| 127 | +## Sample Code |
| 128 | + |
| 129 | +### [C#](#tab/cs) |
| 130 | +[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet0)] |
| 131 | + |
| 132 | +### [Visual Basic](#tab/vb) |
| 133 | +[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet0)] |
| 134 | +*** |
| 135 | + |
| 136 | +## See also |
| 137 | + |
| 138 | +- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk) |
0 commit comments