@Jaibir,
New interfaces and methods will be added to Aspose.Slides for .NET 24.6:
/// <summary>
/// Represents options which can be used to find text in Presentation, Slide or TextFrame.
/// </summary>
public interface ITextSearchOptions
{
/// <summary>
/// Set true to use case-sensitive search, false - otherwise.
/// </summary>
bool CaseSensitive { get; set; }
/// <summary>
/// Set true to match only whole words, false - otherwise.
/// </summary>
bool WholeWordsOnly { get; set; }
}
and
/// <summary>
/// Callback interface used to getting search text result.
/// </summary>
public interface IFindResultCallback
{
/// <summary>
/// Callback method which receives data about found text.
/// </summary>
void FoundResult(ITextFrame textFrame, string sourceText, string foundText, int textPosition);
}
Below is an example code that solves the problem of getting information about all cases of text highlighting:
using (Presentation pres = new Presentation(pptxFileName))
{
// Create callback.
FindResultCallback callback = new FindResultCallback();
// Create search options.
TextSearchOptions options = new TextSearchOptions() { ResultCallback = callback };
// Highlight all words "secteftuer".
pres.HighlightText("secteftuer", Color.Yellow, options);
// Output the number of found fragments of the given text.
Console.WriteLine(callback.Count);
// Output data for each word "secteftuer" found.
foreach (WordInfo info in callback.Words)
{
Console.WriteLine(info.FoundText, info.TextPosition, info.Context);
}
// Get all the data about the found cases in the first slide.
WordInfo[] elements = callback.GetElemensForSlide(callback.SlideNumbers[1]);
// Output the number of found fragments of the given text in the first slide.
Console.WriteLine(elements.Length);
}
/// <summary>
/// Class that provides information about all found occurrences of a given text.
/// </summary>
private class FindResultCallback : IFindResultCallback
{
// Array of retrieved text information.
internal List<WordInfo> Words = new List<WordInfo>();
/// <summary>
/// The number of matches found to a given text.
/// </summary>
public int Count
{
get { return Words.Count; }
}
/// <summary>
/// Gets all slides in which the given text was found.
/// </summary>
public int[] SlideNumbers
{
get
{
List<int> slides = new List<int>();
foreach (var word in Words)
{
if (!slides.Contains(((Slide)word.TextFrame.Slide).SlideNumber))
slides.Add(((Slide)word.TextFrame.Slide).SlideNumber);
}
return slides.ToArray();
}
}
/// <summary>
/// Gets all occurrences of the found text on the slide.
/// </summary>
/// <param name="slideNumber">Slide number</param>
public WordInfo[] GetElemensForSlide(int slideNumber)
{
List<WordInfo> foundElements = new List<WordInfo>();
foreach (var element in Words)
if (((Slide)element.TextFrame.Slide).SlideNumber == slideNumber)
foundElements.Add(element);
return foundElements.ToArray();
}
/// <summary>
/// Callback method which receives data about found text.
/// </summary>
/// <param name="textFrame"><see cref="ITextFrame"/> where serching text was found.</param>
/// <param name="sourceText">Source text of TextFrame where text was found.</param>
/// <param name="foundText">Found text.</param>
/// <param name="textPosition">Position of found text in source text.</param>
public void FoundResult(ITextFrame textFrame, string oldText, string foundText, int textPosition)
{
Words.Add(new WordInfo(textFrame, oldText, foundText, textPosition));
}
}
/// <summary>
/// A class that provides information about each given text found in the presentation.
/// </summary>
private class WordInfo
{
/// <summary>
/// Constructor.
/// </summary>
internal WordInfo(ITextFrame textFrame, string sourctText, string foundText, int textPosition)
{
TextFrame = textFrame;
SourceText = sourctText;
FoundText = foundText;
TextPosition = textPosition;
}
/// <summary>
/// Gets found text.
/// </summary>
public string FoundText { get; set; }
/// <summary>
/// Gets the source text for the TextFrame in which the text was found.
/// </summary>
public string SourceText { get; set; }
/// <summary>
/// Position of the found text in the text frame.
/// </summary>
public int TextPosition { get; set; }
/// <summary>
/// The text frame in which the text was found.
/// </summary>
public ITextFrame TextFrame { get; set; }
/// <summary>
/// The context in which the text was found.
/// </summary>
public string Context
{
get
{
int startIndex = TextPosition - 3 < 0 ? 0 : TextPosition - 3;
int len = TextPosition + FoundText.Length + 20 > SourceText.Length ? SourceText.Length - TextPosition : FoundText.Length + 20;
return (startIndex == 0 ? "" : "...") + SourceText.Substring(startIndex, len) + (startIndex + len < SourceText.Length ? "..." : "");
}
}
}
Also, new methods HighlightText and HighlightRegex will be added to the IPresentation interface that allow you to perform operations for the whole presentation:
/// <summary>
/// Highlight all matches of sample in text frame text using specified color.
/// </summary>
void HighlightText(string text, Color highlightColor);
/// <summary>
/// Highlight all matches of sample text in presentation using specified color.
/// </summary>
void HighlightText(string text, Color highlightColor, ITextSearchOptions options, IFindResultCallback callback);
/// <summary>
/// Highlight all matches of regular expression in presentation using specified color.
/// </summary>
void HighlightRegex(Regex regex, Color highlightColor, IFindResultCallback callback);