Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically search for text in a PDF file and tell the page number? [closed]

There are some tools which allow to extract the whole text portion of a PDF file in order to full text index the PDF.

What I need is a way to search for certain strings and, if thery were found in the PDF file, return the page number?

like image 464
splattne Avatar asked Apr 02 '09 13:04

splattne


People also ask

How do I search for page numbers in a PDF?

Choose View > Navigation > [location]. Choose View > Navigation > Go To Page, type the page number in the Go To Page dialog box and then click OK.

How do I search for a specific text in a PDF?

When a PDF is opened in the Acrobat Reader (not in a browser), the search window pane may or may not be displayed. To display the search/find window pane, use "Ctrl+F".

How do I scrape text in a PDF?

Use Adobe Acrobat Professional. To extract information from a PDF in Acrobat DC, choose Tools > Export PDF and select an option. To extract text, export the PDF to a Word format or rich text format, and choose from several advanced options that include: Retain Flowing Text.


1 Answers

This example uses the library included with Adobe Reader, and comes from http://www.dotnetspider.com/resources/5040-Get-PDF-Page-Number.aspx:

using Acrobat;
using AFORMAUTLib;                          
private void pdfRandD(string fPath)
{
    AcroPDDocClass objPages = new AcroPDDocClass();
    objPages.Open(fPath);
    long TotalPDFPages = objPages.GetNumPages();            
    objPages.Close();
    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;
        }                
        if(k.Trim().Contains(searchWord))
        {
           int pNum = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageLabel(" + p + ",true);"));
           sw.WriteLine("The Word " + searchWord + " is exists in " + pNum);                    
        }

     }
     sw.Close();
     MessageBox.Show("Process completed");
}
like image 180
RossFabricant Avatar answered Oct 21 '22 05:10

RossFabricant