api_name | api_type | ms.assetid | title | ms.suite | ms.author | author | ms.topic | ms.date | ms.localizationpriority | ||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
6de46612-f864-413f-a504-11ea85f1f88f |
How to: Get all the text in a slide in a presentation |
office |
o365devx |
o365devx |
conceptual |
12/06/2024 |
medium |
This topic shows how to use the classes in the Open XML SDK for Office to get all the text in a slide 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
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 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
represents the path for the file from which you want to open the
document.
[!includeUsing Statement] presentationDocument
.
[!includeStructure]
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.
The second overloaded method takes the presentation document passed in and gets a slide part to pass to the third overloaded method. It returns 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.
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
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.
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 slide at the index of slideIndex
of the presentation file found at the filePath
.
Following is the complete sample code in both C# and Visual Basic.