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();
}
}