Hi Silvana,
Hi Silvana,
Thanks for your inquiry. Please check the following code snippet to search text, starting with “tr” and ending on non-whitespace characters, using a regular expression. It will help you to accomplish the task.
Document docPdf = new Document();
MemoryStream ms = new MemoryStream();
Page page = docPdf.Pages.Add();
TextFragment text = new TextFragment("I am trying to highlight the search text in a PDF file. I have not HAD trouble");
page.Paragraphs.Add(text);
docPdf.Save(ms);
docPdf = new Document(ms);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"(?i)tr\S*");
//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 = Aspose.Pdf.Color.Yellow;
}
docPdf.Save(myDir +"regularexpression.pdf");
Please feel free to contact us for any further assistance.
Best Regards,
Forgiveness does not mention that he wanted the pattern is the beginning of a word, I solved using:
Hi Silvana,