using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Text;

namespace SearchTextAndHighlightInPdfUsingCSharp
{
    class Program
    {
        static void Main(string[] args) // Main function to search text in PDF and highlight
        {
            // Instantiate the license to avoid any trial version limitations
            // and watermark in the output ODF file
            Aspose.Pdf.License licHighlightText= new Aspose.Pdf.License();
            licHighlightText.SetLicense("Aspose.Pdf.lic");

            // Load an existing PDF file in which you want to highlight text
            Document doc = new Document("sample_input.pdf");

            // Search target text to highlight
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("The text to be searched"); 
            doc.Pages[1].Accept(textFragmentAbsorber);

            // Create a highlight annotation
            HighlightAnnotation ha = new HighlightAnnotation(doc.Pages[1], textFragmentAbsorber.TextFragments[1].Rectangle);

            // Specify highlight color 
            ha.Color = Color.Yellow;

            // Add annotation to highlight text in PDF 
            doc.Pages[1].Annotations.Add(ha);

            // Save the document 
            doc.Save("PDF_with_Highlighted_Text.pdf");

            System.Console.WriteLine("Done");
        }    
    }
}