Skip to content

Commit f1f74eb

Browse files
authored
Add video (#334)
1 parent 5f0c0e1 commit f1f74eb

File tree

10 files changed

+476
-4
lines changed

10 files changed

+476
-4
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
&copy; [!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)

docs/presentation/overview.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ This section provides how-to topics for working with presentation documents usin
2222

2323
- [Structure of a PresentationML document](structure-of-a-presentationml-document.md)
2424

25-
- [Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)
25+
- [Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)
26+
27+
- [Add a video to a slide in a presentation](how-to-add-a-video-to-a-slide-in-a-presentation.md)
2628

2729
- [Apply a theme to a presentation](how-to-apply-a-theme-to-a-presentation.md)
2830

2931
- [Change the fill color of a shape in a presentation](how-to-change-the-fill-color-of-a-shape-in-a-presentation.md)
3032

3133
- [Create a presentation document by providing a file name](how-to-create-a-presentation-document-by-providing-a-file-name.md)
3234

33-
- [Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md)
35+
- [Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md)
3436

3537
- [Delete a slide from a presentation](how-to-delete-a-slide-from-a-presentation.md)
3638

docs/presentation/working-with-comments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,4 @@ article.
193193
[About the Open XML SDK for Office](../about-the-open-xml-sdk.md)
194194
[How to: Create a Presentation by Providing a File Name](how-to-create-a-presentation-document-by-providing-a-file-name.md)
195195
[How to: Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)
196-
[How to: Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md)
196+
[How to: Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md)

docs/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@
4848
href: presentation/how-to-add-an-audio-to-a-slide-in-a-presentation.md
4949
- name: Add a comment to a slide in a presentation
5050
href: presentation/how-to-add-a-comment-to-a-slide-in-a-presentation.md
51+
- name: Add a video to a slide in a presentation
52+
href: presentation/how-to-add-a-video-to-a-slide-in-a-presentation.md
5153
- name: Apply a theme to a presentation
5254
href: presentation/how-to-apply-a-theme-to-a-presentation.md
5355
- name: Change the fill color of a shape in a presentation
5456
href: presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md
5557
- name: Create a presentation document by providing a file name
5658
href: presentation/how-to-create-a-presentation-document-by-providing-a-file-name.md
5759
- name: Delete all the comments by an author from all the slides in a presentation
58-
href: presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md
60+
href: presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md
5961
- name: Delete a slide from a presentation
6062
href: presentation/how-to-delete-a-slide-from-a-presentation.md
6163
- name: Get all the external hyperlinks in a presentation
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using DocumentFormat.OpenXml;
2+
using DocumentFormat.OpenXml.Presentation;
3+
using A = DocumentFormat.OpenXml.Drawing;
4+
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
5+
using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree;
6+
using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties;
7+
using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties;
8+
using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties;
9+
using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties;
10+
using Picture = DocumentFormat.OpenXml.Presentation.Picture;
11+
using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill;
12+
using DocumentFormat.OpenXml.Packaging;
13+
using ApplicationNonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties;
14+
using System;
15+
using System.IO;
16+
using System.Linq;
17+
18+
// <Snippet0>
19+
AddVideo(args[0], args[1], args[2]);
20+
21+
static void AddVideo(string filePath, string videoFilePath, string coverPicPath)
22+
{
23+
24+
string imgEmbedId = "rId4", embedId = "rId3", mediaEmbedId = "rId2";
25+
UInt32Value shapeId = 5;
26+
// <Snippet1>
27+
using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
28+
// </Snippet1>
29+
{
30+
31+
if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
32+
{
33+
throw new NullReferenceException("Presentation Part is empty or there are no slides in it");
34+
}
35+
// <Snippet2>
36+
//Get presentation part
37+
PresentationPart presentationPart = presentationDocument.PresentationPart;
38+
39+
//Get slides ids.
40+
OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;
41+
42+
//Get relationsipId of the last slide
43+
string? videoSldRelationshipId = ((SlideId) slidesIds[slidesIds.ToArray().Length - 1]).RelationshipId;
44+
45+
if (videoSldRelationshipId == null)
46+
{
47+
throw new NullReferenceException("Slide id not found");
48+
}
49+
50+
//Get slide part by relationshipID
51+
SlidePart? slidePart = (SlidePart) presentationPart.GetPartById(videoSldRelationshipId);
52+
// </Snippet2>
53+
54+
// <Snippet3>
55+
// Create video Media Data Part (content type, extension)
56+
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("video/mp4", ".mp4");
57+
58+
//Get the video file and feed the stream
59+
using (Stream mediaDataPartStream = File.OpenRead(videoFilePath))
60+
{
61+
mediaDataPart.FeedData(mediaDataPartStream);
62+
}
63+
//Adds a VideoReferenceRelationship to the MainDocumentPart
64+
slidePart.AddVideoReferenceRelationship(mediaDataPart, embedId);
65+
66+
//Adds a MediaReferenceRelationship to the SlideLayoutPart
67+
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);
68+
69+
NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "video" };
70+
A.VideoFromFile videoFromFile = new A.VideoFromFile() { Link = embedId };
71+
72+
ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
73+
appNonVisualDrawingProperties.Append(videoFromFile);
74+
75+
//adds sample image to the slide with id to be used as reference in blip
76+
ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId);
77+
using (Stream data = File.OpenRead(coverPicPath))
78+
{
79+
imagePart.FeedData(data);
80+
}
81+
82+
if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null)
83+
{
84+
throw new NullReferenceException("Presentation shape tree is empty");
85+
}
86+
87+
//Getting existing shape tree object from PowerPoint
88+
ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
89+
90+
// specifies the existence of a picture within a presentation.
91+
// It can have non-visual properties, a picture fill as well as shape properties attached to it.
92+
Picture picture = new Picture();
93+
NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties();
94+
95+
A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" };
96+
nonVisualDrawingProperties.Append(hyperlinkOnClick);
97+
98+
NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties();
99+
A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true };
100+
nonVisualPictureDrawingProperties.Append(pictureLocks);
101+
102+
ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new ApplicationNonVisualDrawingPropertiesExtensionList();
103+
ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" };
104+
// </Snippet3>
105+
106+
// <Snippet4>
107+
P14.Media media = new() { Embed = mediaEmbedId };
108+
media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
109+
110+
appNonVisualDrawingPropertiesExtension.Append(media);
111+
appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension);
112+
appNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList);
113+
114+
nonVisualPictureProperties.Append(nonVisualDrawingProperties);
115+
nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
116+
nonVisualPictureProperties.Append(appNonVisualDrawingProperties);
117+
118+
//Prepare shape properties to display picture
119+
BlipFill blipFill = new BlipFill();
120+
A.Blip blip = new A.Blip() { Embed = imgEmbedId };
121+
// </Snippet4>
122+
123+
A.Stretch stretch = new A.Stretch();
124+
A.FillRectangle fillRectangle = new A.FillRectangle();
125+
A.Transform2D transform2D = new A.Transform2D();
126+
A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L };
127+
A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L };
128+
A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
129+
A.AdjustValueList adjValueList = new A.AdjustValueList();
130+
131+
stretch.Append(fillRectangle);
132+
blipFill.Append(blip);
133+
blipFill.Append(stretch);
134+
transform2D.Append(offset);
135+
transform2D.Append(extents);
136+
presetGeometry.Append(adjValueList);
137+
138+
ShapeProperties shapeProperties = new ShapeProperties();
139+
shapeProperties.Append(transform2D);
140+
shapeProperties.Append(presetGeometry);
141+
142+
//adds all elements to the slide's shape tree
143+
picture.Append(nonVisualPictureProperties);
144+
picture.Append(blipFill);
145+
picture.Append(shapeProperties);
146+
147+
shapeTree.Append(picture);
148+
}
149+
}
150+
// </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)