DevExpress Presentation API: Search, Replace, Remove, and Highlight Text in a Slide or Presentation
- 4 minutes to read
The Presentation API library supports the following text-related operations on the presentation or slide level:
- Search for text in shapes, notes, and tables
- Replace text
- Remove specified text
- Change character formatting
The same operations are available on the shape level. For more information, refer to the following help topic: Work with Shape Text.
Replace Text
Call the Presentation.ReplaceText or Slide.ReplaceText method to replace text in a presentation or slide:
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
//...
presentation.ReplaceText("com", "info", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
Slide slide = presentation.Slides[0];
slide.ReplaceText("com", "info", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
}
}
Find Text
Call the Presentation.FindText or Slide.FindText method to obtain text that matches the search query. The method returns a list of TextSearchInfo objects. Each object contain the found text ranges and the parent text area.
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
//...
IList<TextSearchInfo> textRangeInfos = presentation.FindText("com", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
Slide slide = presentation.Slides[0];
IList<TextSearchInfo> slideTextRangeInfos = slide.FindText("com", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
}
}
Apply Custom Formatting to Specified Text Ranges
Call the Presentation.ModifyTextProperties or Slide.ModifyTextProperties method to apply custom formatting to specified text ranges. For example, you can highlight text (change text or background color).
The following code sample searches for occurrences of the “com” string and changes the font color to red. Method calls for both presentation and slides are included.
using DevExpress.Docs.Presentation;
using System.Drawing;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
// ...
IList<TextSearchInfo> textRangeInfos = presentation.FindText("com", new
TextSearchOptions { MatchCase = true, WholeWordOnly = true });
presentation.ModifyTextProperties(textRangeInfos, new TextProperties { Fill = new SolidFill(Color.Red) });
Slide slide = presentation.Slides[0];
IList<TextSearchInfo> slideTextRangeInfos = slide.FindText("com", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
slide.ModifyTextProperties(slideTextRangeInfos, new TextProperties { Fill = new SolidFill(Color.Red) });
}
}
Remove Text
Call the Presentation.RemoveText or Slide.RemoveText method to remove specified text ranges from a presentation or slide:
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
//...
IList<TextSearchInfo> textRangeInfos = presentation.FindText("com", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
presentation.RemoveText(textRangeInfos);
Slide slide = presentation.Slides[0];
IList<TextSearchInfo> slideTextRangeInfos = slide.FindText("com", new TextSearchOptions { MatchCase = true, WholeWordOnly = true });
slide.RemoveText(slideTextRangeInfos);
}
}
Extract Text
For more information on how to extract text from presentations, refer to the following help topic: Extract Presentation Content.