How to Remove Highlighting from previous searched element?

Hi,

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
documentInfo.SearchedItemList)
            {

                ElementWriter writer = new ElementWriter();
                writer.Begin(_pdfdoc.GetPage(searchedItem.PageNo));
                ElementBuilder builder = new ElementBuilder();

                foreach (pdftron.PDF.Rect word_bbox in
searchedItem.BboxList)
                {

                    Element rect = builder.CreateRect(word_bbox.x1,
word_bbox.y1, word_bbox.Width(), word_bbox.Height());
                    rect.SetPathStroke(false);
                    rect.SetPathFill(true);

rect.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
                    //
rect.GetGState().SetFillColor(searchedItem.SearchWordColor);

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

                    rect.GetGState().SetFillOpacity(2);
                    rect.GetGState().SetFillColor(bgColor);
                    writer.WriteElement(rect);

                }
                //writer.Flush();
                writer.End();

            }
            documentInfo.SearchedItemList.Clear();

        }

  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 =
rect.GetGState().GetFillColor();
            rect.SetPathStroke(false);
            rect.SetPathFill(true);

rect.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
            rect.GetGState().SetFillColor(bgColor);

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

            writer.WriteElement(rect);
        }

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

                if (documentInfo.SearchedItemList.Count > 0)
                    RemoveSearch();

                // 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();
                    writer.Begin(page);
                    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(",",
"")

                            if
(wordPattern.ToLower().Contains(textToFind.ToLower()))
                            {
                                SearchedItem existingSearch =
documentInfo.SearchedItemList.Find(delegate(SearchedItem refr)
{ return refr.PageNo == page.GetIndex(); });

                                HighlightSearch(word.GetBBox(),
writer, bgColor, searchedItem);

searchedItem.BboxList.Add(word.GetBBox());

                                if (existingSearch == null)

documentInfo.SearchedItemList.Add(searchedItem);
                            }
                        }
                    }
                    writer.End();
                }
                _pdfview.Update();
                //_pdfview.Refresh();
            }
        }

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

Hi,

Thanks for your suggestions.

We are using pdfnet dll version 5.0.2.0 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
(v5.1).
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.