How do I search for text inside a PDF?

PDF Text search C#

  • I have requirement to read a pdf file and search for a text. I should display in which page that text exist and the number of occurances. I can read the pdf to text but i need to know the page number. Thanks

  • Answer:

    You can use http://bitmiracle.com/pdf-library/ for this (I work for Bit Miracle). Docotic.Pdf comes with commercial and free licenses. Here is a sample for how to search text in PDF: PdfDocument doc = new PdfDocument("file.pdf"); string textToSearch = "some text"; for (int i = 0; i < doc.Pages.Count; i++) { string pageText = doc.Pages[i].GetText(); int count = 0; int lastStartIndex = pageText.IndexOf(textToSearch, 0, StringComparison.CurrentCultureIgnoreCase); while (lastStartIndex != -1) { count++; lastStartIndex = pageText.IndexOf(textToSearch, lastStartIndex + 1, StringComparison.CurrentCultureIgnoreCase); } if (count != 0) Console.WriteLine("Page {0}: '{1}' found {2} times", i, textToSearch, count); } You may want to remove third argument for IndexOf method if you want to perform case-sensitive search.

dps123 at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Have you checked itextsharp out? http://itextsharp.sourceforge.net/ EDIT: To elaborate, in the TOC, i saw a section on: 15.3.3: Extracting text with PdfReaderContentParser and PdfTextExtractor And under PdfReaderContentParser: http://api.itextpdf.com/com/itextpdf/text/pdf/parser/PdfReaderContentParser.html there is an option to process the pdf content per page. So it seems to be a round about way, but you can iterate through each page, searching the content for the word that you want and then return the page that you found it under.

Joe

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.