Skip to content

Commit

Permalink
closes OfficeDev#187
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeebowen committed Dec 6, 2024
1 parent bd191aa commit ea176fd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 358 deletions.
6 changes: 3 additions & 3 deletions docs/includes/presentation/using-statement.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The `using` statement provides a recommended
alternative to the typical .Open, .Save, .Close sequence. It ensures
that the <xref:System.IDisposable.Dispose> method (internal method
With v3.0.0+ the <xref:DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Close> method has been removed and
the `using` statement provides the recommended replacement for the deprecated `.Create`, `.Save`, `.Close` sequence.
It ensures that the <xref:System.IDisposable.Dispose> method (internal method
used by the Open XML SDK to clean up resources) is automatically called
when the closing brace is reached. The block that follows the `using` statement establishes a scope for the
object that is created or named in the `using` statement, in this case
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/06/2024
ms.localizationpriority: medium
---
# Get all the text in a slide in a presentation
Expand All @@ -26,33 +26,23 @@ programmatically.
## Getting a PresentationDocument object
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 **file** parameter is a string that
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 `file` 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(file, False)
' Insert other code here.
End Using
```
### [C#](#tab/cs-1)
[!code-csharp[](../../samples/presentation/get_all_the_text_a_slide/cs/Program.cs#snippet1)]

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


Expand All @@ -64,42 +54,19 @@ document.
[!include[Structure](../includes/presentation/structure.md)]

## How the Sample Code Works
The sample code consists of three overloads of the **GetAllTextInSlide** method. In the following

The sample code consists of three overloads of the `GetAllTextInSlide` method. In the following
segment, the first overloaded method opens the source presentation that
contains the slide with text to get, and passes the presentation to the
second overloaded method, which gets the slide part. This method returns
the array of strings that the second method returns to it, each of which
represents a paragraph of text in the specified slide.

### [C#](#tab/cs-1)
```csharp
// Get all the text in a slide.
public static string[] GetAllTextInSlide(string presentationFile, int slideIndex)
{
// Open the presentation as read-only.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
{
// Pass the presentation and the slide index
// to the next GetAllTextInSlide method, and
// then return the array of strings it returns.
return GetAllTextInSlide(presentationDocument, slideIndex);
}
}
```
### [C#](#tab/cs-2)
[!code-csharp[](../../samples/presentation/get_all_the_text_a_slide/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-1)
```vb
' Get all the text in a slide.
Public Shared Function GetAllTextInSlide(ByVal presentationFile As String, ByVal slideIndex As Integer) As String()
' Open the presentation as read-only.
Using presentationDocument As PresentationDocument = PresentationDocument.Open(presentationFile, False)
' Pass the presentation and the slide index
' to the next GetAllTextInSlide method, and
' then return the array of strings it returns.
Return GetAllTextInSlide(presentationDocument, slideIndex)
End Using
End Function
```
### [Visual Basic](#tab/vb-2)
[!code-vb[](../../samples/presentation/get_all_the_text_a_slide/vb/Program.vb#snippet2)]
***


Expand All @@ -109,239 +76,57 @@ to the first overloaded method the array of strings that the third
overloaded method returns to it, each of which represents a paragraph of
text in the specified slide.

### [C#](#tab/cs-2)
```csharp
public static string[] GetAllTextInSlide(PresentationDocument presentationDocument, int slideIndex)
{
// Verify that the presentation document exists.
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument");
}

// Verify that the slide index is not out of range.
if (slideIndex < 0)
{
throw new ArgumentOutOfRangeException("slideIndex");
}

// Get the presentation part of the presentation document.
PresentationPart presentationPart = presentationDocument.PresentationPart;

// Verify that the presentation part and presentation exist.
if (presentationPart != null && presentationPart.Presentation != null)
{
// Get the Presentation object from the presentation part.
Presentation presentation = presentationPart.Presentation;

// Verify that the slide ID list exists.
if (presentation.SlideIdList != null)
{
// Get the collection of slide IDs from the slide ID list.
var slideIds = presentation.SlideIdList.ChildElements;

// If the slide ID is in range...
if (slideIndex < slideIds.Count)
{
// Get the relationship ID of the slide.
string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;

// Get the specified slide part from the relationship ID.
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);

// Pass the slide part to the next method, and
// then return the array of strings that method
// returns to the previous method.
return GetAllTextInSlide(slidePart);
}
}
}
// Else, return null.
return null;
}
```
### [C#](#tab/cs-3)
[!code-csharp[](../../samples/presentation/get_all_the_text_a_slide/cs/Program.cs#snippet3)]

### [Visual Basic](#tab/vb-2)
```vb
Public Shared Function GetAllTextInSlide(ByVal presentationDocument As PresentationDocument, ByVal slideIndex As Integer) As String()
' Verify that the presentation document exists.
If presentationDocument Is Nothing Then
Throw New ArgumentNullException("presentationDocument")
End If

' Verify that the slide index is not out of range.
If slideIndex < 0 Then
Throw New ArgumentOutOfRangeException("slideIndex")
End If

' Get the presentation part of the presentation document.
Dim presentationPart As PresentationPart = presentationDocument.PresentationPart

' Verify that the presentation part and presentation exist.
If presentationPart IsNot Nothing AndAlso presentationPart.Presentation IsNot Nothing Then
' Get the Presentation object from the presentation part.
Dim presentation As Presentation = presentationPart.Presentation

