@interface PTTextSearch : NSObject
TextSearch searches through a PDF document for a user-given search pattern. The current implementation supports both verbatim search and the search using regular expressions, whose detailed syntax can be found at:
TextSearch also provides users with several useful search modes and extra information besides the found string that matches the pattern. TextSearch can either keep running until a matched string is found or be set to return periodically in order for the caller to perform any necessary updates (e.g., UI updates). It is also worth mentioning that the search modes can be changed on the fly while searching through a document.
Possible use case scenarios for TextSearch include:
- Guide users of a PDF viewer (e.g. implemented by PDFViewCtrl) to places where they are intersted in;
- Find interested PDF documents which contain certain patterns;
- Extract interested information (e.g., credit card numbers) from a set of files;
- Extract Highlight information (refer to the Highlights class for details) from files for external use.
Since hyphens (‘-’) are frequently used in PDF documents to concatenate the two broken pieces of a word at the end of a line, for example
TextSearch is powerful for finding patterns in PDF files; yes, it is really pow-
a search for
powerful should return both instances. However, not all end-of-line
hyphens are hyphens added to connect a broken word; some of them could be
hyphens. In addition, an input search pattern may also contain hyphens that complicate
the situation. To tackle this problem, the following conventions are adopted:
a)When in the verbatim search mode and the pattern contains no hyphen, a matching
string is returned if it is exactly the same or it contains end-of-line
or start-of-line hyphens. For example, as mentioned above, a search for
would return both instances.
b)When in verbatim search mode and the pattern contains one or multiple hyphens, a
matching string is returned only if the string matches the pattern exactly. For
example, a search for
pow-erful will only return the second instance, and a search
power-ful will return nothing.
c)When searching using regular expressions, hyphens are not taken care implicitly.
Users should take care of it themselves. For example, in order to find both the
powerful instances, the input pattern can be
A sample use case (in C++):
//... Initialize PDFNet ... PDFDoc doc(filein); doc.InitSecurityHandler(); int page_num; char buf; UString result_str, ambient_string; Highlights hlts; TextSearch txt_search; TextSearch::Mode mode = TextSearch::e_whole_word | TextSearch::e_page_stop; UString pattern( "joHn sMiTh" ); //PDFDoc doesn't allow simultaneous access from different threads. If this //document could be used from other threads (e.g., the rendering thread inside //PDFView/PDFViewCtrl, if used), it is good practice to lock it. //Notice: don't forget to call doc.Unlock() to avoid deadlock. doc.Lock(); txt_search.Begin( doc, pattern, mode ); while ( true ) ; //unlock the document to avoid deadlock. doc.UnLock();
For a full sample, please take a look at the TextSearch sample project.
Constructor and destructor.
Initialize for search process. This should be called before starting the actual search with method Run().
- (BOOL)Begin:(PTPDFDoc *)doc pattern:(NSString *)pattern mode:(unsigned int)mode start_page:(int)start_page end_page:(int)end_page;
func begin(_ doc: PTPDFDoc!, pattern: String!, mode: UInt32, start_page: Int32, end_page: Int32) -> Bool
the PDF document to search in.
the pattern to search for. When regular expression is used, it contains the expression, and in verbatim mode, it is the exact string to search for.
the mode of the search process.
the start page of the page range to search in. The default value is -1 indicating the range starts from the first page.
the end page of the page range to search in. The default value is -1 indicating the range ends at the last page.
true if the initialization has succeeded.
Search the document and returns upon the following circumstances: a)Reached the end of the document; b)Reached the end of a page (if set to return by specifying mode ‘e_page_stop’ ); c)Found an instance matching the search pattern.
Note that this method should be called in a loop in order to find all matching instances; in other words, the search is conducted in an incremental fashion.
Set the current search pattern. Note that it is not necessary to call this method since the search pattern is already set when calling the Begin() method. This method is provided for users to change the search pattern while searching through a document.
- (BOOL)SetPattern:(NSString *)pattern;
func setPattern(_ pattern: String!) -> Bool
the search pattern to set.
true if the setting has succeeded.
Retrieve the current search mode.
- (unsigned int)GetMode;
func getMode() -> UInt32
the current search mode.
Set the current search mode. For example, the following code turns on the regular expressions:
… TextSearch::Mode mode = ts.GetMode(); mode |= TextSearch::e_reg_expression; ts.SetMode(mode); …
- (void)SetMode:(unsigned int)mode;
func setMode(_ mode: UInt32)
the search mode to set.
Tells TextSearch that language is from right to left.
func setRightToLeftLanguage(_ flag: Bool)
Set to true if the language is right to left.
Retrieve the number of the current page that is searched in. If the returned value is -1, it indicates the search process has not been initialized (e.g., Begin() is not called yet); if the returned value is 0, it indicates the search process has finished, and if the returned value is positive, it is a valid page number.
func getCurrentPage() -> Int32
the current page number.