Skip to content

Commit

Permalink
closes OfficeDev#188
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Dec 6, 2024
1 parent 1624302 commit bd191aa
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ms.suite: office
ms.author: o365devx
author: o365devx
ms.topic: conceptual
ms.date: 11/01/2017
ms.date: 12/04/2024
ms.localizationpriority: medium
---
# Get all the text in all slides in a presentation
Expand All @@ -26,33 +26,23 @@ all of the text in all of the slides in a presentation programmatically.

In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument> class represents a
presentation document package. To work with a presentation document,
first create an instance of the **PresentationDocument** class, and then work with
first create an instance of the `PresentationDocument` class, and then work with
that instance. To create the class instance from the document call the
<xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open#documentformat-openxml-packaging-presentationdocument-open(system-string-system-boolean)>
method that uses a file path, and a Boolean value as the second
parameter to specify whether a document is editable. To open a document
for read/write access, assign the value **true** to this parameter; for read-only access
assign it the value **false** as shown in the
following **using** statement. In this code,
the **presentationFile** parameter is a string
for read/write access, assign the value `true` to this parameter; for read-only access
assign it the value `false` as shown in the
following `using` statement. In this code,
the `presentationFile` parameter is a string
that represents the path for the file from which you want to open the
document.

### [C#](#tab/cs-0)
```csharp
// Open the presentation as read-only.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
{
// Insert other code here.
}
```

