Search a String in a PDF File

This article helps about how to use Search a String in a PDF File with code snippet

This module is used to search a string in a PDF file and it will produce a text file with page number where the search string is present. To run this module Adobe Professional is required.

using Acrobat;
using AFORMAUTLib;
private void pdfRandD(string fPath)
AcroPDDocClass objPages = new AcroPDDocClass();
long TotalPDFPages = objPages.GetNumPages();
AcroAVDocClass avDoc = new AcroAVDocClass();
avDoc.Open(fPath, "Title");
IAFormApp formApp = new AFormAppClass();
IFields myFields = (IFields)formApp.Fields;
string searchWord = "Search String";
string k = "";
StreamWriter sw = new StreamWriter(@"D:\KCG_FileChecker_Inputs\MAC\pdf\0230_525490_23_cha17.txt", false);
for (int p = 0; p < TotalPDFPages; p++)
int numWords = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageNumWords(" + p + ");"));
k = "";
for (int i = 0; i < numWords; i++)
string chkWord = myFields.ExecuteThisJavascript("event.value=this.getPageNthWord(" + p + "," + i + ", true);");
k = k + " " + chkWord;
int pNum = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageLabel(" + p + ",true);"));
sw.WriteLine("The Word " + searchWord + " is exists in " + pNum);
MessageBox.Show("Process completed");


Author: Vignesh21 May 2011 Member Level: Bronze   Points : 1

Hi Ravichandran,
Can I use this code snippet for web based applications too?
Also will this help me to find for text in other document types like Word, Excel, Text files etc?

Thanks in advance.

Guest Author: kishore kumar16 Mar 2012

when iam trying to run your throws as exception on
AcroPDDocClass objPages = new AcroPDDocClass();
the exception is COMexception was unhandled:
Retrieving the COM class factory for component with CLSID {FF76CB60-2E68-101B-B02E-04021C009402} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: