Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search for a Word in PDF using C#
As a C# developer, you can easily search any text from PDF documents programmatically in your .NET applications. Here, you will learn how to search for a word in PDF documents using C#.
// Specify path to the index folder
string indexFolder = @"C:\Files\Index\";
// Specify path to a folder containing PDF documents to search
string documentsFolder = @"C:\Files\Files\";
// create or load an index
Index index = new Index(indexFolder);
// Subscribe to index events
index.Events.ErrorOccurred += (sender, args) =>
{
// Writing error messages to the console
Console.WriteLine(args.Message);
};
// Add files synchronously
// Synchronous indexing documents from the specified folder
index.Add(documentsFolder);
// Perform search
string query = "Vestibulum"; // Specify a search query
SearchResult result = index.Search(query); // Searching in the index
// Use search results
// Printing the result
Console.WriteLine("Documents found: " + result.DocumentCount);
Console.WriteLine("Total occurrences found: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
Console.WriteLine("\tDocument: " + document.DocumentInfo.FilePath);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
}
// Highlight occurrences in text
if (result.DocumentCount > 0)
{
// Getting the first found document
FoundDocument document = result.GetFoundDocument(0);
string path = documentsFolder + "Highlighted.html";
// Creating the output adapter to a file
OutputAdapter outputAdapter = new FileOutputAdapter(path);
// Creating the highlighter object
HtmlHighlighter highlighter = new HtmlHighlighter(outputAdapter);
// Generating output HTML formatted document with highlighted search results
index.Highlight(document, highlighter);
Console.WriteLine();
Console.WriteLine("Generated HTML file can be opened with Internet browser.");
Console.WriteLine("The file can be found by the following path:");
Console.WriteLine(path);
}
// Specify path to the index folder
string indexFolder = @"C:\Files\Index\";
// Specify path to a folder containing PDF documents to search
string documentsFolder = @"C:\Files\Files\";
// Creating index in the specified folder
Index index = new Index(indexFolder);
// Indexing documents from the specified folder
index.Add(documentsFolder);
// Define search options
SearchOptions options = new SearchOptions();
options.UseCaseSensitiveSearch = true; // Enabling case sensitive search
// search word
string query = "Vestibulum";
// Perform the Search
SearchResult result = index.Search(query, options);
// Printing the result
Console.WriteLine("Documents found: " + result.DocumentCount);
Console.WriteLine("Total occurrences found: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
FoundDocument document = result.GetFoundDocument(i);
Console.WriteLine("\tDocument: " + document.DocumentInfo.FilePath);
Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment