←Select platform

Find Method (DocumentViewerText)

Summary

Performs a "find", "find next" or "find previous" search operation on the text of the document.

Syntax

C#
VB
C++
public System.Collections.Generic.IList<Leadtools.Documents.UI.DocumentViewerTextItem> Find( 
   DocumentViewerFindText findText, 
   bool isFirst, 
   bool findNext 
) 
Public Function Find( 
   ByVal findText As DocumentViewerFindText, 
   ByVal isFirst As Boolean, 
   ByVal findNext As Boolean 
) As System.Collections.Generic.IList(Of Leadtools.Documents.UI.DocumentViewerTextItem) 
public:  
   System::Collections::Generic::IList<Leadtools::Documents::UI::DocumentViewerTextItem^>^ Find( 
      DocumentViewerFindText^ findText, 
      bool isFirst, 
      bool findNext 
   ) 

Parameters

findText

Data that contains the text to find.

isFirst

true to perform a "find" operation, otherwise, continues searching for either next or previous text.

findNext

true to perform a "find next" operation; otherwise, false to perform a

"find previous" operation.

Return Value

A list of the text items found, or null if no next was found with this operation.

Remarks

Find and LastFindText are used together to perform "find", "find next" and "find previous" text search operations.

Typical applications that perform text search contain a menu item for "Find", "Find Next" and "Find Previous". The following explains how to use Find and LastFindText to perform these operations.

  • "Find Next" and "Find Previous" are used to continue searching from the last location. Therefore, at the beginning of the program, these are not called and the corresponding UI elements should be disabled. Note that some applications select to check this state and call "Find" when "Find Next" is called without previous data. In either case, the data for the last find operation is an instance of DocumentViewerFindText object that is set to null, DocumentViewerText saves a value that can be used for this purpose in the LastFindText property which will have a value of null when a new document is set in the viewer.

  • When the user select "Find" from the application menu, the options for the search must be collected. This is performed by creating a new instance of DocumentViewerFindText and setting the options. Typical "Find Text" dialogs contain options that matches these properties as follows:

  • The text to find: Set this in DocumentViewerFindText.Text

  • Whether to perform case sensitive search and if whole words must be matched: Set these in DocumentViewerFindText.MatchCase and DocumentViewerFindText.WholeWordsOnly

  • If search is restricted to the current page or includes all in the document: Set these in DocumentViewerFindText.GotoNextPage and DocumentViewerFindText.PageNumber

  • Once the options are collected, call the Find method using the following parameters:

    Find(options, true, false)
    The first parameter are the options we just collected. The second is isFirst which is true to indicate that this is the first find operation and to start search from the beginning of the document. The last is

findNext

which is false to indicate that the search must progress forward from current location (start at the document). The method will run and return either the text items found or null otherwise. If text was found (the return value is not null), then the return value can be used to parse the text characters to show to the user or if DocumentViewerFindText.AutoSelect and DocumentViewerFindText.AutoEnsureVisible were set to true (default value), then document viewer will automatically select this text and highlight in the viewer and optionally, pan the image viewer to ensure that the value is visible in the current view. At this point, typical applications leave the find dialog open with the same option and prompt the user to either stop the search (close the dialog) or perform a "find next" or "find previous" operation from the search dialog itself. In either case, "find next" and "find previous" can be performed without the need to the DocumentViewerFindText object created in this step, as explained later. If null is returned, then the search could not find any matching text in the page or document. The application should show a warning message or indicator to the user to signal that the operation is complete. In this case, either exit the dialog or prompt the user to enter a new value.

  • If text items were found using the first Find, then a copy of the DocumentViewerFindText object will be stored in the LastFindText property. This object contains the same text and options passed by the user as well as internal data used by the viewer to track the location in the page and document of the search. Typical applications will check the value of LastFindText against null and enable the "Find Next" and "Find Previous" UI elements accordingly. To perform a "find next" operation, call Find with the following parameters:

    Find(documentViewerText.LastFindText, false, true)
    The first parameter contains the same options used (including the internal data used to track the current location). isFirst is false to indicate that search must use this internal data to continue to next match found, findNext is true to indicate that the search must continue forward. Similarly, "find previous" operations can be performed using:
    Find(documentViewerText.LastFindText, false, false)
    The only difference is the value of findNext indicating the direction of the search (forward or backward). As in the first step, the result from Find can be checked against null to inform the user when no more matches are found. Find can be called many times until the search is exhausted. This includes reaching the end of the page or document when performing "find next", or when the original location from the first is reached if DocumentViewerFindText.Recursive is true. The viewer uses the internal data stored to ensure that recursive search will stop at the original location and not continue past it.

  • At any time, call Find with a brand new DocumentViewerFindText object instead of the value of LastFindText to reset the search and start from the beginning of the document.

