Skip to content

Commit 638c8fb

Browse files
authored
Merge branch main into live (#340)
1 parent e7a243e commit 638c8fb

File tree

12 files changed

+1983
-99
lines changed

12 files changed

+1983
-99
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: 0265732e-d81a-4654-b0ba-d9d87e544f7c
8+
title: 'How to: Add an audio file 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: 03/31/2025
15+
ms.localizationpriority: medium
16+
---
17+
18+
# Add an audio file 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 an audio file to the last 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 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 <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open*> method that uses a file path, and a
29+
Boolean value as the second parameter to specify whether a document is editable. To open a document for read/write, specify the value `true` for this parameter as shown in the following
30+
`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.
31+
32+
### [C#](#tab/cs-1)
33+
[!code-csharp[](../../samples/presentation/add_audio/cs/Program.cs#snippet1)]
34+
35+
### [Visual Basic](#tab/vb-1)
36+
[!code-vb[](../../samples/presentation/add_audio/vb/Program.vb#snippet1)]
37+
***
38+
39+
40+
[!include[Using Statement](../includes/presentation/using-statement.md)] `ppt`.
41+
42+
43+
## The Structure of the Audio From File
44+
45+
The PresentationML document consists of a number of parts, among which is the Picture (`<pic/>`) element.
46+
47+
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.
48+
49+
Audio File (`<audioFile/>`) specifies the presence of an audio file. This element is specified within the non-visual properties of an object. The audio shall be attached to an object as this is how it is represented within the document. The actual playing of the audio however is done within the timing node list that is specified under the timing element.
50+
51+
Consider the following ``Picture`` object that has an audio file attached to it.
52+
53+
```xml
54+
<p:pic>
55+
<p:nvPicPr>
56+
<p:cNvPr id="7" name="Rectangle 6">
57+
<a:hlinkClick r:id="" action="ppaction://media"/>
58+
</p:cNvPr>
59+
<p:cNvPicPr>
60+
<a:picLocks noRot="1"/>
61+
</p:cNvPicPr>
62+
<p:nvPr>
63+
<a:audioFile r:link="rId1"/>
64+
</p:nvPr>
65+
</p:nvPicPr>
66+
</p:pic>
67+
```
68+
69+
In the above example, we see that there is a single audioFile 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 audioFile element from within the timing node list. The Linked relationship id is used to retrieve the actual audio file for playback purposes.
70+
71+
&copy; [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)]
72+
73+
The following XML Schema fragment defines the contents of audioFile.
74+
75+
```xml
76+
<xsd:complexType name="CT_TLMediaNodeAudio">
77+
<xsd:sequence>
78+
<xsd:element name="cMediaNode" type="CT_TLCommonMediaNodeData" minOccurs="1" maxOccurs="1"/>
79+
</xsd:sequence>
80+
<xsd:attribute name="isNarration" type="xsd:boolean" use="optional" default="false"/>
81+
</xsd:complexType>
82+
```
83+
84+
## How the Sample Code Works
85+
86+
After opening the presentation file for read/write access in the `using` statement, the code gets the presentation
87+
part from the presentation document. Then it gets the relationship ID of
88+
the last slide, and gets the slide part from the relationship ID.
89+
90+
91+
### [C#](#tab/cs-2)
92+
[!code-csharp[](../../samples/presentation/add_audio/cs/Program.cs#snippet2)]
93+
94+
### [Visual Basic](#tab/vb-2)
95+
[!code-vb[](../../samples/presentation/add_audio/vb/Program.vb#snippet2)]
96+
***
97+
98+
The code first creates a media data part for the audio file to be added. With the audio file stream open, it feeds the media data part object. Next, audio and media relationship references are added to the slide using the provided embedId for future reference to the audio file and mediaEmbedId for media reference.
99+
100+
An image part is then added with a sample picture to be used as a placeholder for the audio. 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 `<audioFile/>` 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)]
101+
102+
### [C#](#tab/cs-3)
103+
[!code-csharp[](../../samples/presentation/add_audio/cs/Program.cs#snippet3)]
104+
105+
### [Visual Basic](#tab/vb-3)
106+
[!code-vb[](../../samples/presentation/add_audio/vb/Program.vb#snippet3)]
107+
***
108+
109+
Next the Media(CT_Media) element is created with use of the 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 an placeholder image in the Image Part created previously.
110+
111+
### [C#](#tab/cs-4)
112+
[!code-csharp[](../../samples/presentation/add_audio/cs/Program.cs#snippet4)]
113+
114+
### [Visual Basic](#tab/vb-4)
115+
[!code-vb[](../../samples/presentation/add_audio/vb/Program.vb#snippet4)]
116+
***
117+
118+
All other elements such as 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 includes audio is added to the ShapeTree(`<sp/>`) of the slide.
119+
120+
Following is the complete sample code that you can use to add audio to the slide.
121+
122+
## Sample Code
123+
124+
### [C#](#tab/cs)
125+
[!code-csharp[](../../samples/presentation/add_audio/cs/Program.cs#snippet0)]
126+
127+
### [Visual Basic](#tab/vb)
128+
[!code-vb[](../../samples/presentation/add_audio/vb/Program.vb#snippet0)]
129+
***
130+
131+
## See also
132+
133+
- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk)

docs/toc.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
- name: Getting started
66
items:
77
- name: Overview
8-
href: getting-started.md
9-
- name: About the Open XML SDK
8+
href: getting-started.md
9+
- name: About the Open XML SDK
1010
href: about-the-open-xml-sdk.md
11-
- name: What's new in the Open XML SDK
11+
- name: What's new in the Open XML SDK
1212
href: what-s-new-in-the-open-xml-sdk.md
1313
- name: Open XML SDK design considerations
1414
href: open-xml-sdk-design-considerations.md
@@ -21,7 +21,7 @@
2121
- name: Custom SDK Features
2222
href: general/features.md
2323
- name: Introduction to markup compatibility
24-
href: general/introduction-to-markup-compatibility.md
24+
href: general/introduction-to-markup-compatibility.md
2525
- name: Add a new document part that receives a relationship ID to a package
2626
href: general/how-to-add-a-new-document-part-that-receives-a-relationship-id-to-a-package.md
2727
- name: Add a new document part to a package
@@ -43,7 +43,9 @@
4343
- name: Presentations
4444
items:
4545
- name: Overview
46-
href: presentation/overview.md
46+
href: presentation/overview.md
47+
- name: Add an audio file to a slide in a presentation
48+
href: presentation/how-to-add-an-audio-to-a-slide-in-a-presentation.md
4749
- name: Add a comment to a slide in a presentation
4850
href: presentation/how-to-add-a-comment-to-a-slide-in-a-presentation.md
4951
- name: Apply a theme to a presentation
@@ -73,7 +75,7 @@
7375
- name: Open a presentation document for read-only access
7476
href: presentation/how-to-open-a-presentation-document-for-read-only-access.md
7577
- name: Retrieve the number of slides in a presentation document
76-
href: presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md
78+
href: presentation/how-to-retrieve-the-number-of-slides-in-a-presentation-document.md
7779
- name: Structure of a PresentationML document
7880
href: presentation/structure-of-a-presentationml-document.md
7981
- name: Working with animation
@@ -102,6 +104,8 @@
102104
href: spreadsheet/how-to-add-custom-ui-to-a-spreadsheet-document.md
103105
- name: Calculate the sum of a range of cells in a spreadsheet document
104106
href: spreadsheet/how-to-calculate-the-sum-of-a-range-of-cells-in-a-spreadsheet-document.md
107+
- name: Copy a Worksheet Using SAX (Simple API for XML)
108+
href: spreadsheet/how-to-copy-a-worksheet-with-sax.md
105109
- name: Create a spreadsheet document by providing a file name
106110
href: spreadsheet/how-to-create-a-spreadsheet-document-by-providing-a-file-name.md
107111
- name: Delete text from a cell in a spreadsheet
@@ -153,7 +157,7 @@
153157
- name: Overview
154158
href: word/overview.md
155159
- name: Structure of a WordprocessingML document
156-
href: word/structure-of-a-wordprocessingml-document.md
160+
href: word/structure-of-a-wordprocessingml-document.md
157161
- name: Accept all revisions in a word processing document
158162
href: word/how-to-accept-all-revisions-in-a-word-processing-document.md
159163
- name: Add tables to word processing documents
@@ -192,6 +196,8 @@
192196
href: word/how-to-remove-hidden-text-from-a-word-processing-document.md
193197
- name: Remove the headers and footers from a word processing document
194198
href: word/how-to-remove-the-headers-and-footers-from-a-word-processing-document.md
199+
- name: Replace Text in a Word Document Using SAX (Simple API for XML)'
200+
href: word/how-to-replace-text-in-a-word-document-with-sax.md
195201
- name: Replace the header in a word processing document
196202
href: word/how-to-replace-the-header-in-a-word-processing-document.md
197203
- name: Replace the styles parts in a word processing document

docs/word/how-to-accept-all-revisions-in-a-word-processing-document.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Using the Open XML SDK, you can create document structure and content using stro
4646

4747
## ParagraphPropertiesChange Element
4848

49-
When you accept a revision mark, you change the properties of a paragraph either by deleting an existing text or inserting a new text. In the following sections, you read about three elements that are used in the code to change the paragraph contents, mainly, `<w: pPrChange>` (Revision Information for Paragraph Properties), `<w:del>` (Deleted Paragraph), and `<w:ins>` (Inserted Table Row) elements.
49+
When you accept a revision mark, you change the properties of a paragraph either by deleting existing text or inserting new text. In the following sections, you read about three elements that are used in the code to change the paragraph contents, mainly, `<w: pPrChange>` (Revision Information for Paragraph Properties), `<w:del>` (Deleted Paragraph), and `<w:ins>` (Inserted Table Row) elements.
5050

5151
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification introduces the `ParagraphPropertiesChange` element (`pPrChange`).
5252

@@ -86,9 +86,12 @@ This element specifies that the paragraph mark delimiting the end of a paragraph
8686

8787
Consider a document consisting of two paragraphs (with each paragraph delimited by a pilcrow ¶):
8888

89-
![Two paragraphs each delimited by a pilcrow](../media/w-delparagraphs01.gif) If the physical character delimiting the end of the first paragraph is deleted and this change is tracked as a revision, the following will result:
89+
![Two paragraphs each delimited by a pilcrow](../media/w-delparagraphs01.gif)
90+
91+
If the physical character delimiting the end of the first paragraph is deleted and this change is tracked as a revision, the following will result:
9092

9193
![Two paragraphs delimited by a single pilcrow](../media/w-delparagraphs02.gif)
94+
9295
This revision is represented using the following WordprocessingML:
9396

9497
```xml
@@ -162,12 +165,87 @@ a revision.
162165

163166
© [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)]
164167

