The keyword parameter can contain a text or a regular expression. SearchResult class contains every occurrence of the keyword in the document text. This class has the following members:
A zero-based index of the start position of the search result. Depending on SearchOptions.SearchByPages property value this index starts from the document start or the document page start.
Check if collection isn’t null (search is supported for the document);
Iterate through the collection and get position and text.
The following example shows how to find a keyword in a document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Search a keyword:IEnumerable<SearchResult>sr=parser.Search("page number");// Check if search is supportedif(sr==null){Console.WriteLine("Search isn't supported");return;}// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,s.Text));}}
Search text by regular expression
SearchOptions parameter is used to customize a search. This class has the following members:
Check if collection isn’t null (search is supported for the document);
Iterate through the collection and get position and text.
The following example shows how to search with a regular expression in a document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Search with a regular expression with case matchingIEnumerable<SearchResult>sr=parser.Search("page number: [0-9]+",newSearchOptions(true,false,true));// Check if search is supportedif(sr==null){Console.WriteLine("Search isn't supported");return;}// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,s.Text));}}
Search text with highlights
Here are the steps to search text with a highlights:
Instantiate Parser object for the initial document;
Instantiate HighlightOptions object with the parameters for the highlight extraction;
Instantiate SearchOptions object with the parameters for the search;
Check if collection isn’t null (search is supported for the document);
Iterate through the collection and get position and text.
The following example shows how to search a text with the highlights:
// Create an instance of Parser classusing(Parserparser=newParser(Constants.SamplePdf)){HighlightOptionshighlightOptions=newHighlightOptions(15);// Search a keyword:IEnumerable<SearchResult>sr=parser.Search("lorem",newSearchOptions(true,false,false,highlightOptions));// Check if search is supportedif(sr==null){Console.WriteLine("Search isn't supported");return;}// Iterate over search resultsforeach(SearchResultsinsr){// Print the found text and highlights: Console.WriteLine(string.Format("{0}{1}{2}",s.LeftHighlightItem.Text,s.Text,s.RightHighlightItem.Text));}}
Search text with page numbers
Here are the steps to search text with page numbers:
Instantiate Parser object for the initial document;
Instantiate SearchOptions object with the parameters for the search;
Check if collection isn’t null (search is supported for the document);
Iterate through the collection and get position, text and page number.
The following example shows how to search a text with page numbers:
// Create an instance of Parser classusing(Parserparser=newParser(Constants.SamplePdf)){// Search a keyword with page numbersIEnumerable<SearchResult>sr=parser.Search("lorem",newSearchOptions(false,false,false,true));// Check if search is supportedif(sr==null){Console.WriteLine("Search isn't supported");return;}// Iterate over search resultsforeach(SearchResultsinsr){// Print an index, page number and found text:Console.WriteLine(string.Format("At {0} (page {1}): {2}",s.Position,s.PageIndex,s.Text));}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.