The LEADTOOLS Document Viewer demo uses the above to perform text search operations. Refer to the demo source code for a full example.

Calling Find required obtaining the text of the page(s) using DocumentPage.GetText, the viewer can automatically perform this when the value of AutoGetText is true.

Find performs the action of selecting text in the page or document by calling SetSelectedTextItems.

Example

Start with the example created in DocumentViewer, remove all the code in the Example function and add the code below.

When the user clicks the Example button, we will try to find the word "LEAD" in the current page, and continue with finding next occurance until we reach the end of the page.

C#
VB
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Documents; 
using Leadtools.Documents.UI; 
using Leadtools.Codecs; 
using Leadtools.Caching; 
using Leadtools.Annotations.Core; 
using Leadtools.Forms.Ocr; 
 
var text = _documentViewer.Text; 
// First check if we have text for this page 
var pageNumber = _documentViewer.CurrentPageNumber; 
if (!text.HasDocumentPageText(pageNumber)) 
{ 
   // Get the text 
   text.GetDocumentPageText(pageNumber); 
} 
 
// Clear the last find data (if any) 
text.ClearLastFindText(); 
 
// Find the first occurance of the word "LEAD" ignoring the case 
var findText = new DocumentViewerFindText(); 
// The text 
findText.Text = "LEAD"; 
// Ignore case 
findText.MatchCase = false; 
// Any word that contains the phrase 
findText.WholeWordsOnly = false; 
// In this page only 
findText.PageNumber = _documentViewer.CurrentPageNumber; 
findText.GotoNextPage = false; 
// Do not go back to the beginning of the page 
findText.Recursive = false; 
// Select the text if found 
findText.AutoSelect = true; 
// Make sure we pan to it if the location is not in the current view 
findText.AutoEnsureVisible = true; 
 
// Find it 
if (text.Find(findText, true, true) != null) 
{ 
   do 
   { 
      MessageBox.Show("Found"); 
 
      // Find next, using the same info so it continues from 
      // last location 
   } 
   while (text.Find(text.LastFindText, false, true) != null); 
} 
 
MessageBox.Show("Reached the end of the page"); 
Imports Leadtools 
Imports Leadtools.Controls 
Imports Leadtools.Documents 
Imports Leadtools.Documents.UI 
Imports Leadtools.Codecs 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Core 
Imports Leadtools.Forms.Ocr 
 
Dim text As DocumentViewerText = _documentViewer.Text 
' First check if we have text for this page 
Dim pageNumber As Integer = _documentViewer.CurrentPageNumber 
If Not text.HasDocumentPageText(pageNumber) Then 
   ' Get the text 
   text.GetDocumentPageText(pageNumber) 
End If 
 
' Clear the last find data (if any) 
text.ClearLastFindText() 
 
' Find the first occurance of the word "LEAD" ignoring the case 
Dim findText As New DocumentViewerFindText() 
' The text 
findText.Text = "LEAD" 
' Ignore case 
findText.MatchCase = False 
' Any word that contains the phrase 
findText.WholeWordsOnly = False 
' In this page only 
findText.PageNumber = _documentViewer.CurrentPageNumber 
findText.GotoNextPage = False 
' Do not go back to the beginning of the page 
findText.Recursive = False 
' Select the text if found 
findText.AutoSelect = True 
' Make sure we pan to it if the location is not in the current view 
findText.AutoEnsureVisible = True 
 
' Find it 
If Not IsNothing(text.Find(findText, True, True)) Then 
   Do 
      MessageBox.Show("Found") 
 
      ' Find next, using the same info so it continues from 
      ' last location 
   Loop While Not IsNothing(text.Find(text.LastFindText, False, True)) 
End If 
 
MessageBox.Show("Reached the end of the page") 

Requirements

Target Platforms

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
Leadtools.Documents.UI Assembly
Click or drag to resize