To search a keyword in PDF documents Search(String) method is used. This method returns the collection of SearchResult objects. For details, see Search Text.
Here are the steps to search a keyword in PDF document:
Instantiate Parser object for the initial document;
Iterate through the collection and get the position and text.
Warning
Search(String) method returns null value if search isn’t supported for the document. For example, text extraction isn’t supported for Zip archive. Therefore, for Zip archive Search(String) method returns null. For empty PDF document Search(String) method returns an empty collection.
The following example shows how to find a keyword in PDF document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Search a keyword:IEnumerable<SearchResult>sr=parser.Search("page number");// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,s.Text));}}
Search(String, SearchOptions) is used for the advanced search in PDF documents - like search with regular expressions, search by pages etc. SearchOptions parameter is used to customize a search.
Here are the steps to search with a regular expression in PDF document:
Instantiate Parser object for the initial document;
Instantiate SearchOptions object with the parameters for the search;
Iterate through the collection and get the position and text.
The following example shows how to search with a regular expression in PDF 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));// Iterate over search resultsforeach(SearchResultsinsr){// Print an index and found text:Console.WriteLine(string.Format("At {0}: {1}",s.Position,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.