### [Visual Basic](#tab/vb-0)
```vb
Using presentationDocument As PresentationDocument = PresentationDocument.Open(presentationFile, False)
' Insert other code here.
End Using
```
### [C#](#tab/cs-1)
[!code-csharp[](../../samples/presentation/get_all_the_text_all_slides/cs/Program.cs#snippet1)]

### [Visual Basic](#tab/vb-1)
[!code-vb[](../../samples/presentation/get_all_the_text_all_slides/vb/Program.vb#snippet1)]
***


Expand All @@ -65,55 +55,29 @@ document.

## Sample Code
The following code gets all the text in all the slides in a specific
presentation file. For example, you can enter the name of the
presentation file from the keyboard, and then use a **foreach** loop in your program to get the array of
strings returned by the method **GetSlideIdAndText** as shown in the following
presentation file. For example, you can pass the name of the file as an argument,
and then use a `foreach` loop in your program to get the array of
strings returned by the method `GetSlideIdAndText` as shown in the following
example.

### [C#](#tab/cs-1)
```csharp
Console.Write("Please enter a presentation file name without extension: ");
string fileName = Console.ReadLine();
string file = @"C:\Users\Public\Documents\" + fileName + ".pptx";
int numberOfSlides = CountSlides(file);
System.Console.WriteLine("Number of slides = {0}", numberOfSlides);
string slideText;
for (int i = 0; i < numberOfSlides; i++)
{
GetSlideIdAndText(out slideText, file, i);
System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText);
}
System.Console.ReadKey();
```
### [C#](#tab/cs-2)
[!code-csharp[](../../samples/presentation/get_all_the_text_all_slides/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-1)
```vb
Console.Write("Please enter a presentation file name without extension: ")
Dim fileName As String = System.Console.ReadLine()
Dim file As String = "C:\Users\Public\Documents\" + fileName + ".pptx"
Dim numberOfSlides As Integer = CountSlides(file)
System.Console.WriteLine("Number of slides = {0}", numberOfSlides)
Dim slideText As String = Nothing
For i As Integer = 0 To numberOfSlides - 1
GetSlideIdAndText(slideText, file, i)
System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText)
Next
System.Console.ReadKey()
```
### [Visual Basic](#tab/vb-2)
[!code-vb[](../../samples/presentation/get_all_the_text_all_slides/vb/Program.vb#snippet2)]
***


The following is the complete sample code in both C\# and Visual Basic.

### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/get_all_the_text_all_slides/cs/Program.cs)]
[!code-csharp[](../../samples/presentation/get_all_the_text_all_slides/cs/Program.cs#snippet)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/get_all_the_text_all_slides/vb/Program.vb)]
[!code-vb[](../../samples/presentation/get_all_the_text_all_slides/vb/Program.vb#snippet)]
***

--------------------------------------------------------------------------------
## See also


[Open XML SDK class library
reference](/office/open-xml/open-xml-sdk)
[Open XML SDK class library reference](/office/open-xml/open-xml-sdk)
2 changes: 1 addition & 1 deletion samples/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.1.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.2.0" />
</ItemGroup>
</Project>
22 changes: 18 additions & 4 deletions samples/presentation/get_all_the_text_all_slides/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@
using System.Text;
using A = DocumentFormat.OpenXml.Drawing;

if (args is [{ } sldText, { } slideIndex])
if (args is [{ } filePath, { } slideIndex])
{
GetSlideIdAndText(out string text, sldText, int.Parse(slideIndex));
GetSlideIdAndText(out string text, filePath, int.Parse(slideIndex));
Console.WriteLine($"Side #{slideIndex + 1} contains: {text}");
}

if (args is [{ } presentationFile])
// <Snippet2>
if (args is [{ } path])
{
CountSlides(presentationFile);
int numberOfSlides = CountSlides(path);
Console.WriteLine($"Number of slides = {numberOfSlides}");

for (int i = 0; i < numberOfSlides; i++)
{
GetSlideIdAndText(out string text, path, i);
Console.WriteLine($"Side #{i + 1} contains: {text}");
}
}
// </Snippet2>

// <Snippet>
static int CountSlides(string presentationFile)
{
// <Snippet1>
// Open the presentation as read-only.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
// </Snippet1>
{
// Pass the presentation to the next CountSlides method
// and return the slide count.
Expand Down Expand Up @@ -89,3 +102,4 @@ static void GetSlideIdAndText(out string sldText, string docName, int index)
sldText = paragraphText.ToString();
}
}
// </Snippet>
65 changes: 52 additions & 13 deletions samples/presentation/get_all_the_text_all_slides/vb/Program.vb
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
Imports System.Text
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Presentation
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports A = DocumentFormat.OpenXml.Drawing

Module Program
Sub Main(args As String())
If args.Length = 2 Then
Dim filePath As String = args(0)
Dim slideIndex As Integer = Integer.Parse(args(1))
Dim text As String
GetSlideIdAndText(text, filePath, slideIndex)
Console.WriteLine($"Slide #{slideIndex + 1} contains: {text}")
End If

Module MyModule
' <Snippet2>
If args.Length = 1 Then
Dim path As String = args(0)
Dim numberOfSlides As Integer = CountSlides(path)
Console.WriteLine($"Number of slides = {numberOfSlides}")

Sub Main(args As String())
For i As Integer = 0 To numberOfSlides - 1
Dim text As String
GetSlideIdAndText(text, path, i)
Console.WriteLine($"Slide #{i + 1} contains: {text}")
Next
End If
' </Snippet2>
End Sub

Public Function CountSlides(ByVal presentationFile As String) As Integer
' <Snippet>
Function CountSlides(presentationFile As String) As Integer
' <Snippet1>
' Open the presentation as read-only.
Using presentationDocument__1 As PresentationDocument = PresentationDocument.Open(presentationFile, False)
Using presentationDocument As PresentationDocument = PresentationDocument.Open(presentationFile, False)
' </Snippet1>
' Pass the presentation to the next CountSlides method
' and return the slide count.
Return CountSlides(presentationDocument__1)
Return CountSlidesFromPresentation(presentationDocument)
End Using
End Function

' Count the slides in the presentation.
Public Function CountSlides(ByVal presentationDocument As PresentationDocument) As Integer
Function CountSlidesFromPresentation(presentationDocument As PresentationDocument) As Integer
' Check for a null document object.
If presentationDocument Is Nothing Then
Throw New ArgumentNullException("presentationDocument")
Throw New ArgumentNullException(NameOf(presentationDocument))
End If

Dim slidesCount As Integer = 0
Expand All @@ -34,20 +59,32 @@ Module MyModule
If presentationPart IsNot Nothing Then
slidesCount = presentationPart.SlideParts.Count()
End If

' Return the slide count to the previous method.
Return slidesCount
End Function

Public Sub GetSlideIdAndText(ByRef sldText As String, ByVal docName As String, ByVal index As Integer)
Sub GetSlideIdAndText(ByRef sldText As String, docName As String, index As Integer)
Using ppt As PresentationDocument = PresentationDocument.Open(docName, False)
' Get the relationship ID of the first slide.
Dim part As PresentationPart = ppt.PresentationPart
Dim slideIds As OpenXmlElementList = part.Presentation.SlideIdList.ChildElements
Dim slideIds As OpenXmlElementList = If(part?.Presentation?.SlideIdList?.ChildElements, Nothing)

If part Is Nothing OrElse slideIds.Count = 0 Then
sldText = ""
Return
End If

Dim relId As String = TryCast(slideIds(index), SlideId).RelationshipId
Dim relId As String = CType(slideIds(index), SlideId).RelationshipId

If relId Is Nothing Then
sldText = ""
Return
End If

' Get the slide part from the relationship ID.
Dim slide As SlidePart = DirectCast(part.GetPartById(relId), SlidePart)
Dim slide As SlidePart = CType(part.GetPartById(relId), SlidePart)

' Build a StringBuilder object.
Dim paragraphText As New StringBuilder()

Expand All @@ -59,4 +96,6 @@ Module MyModule
sldText = paragraphText.ToString()
End Using
End Sub
End Module
' </Snippet>
End Module

0 comments on commit bd191aa

Please sign in to comment.