168+
## Move From Element
169+
170+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
171+
introduces the Move From element (`moveFrom`).
172+
173+
### moveFrom (Move Source Paragraph)
174+
175+
This element indicates that the parent paragraph has been relocated
176+
from this position and marked as a revision. This does not affect the revision
177+
status of the paragraph's content and pertains solely to the paragraph's
178+
existence as a distinct entity.
179+
180+
Consider a WordprocessingML document where a paragraph of text is moved down
181+
within the document. This relocated paragraph would be represented using the
182+
following WordprocessingML markup:
183+
184+
```xml
185+
<w:moveFromRangeStart w:id="0" w:name="aMove"/>
186+
<w:p>
187+
<w:pPr>
188+
<w:rPr>
189+
<w:moveFrom w:id="1" … />
190+
</w:rPr>
191+
</w:pPr>
192+
…</w:p>
193+
</w:moveFromRangeEnd w:id="0"/>
194+
```
195+
196+
### moveFromRangeStart (Move Source Location Container - Start)
197+
198+
This element marks the beginning of a region where the move source contents are part of a single named move.
199+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
200+
introduces the Move From Range Start element (`moveFromRangeStart`).
201+
202+
### moveFromRangeEnd (Move Source Location Container - End)
203+
204+
This element marks the end of a region where the move source contents are part of a single named move.
205+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
206+
introduces the Move From Range End element (`moveFromRangeEnd`).
207+
208+
## The Moved To Element
209+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
210+
introduces the MoveTo element (`moveTo`).
211+
212+
### moveTo (Move Destination Paragraph)
213+
This element specifies that the parent paragraph has been moved to this location and tracked as a revision.
214+
This does not imply anything about the revision state of the contents of the paragraph, and applies only to the existence of the paragraph as its own unique paragraph.
215+
216+
Consider a WordprocessingML document in which a paragraph of text is moved down in the document.
217+
This moved paragraph would be represented using the following WordprocessingML markup:
218+
219+
```xml
220+
<w:moveToRangeStart w:id="0" w:name="aMove"/>
221+
<w:p>
222+
<w:pPr>
223+
<w:rPr>
224+
<w:moveTo w:id="1" … />
225+
</w:rPr>
226+
</w:pPr>
227+
…</w:p>
228+
</w:moveToRangeEnd w:id="0"/>
229+
```
230+
231+
### moveToRangeStart (Move Destination Location Container - Start)
232+
233+
This element specifies the start of the region whose move destination contents are part of a single named move.
234+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
235+
introduces the Move To Range Start element (`moveToRangeStart`).
236+
237+
### moveToRangeEnd (Move Destination Location Container - End)
238+
239+
This element specifies the end of a region whose move destination contents are part of a single named move.
240+
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
241+
introduces the Move To Range End element (`moveToRangeEnd`).
242+
165243
## Sample Code
166244

167245
The following code example shows how to accept the entire revisions in a
168246
word processing document.
169247

170-
After you have run the program, open the word processing file to make
248+
After you have run the program, open the word processing document to make
171249
sure that all revision marks have been accepted.
172250

173251
### [C#](#tab/cs)

0 commit comments

Comments
 (0)