Tutorial - Search Text

Top  Previous  Next

 

The following code snippet demonstrates how to SearchFirst and SearchNextOnPage function. Similarly, you can use SearchFirstOnPage and SearchNext.

 

[CS]

       axgtPDFDocumentX1.LoadFromFile("InputFile");

               

       PDFtoolkitX.gtPDFTextElementX TextElem;

       TextElem = axgtPDFDocumentX1.CreatePDFTextElement();

       int LI = axgtPDFDocumentX1.SearchFirst("the", ref TextElem, false, true);

       while(LI>0)

       {

MessageBox.Show("Fount At: "+ LI.ToString()+"\n"+"The String is : "+TextElem.Text, "Searching for \"the\"");

               LI = axgtPDFDocumentX1.SearchNextOnPage(ref TextElem, 1);                        

       }

 

[VB]

gtPDFDocumentX1.LoadFromFile ("InputFile")

Dim TextElem As gtPDFTextElementX

Set TextElem = gtPDFDocumentX1.CreatePDFTextElement()

Dim LI As Integer

LI = gtPDFDocumentX1.SearchFirst("the", TextElem, False, True)

While (LI > 0)

  MsgBox "Fount At: " + Str(LI) + " The String is : " + TextElem.Text, vbOKOnly, " Searching for 'the'"

  LI = gtPDFDocumentX1.SearchNextOnPage(TextElem, 1)

Wend

 

 

The next example demonstrates how to use GetSearchCount and GetSearchTextAt to search for all instances of a piece of text in the document.

 

 

[VB]

 

Dim TXTELE As gtPDFTextElementX

Dim Li, Ls As Integer

 

gtPDFDocumentX1.LoadFromFile "Input.pdf"

 

'Retrieve the count of all the instances of "search string" in the PDF file

Ls = gtPDFDocumentX1.GetSearchCount("search string", False, False)

 

For Li = 0 To Ls - 1

'Retrieve each TextElement

Set TXTELE = gtPDFDocumentX1.GetSearchTextAt(Li)

MsgBox TXTELE.Text

Next