Some test text!

Search
Hamburger Icon

iOS / Guides

Text search

There are two parts to text searching. First is the UI component that is presented to the user. Second is an API guide to perform the text search functionality.

Text search API for iOS

To search for text in a PDF using regular expression and then apply a link annotation on the highlighted result.

In this example, we add a link annotation but any other types of annotations can be applied here such as redaction annotations in the case of a search and redact workflow.
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTTextSearch *txt_search = [[PTTextSearch alloc] init];
unsigned int mode = e_ptwhole_word | e_ptpage_stop;
NSString *pattern = @"";

//use regular expression to find credit card number
mode |= e_ptreg_expression | e_pthighlight;
[txt_search SetMode: mode];
pattern = @"\\d{4}-\\d{4}-\\d{4}-\\d{4}"; //or "(\\d{4}-){3}\\d{4}"
[txt_search SetPattern: pattern];

//call Begin() method to initialize the text search.
[txt_search Begin: doc pattern: pattern mode: mode start_page: -1 end_page: -1];
PTSearchResult *result = [txt_search Run];

if ( result ) 
{
  //add a link annotation based on the location of the found instance
  PTHighlights *hlts = [result GetHighlights];
  [hlts Begin: doc];
  while ( [hlts HasNext] )
  {
    PTPage *cur_page = [doc GetPage: [hlts GetCurrentPageNumber]];
    PTVectorQuadPoint *quads = [hlts GetCurrentQuads];
    int i = 0;
    for ( ; i < [quads size]; ++i )
    {
      //assume each quad is an axis-aligned rectangle
      PTQuadPoint *q = [quads get: i];
      double x1 = MIN(MIN(MIN([[q getP1] getX], [[q getP2] getX]), [[q getP3] getX]), [[q getP4] getX]);
      double x2 = MAX(MAX(MAX([[q getP1] getX], [[q getP2] getX]), [[q getP3] getX]), [[q getP4] getX]);
      double y1 = MIN(MIN(MIN([[q getP1] getY], [[q getP2] getY]), [[q getP3] getY]), [[q getP4] getY]);
      double y2 = MAX(MAX(MAX([[q getP1] getY], [[q getP2] getY]), [[q getP3] getY]), [[q getP4] getY]);
      PTPDFRect * rect = [[PTPDFRect alloc] initWithX1: x1 y1: y1 x2: x2 y2: y2];
      PTAction *action = [PTAction CreateURI: [doc GetSDFDoc] uri: @"http://www.pdftron.com"];

      PTLink *hyper_link = [PTLink CreateWithAction: [doc GetSDFDoc] pos: rect action: action];
      [cur_page AnnotPushBack: hyper_link];
    }
    [hlts Next];
  }
}

Search PDF files for text
Full code sample which shows how to use TextSearch to search text on PDF pages using regular expressions.

Get the answers you need: Chat with us