' Verify that the slide ID list exists.
If presentation.SlideIdList IsNot Nothing Then
' Get the collection of slide IDs from the slide ID list.
Dim slideIds = presentation.SlideIdList.ChildElements

' If the slide ID is in range...
If slideIndex < slideIds.Count Then
' Get the relationship ID of the slide.
Dim slidePartRelationshipId As String = (TryCast(slideIds(slideIndex), SlideId)).RelationshipId

' Get the specified slide part from the relationship ID.
Dim slidePart As SlidePart = CType(presentationPart.GetPartById(slidePartRelationshipId), SlidePart)

' Pass the slide part to the next method, and
' then return the array of strings that method
' returns to the previous method.
Return GetAllTextInSlide(slidePart)
End If
End If
End If

' Else, return null.
Return Nothing
End Function
```
### [Visual Basic](#tab/vb-3)
[!code-vb[](../../samples/presentation/get_all_the_text_a_slide/vb/Program.vb#snippet3)]
***


The following code segment shows the third overloaded method, which
takes takes the slide part passed in, and returns to the second
overloaded method a string array of text paragraphs. It starts by
verifying that the slide part passed in exists, and then it creates a
linked list of strings. It iterates through the paragraphs in the slide
passed in, and using a **StringBuilder** object
passed in, and using a `StringBuilder` object
to concatenate all the lines of text in a paragraph, it assigns each
paragraph to a string in the linked list. It then returns to the second
overloaded method an array of strings that represents all the text in
the specified slide in the presentation.

### [C#](#tab/cs-3)
```csharp
public static string[] GetAllTextInSlide(SlidePart slidePart)
{
// Verify that the slide part exists.
if (slidePart == null)
{
throw new ArgumentNullException("slidePart");
}

// Create a new linked list of strings.
LinkedList<string> texts = new LinkedList<string>();

// If the slide exists...
if (slidePart.Slide != null)
{
// Iterate through all the paragraphs in the slide.
foreach (var paragraph in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
{
// Create a new string builder.
StringBuilder paragraphText = new StringBuilder();

// Iterate through the lines of the paragraph.
foreach (var text in paragraph.Descendants<DocumentFormat.OpenXml.Drawing.Text>())
{
// Append each line to the previous lines.
paragraphText.Append(text.Text);
}

if (paragraphText.Length > 0)
{
// Add each paragraph to the linked list.
texts.AddLast(paragraphText.ToString());
}
}
}

if (texts.Count > 0)
{
// Return an array of strings.
return texts.ToArray();
}
else
{
return null;
}
}
```
### [C#](#tab/cs-4)
[!code-csharp[](../../samples/presentation/get_all_the_text_a_slide/cs/Program.cs#snippet4)]

### [Visual Basic](#tab/vb-3)
```vb
Public Shared Function GetAllTextInSlide(ByVal slidePart As SlidePart) As String()
' Verify that the slide part exists.
If slidePart Is Nothing Then
Throw New ArgumentNullException("slidePart")
End If

' Create a new linked list of strings.
Dim texts As New LinkedList(Of String)()

' If the slide exists...
If slidePart.Slide IsNot Nothing Then
' Iterate through all the paragraphs in the slide.
For Each paragraph In slidePart.Slide.Descendants(Of DocumentFormat.OpenXml.Drawing.Paragraph)()
' Create a new string builder.
Dim paragraphText As New StringBuilder()

' Iterate through the lines of the paragraph.
For Each text In paragraph.Descendants(Of DocumentFormat.OpenXml.Drawing.Text)()
' Append each line to the previous lines.
paragraphText.Append(text.Text)
Next text

If paragraphText.Length > 0 Then
' Add each paragraph to the linked list.
texts.AddLast(paragraphText.ToString())
End If
Next paragraph
End If

If texts.Count > 0 Then
' Return an array of strings.
Return texts.ToArray()
Else
Return Nothing
End If
End Function
```
### [Visual Basic](#tab/vb-4)
[!code-vb[](../../samples/presentation/get_all_the_text_a_slide/vb/Program.vb#snippet4)]
***



--------------------------------------------------------------------------------
## Sample Code

Following is the complete sample code that you can use to get all the
text in a specific slide in a presentation file. For example, you can
use the following **foreach** loop in your
program to get the array of strings returned by the method **GetAllTextInSlide**, which represents the text in
the second slide of the presentation file "Myppt8.pptx."
use the following `foreach` loop in your
program to get the array of strings returned by the method `GetAllTextInSlide`, which represents the text in
the slide at the index of `slideIndex` of the presentation file found at the `filePath`.

### [C#](#tab/cs-4)
```csharp
foreach (string s in GetAllTextInSlide(@"C:\Users\Public\Documents\Myppt8.pptx", 1))
Console.WriteLine(s);
```
### [C#](#tab/cs-5)
[!code-csharp[](../../samples/presentation/get_all_the_text_a_slide/cs/Program.cs#snippet5)]

### [Visual Basic](#tab/vb-4)
```vb
For Each s As String In GetAllTextInSlide("C:\Users\Public\Documents\Myppt8.pptx", 1)
Console.WriteLine(s)
Next
```
### [Visual Basic](#tab/vb-5)
[!code-vb[](../../samples/presentation/get_all_the_text_a_slide/vb/Program.vb#snippet5)]
***


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

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

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

--------------------------------------------------------------------------------
## See also
Expand Down
Loading

0 comments on commit ea176fd

Please sign in to comment.