
You may need to locate specific information, a phrase, or a word in your documents. As a C# developer, you can programmatically search PDF files in .NET applications. This article shows how to search text in PDF documents using C#.
The following topics are discussed/covered in this article:
- C# API for Searching Text
- Search Text in PDF Documents using C#
- Case-Sensitive Text Search in PDF using C#
C# API for Searching Text
For searching text in PDF documents, I will be using GroupDocs.Search for .NET API. It allows you to perform text search operations in all popular document formats such as PDF, Word, Excel, PowerPoint, and many more. It also enables you to fetch your required information from files, documents, emails, and archives. You can create and merge multiple indexes to rapidly and smartly search through them using simple, Boolean, Regular Expression (Regex), Fuzzy, and other types of queries.
You can either download the DLL of the API or install it using NuGet.
Install-Package GroupDocs.Search
Search Text in PDF Documents using C#
You can search for any text or a specific word in your PDF documents programmatically by following the simple steps mentioned below:
- Create an instance of the Index class
- Specify the path to the index folder
- Subscribe to the index events
- Add PDF files to the Index by calling the Add() method
- Define a search query
- Perform a search using the Search() method with search query
- Use the SearchResult and print summary
- Highlight the searched results in the output using the Highlight() method
The following code sample shows how to search text in PDF documents using C#.
The above code sample will generate the following output:
Documents found: 1
Total occurrences found: 4
Document: C:\Files\Files\sample.pdf
Occurrences: 4
Generated HTML file can be opened with Internet browser.
The file can be found by the following path:
C:\Files\Files\Highlighted.html

Highlighted the Searched Text in PDF Documents using C#
The Index and Index Event
The Index class handles document indexing and searching. You can create an index in memory or on disk via its constructor. In the example, the index is stored on disk for reuse.
The ErrorOccurred event reports errors that occur while indexing files, so subscribe to it to receive error information.
Add Files to the Index
The Add() method adds a single file or all files in a folder (including subfolders) using an absolute or relative path, indexing every document found.
Perform a Search Operation
The Index class offers several Search methods. Search with a simple keyword or a detailed SearchQuery.
The SearchResult class provides details of a search result matching a search query. The following methods and properties of this class facilitate getting details of search results:
- The OccurrenceCount property shows the total number of occurrences found.
- The DocumentCount property provides the number of documents found in the Index.
- The GetFoundDocument(int) method returns the FoundDocument by its index.
- The FoundDocument.OccurrenceCount property returns the number of occurrences found in the document.
Highlight the Search Results
The HtmlHighlighter class creates HTML that highlights search results throughout the document.
The Highlight() method returns HTML with highlighted terms. See “Highlighting Search Results” for details.
Case-Sensitive Text Search in PDF using C#
You can perform case‑sensitive searches for specific phrases or words in PDF files by following these steps:
- Create an instance of the Index class
- Specify the path to the index folder
- Add PDF files to the Index by calling the Add() method
- Create an instance of the SearchOptions
- Set the UseCaseSensitiveSearch property to true
- Define a search query
- Perform a search using the Search() method with search query and the SearchOptions
- Use the SearchResult and print summary
The code sample below demonstrates how to perform a case‑sensitive text search in a PDF document using C#.
Documents found: 1
Total occurrences found: 2
Document: C:\Files\Files\sample.pdf
Occurrences: 2
The SearchOptions class provides options to perform the search operations. The UseCaseSensitiveSearch property of this class allows you to perform a case‑sensitive search for a word or text.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to search text in a PDF document using C#. You have also learned how to perform a case‑sensitive text search in a PDF document using C#. You can learn more about GroupDocs.Search for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.