Search Text and highlight it

764 Views Asked by At

My code is in C# I am using Aspose to search text and highlight it in pdf. It is working but the time taken is very huge. Example : My document has 25 pages and it has 25 instance of search text , 1 search text in each page. It take 2 minutes which is unacceptable.

I have 3 questions:

  1. Is it a way to reduce this time taken ?
  2. Currently this approach is for pdf, in my case i have all types of doc (xls, pdf, ppt, doc)? Is there any way where this search and highlighting can be performed in all docs ?
  3. Is there some better way of doing it other than aspose ?

// open document
Document document = new Document(@"C:\TestArea\Destination\SUP000011\ATM-1B4L2KQ0ZE0-0001\OpenAML.pdf");

//create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Martin");

//accept the absorber for all the pages
for (int i = 1; i <= document.Pages.Count; i++)
{
    document.Pages[i].Accept(textFragmentAbsorber);

    //get the extracted text fragments
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

    //loop through the fragments
    foreach (TextFragment textFragment in textFragmentCollection)
    {
        //update text and other properties
       // textFragment.TextState.Invisible = false;

        //textFragment.Text = "TEXT";
        textFragment.TextState.Font = FontRepository.FindFont("Verdana");
        textFragment.TextState.FontSize = 9;
        textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
        textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Yellow);
        //textFragment.TextState.Underline = true;
    }
}

// Save resulting PDF document.
document.Save(@"C:\TestArea\Destination\SUP000011\ATM-1B4L2KQ0ZE0-0001\Highlightdoc.pdf");
0

There are 0 best solutions below