Search code examples
objective-ccocoapdfquartz-graphics

In a Cocoa Mac Os X Objective-C Application, How to implement a Search in the pdf page currently displayed by a PDFView?


I am having problems with searching a PDFView in a Cocoa Application. Now I have set up an IBAction containing the code for selecting a specific word ("continue") that I know resides in the current pdf page in my tests. But The Word is never selected, although my code gathers the PDFSelection objects array correctly, finding only 1 occurrence. My code is :

-(IBAction)sampleAction:(id)sender
{
  NSArray *results = [self.pdfView.document findString:@"continue" withOptions:NSCaseInsensitiveSearch];

for (PDFSelection *pdfSel in results)
{

    [pdfSel drawForPage:self.pdfView.currentPage active:YES];

}

[self.pdfView scrollSelectionToVisible:self];

}

Nevertheless, in the currently displayed page, the word continue is there, but I get no selection. Please help !


Solution

  • Try finding the next occurrence with:

    PDFSelection *pdfSel=[self.pdfView.document findString: @"continue"
      fromSelection: self.pdfView.currentSelection
      withOptions: NSCaseInsensitiveSearch];
    
    if (pdfSel != nil)
    {
        [self.pdfView setCurrentSelection: pdfSel];
        [self.pdfView scrollSelectionToVisible: self];
    }