Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

problem with use regular expression for highlight text

$
0
0
I am trying to highlight the search text in a PDF file. I have not had trouble finding simple sentences, but when I use the regular expression * at the end of a word.

For example, if I search for "tr *" in:

   "I am trying to highlight the search text in a PDF file. I have not HAD trouble"

I hope as a result, "trying" and "trouble", and actually I also highlighting all the words with "t" as "highlight", "text", "not" .....

How the operator "*" is used in regular expressions using TextFragmentAbsorber?
How I can specify that recognizes the exact pattern besides the "*" as in my example "tr*"?

My code is:

 Document docPdf = new Document(new MemoryStream(binaryFile));
 TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(?i)tr*");
                           
  //set text search option to specify regular expression usage
  TextSearchOptions textSearchOptions = new TextSearchOptions(true);
  textFragmentAbsorber.TextSearchOptions = textSearchOptions;
  //accept the absorber for all the pages
  docPdf.Pages.Accept(textFragmentAbsorber);
  //get the extracted text fragments
  TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
   //loop through the fragments
   foreach (TextFragment textFragment in textFragmentCollection)
    {
            //highlight background text
              textFragment.TextState.BackgroundColor = System.Drawing.Color.Yellow;
     }
   
    docPdf.Save(stream);


I hope I was clear with my question ... I hope your answer!
Thanks

Viewing all articles
Browse latest Browse all 3131

Trending Articles