How to highlight words

I am iterating though all TextFragments then applying some rules such as capitalization of specific words and or phrases and the rules change based on location within the document. I would also like to highlight the words I modify. How can I highlight a word within a TextFragment?

@mjanulaitis

Thanks for contacting support.

Please use following code snippet to search and highlight found TextFragments in a PDF:

Document document = new Document(dataDir + "SampleForHighlighting.pdf"); 
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"(?i)Garofalo(?i)");
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
document.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber.TextFragments;
if (textFragmentCollection1.Count > 0)
{
  foreach (TextFragment textFragment in textFragmentCollection1)
  {
   Aspose.Pdf.Annotations.HighlightAnnotation highlightText = new Aspose.Pdf.Annotations.HighlightAnnotation(textFragment.Page, new Aspose.Pdf.Rectangle(textFragment.Position.XIndent, textFragment.Position.YIndent, textFragment.Position.XIndent + textFragment.Rectangle.Width, textFragment.Position.YIndent + textFragment.Rectangle.Height));
   highlightText.Opacity = 0.5;
   highlightText.Color = Aspose.Pdf.Color.Yellow;
   textFragment.Page.Annotations.Add(highlightText);
 }
}
document.Save(dataDir + "SampleForHighlighting_out.pdf");

In case you need further assistance, please feel free to let us know.

Helpful, but the question was about specific words. I solved this on my own using the following code. This highlights a single character:

        string letter = before.Substring(startIndex, 1);

        for (int segNum = 1; segNum <= textFragment.Segments.Count; segNum++)
        {
            TextSegment segment = textFragment.Segments[segNum];

            if (segment.EndCharIndex > startIndex)
            {
                var leftChar = segment.Characters[startIndex + 1];

                Aspose.Pdf.Rectangle rect = leftChar.Rectangle;
                Aspose.Pdf.Annotations.HighlightAnnotation highlightText = new Aspose.Pdf.Annotations.HighlightAnnotation(textFragment.Page, rect);
                highlightText.Opacity = 0.5;
                highlightText.Color = Aspose.Pdf.Color.Red;
                textFragment.Page.Annotations.Add(highlightText);

            }
        }

@mjanulaitis

Thanks for the acknowledgement.

It is good to know that you managed to fulfill your requirements. Please keep using our API and in case you need any further assistance, please feel free to let us know.