How to Remove Highlighting from previous searched element?


I am creating a search text application which highlights the searched
text from all over the pdf file with green color. Now problem is that
when i search next word then it highlight the next word but previous
text also remains highlighted. Can you please tell me how can i remove
highlighting from previous text at the time of searching a new text in
pdf through pdftron?

Currently i am using the following code snippet for doing this but it
is not removing the highlighting of previous text. Hoping for right
solution. Thanks in advance.

private void RemoveSearch()

            ColorPt bgColor = new ColorPt();
            foreach (SearchedItem searchedItem in

                ElementWriter writer = new ElementWriter();
                ElementBuilder builder = new ElementBuilder();

                foreach (pdftron.PDF.Rect word_bbox in

                    Element rect = builder.CreateRect(word_bbox.x1,
word_bbox.y1, word_bbox.Width(), word_bbox.Height());


rect.GetGState().SetBlendMode(GState.BlendMode.e_bl_color);//Set blend
mode to show the highlighted text





  private void HighlightSearch(pdftron.PDF.Rect word_bbox,
ElementWriter writer, ColorPt bgColor, SearchedItem searchedItem)

            ElementBuilder builder = new ElementBuilder();
            Element rect = builder.CreateRect(word_bbox.x1,
word_bbox.y1, word_bbox.Width(), word_bbox.Height());
            searchedItem.SearchWordColor =


blend mode to show the highlighted text


        public void FindAll(string textToFind)
            if (_pdfdoc != null && documentInfo != null)
                TextExtractor txt = new TextExtractor(); // Used to
extract words

                if (documentInfo.SearchedItemList.Count > 0)

                // The color of modified text - green.
                ColorPt bgColor = new ColorPt(0, 1, 0);

                PageIterator itr = _pdfdoc.GetPageIterator();

                for (; itr.HasNext(); itr.Next())
                    pdftron.PDF.Page page = itr.Current();

                    SearchedItem searchedItem = new SearchedItem();
                    searchedItem.PageNo = page.GetIndex();

                    txt.Begin(page); // Read the page.

                    // Example 2. Extract words one by one.
                    TextExtractor.Word word;
                    ElementWriter writer = new ElementWriter();
                    for (TextExtractor.Line line = txt.GetFirstLine();
line.IsValid(); line = line.GetNextLine())
                        for (word = line.GetFirstWord();
word.IsValid(); word = word.GetNextWord())
                            string wordPattern =
word.GetString().Replace(":", "").Replace(";", "").Replace(")",
"").Replace("(", "");//.Replace("[", "").Replace("]", "").Replace(",",

                                SearchedItem existingSearch =
documentInfo.SearchedItemList.Find(delegate(SearchedItem refr)
{ return refr.PageNo == page.GetIndex(); });

writer, bgColor, searchedItem);


                                if (existingSearch == null)


Is there a reason why you need implement your own text highlighting in
PDFViewCtrl? PDFViewCtrl already comes with built-in text selection
and highlighting (e.g. pdfview.Find()/SelectByRect()/SelectbyStruct()
and SelectByHighlights(). The last method takes Highlights object
which can be created using TextSearch).

In case you need to implement your own highlighting it may be better
to create text highlight annotation instead of directly modifying page
content (the latter is possible but is much more work). The idea is
that you can create 'pdftron.PDF.Annots.Highlight' annotation on a
given location, call RefreshAppearance() and add it to the page
(similar to Annotation sample project). Latter on you can easily
delete or modify these annotations


Thanks for your suggestions.

We are using pdfnet dll version which does not contain the
selectbyhighlight() method. I have tried to use selectbyRect() and
selectbystruct() but it is highlighting just first occurence of
searched text instead of highlighting all the occurences all over the
pdf.Can you please let me know how can I highlight all the occurences
of searched text ??

Your help or any suggestion will be really helpful to us.

Thanks in Advance.

SelectByHighlights() is available in the current version of PDFNet
To a highlight for multiple occurences of the same of different
keyword you can use use pdftron.PDF.TextSearch and obtain Highlights
objects. You can then find addtional Highlights and merge them with
previous ones using Highlights.Add().

In case this does not work for you, it is probably better/simpler to
create highlight annotations instead of directly modifying the page
content stream.