PTTextSearch GetHighlights always empty

on iOS I,m trying to performa a search that would retrieve all occurrences a term including a AmbientString and Highlights.

The search work fine and return page, ambient string but the highlights are always empty.
Some of the sample code I found for it like "Complete Reader" on iOS have code for PTTextSearch but does not seem to be in use, the search is instead done by FindText of PTPDFViewCtrl.

I'm using PDFNet version 6.32

I've included my code below:

Mario.

PTTextSearch *textSearch = [[PTTextSearch alloc] init];
unsigned int mode = e_ptwhole_word | e_pthighlight | e_ptambient_string;
[self.searchResults removeAllObjects];
self.showSearchResult = YES;

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
    _isSearching = YES;
    [_pdfDoc Lock];
    if ([textSearch Begin:_pdfDoc pattern:searchText mode:mode start_page:-1 end_page:-1]) {
        PTSearchResult* result = [textSearch Run];
        while (result.IsFound && _isSearching) {
            
            if (result.GetHighlights.GetCurrentQuads.size == 0) {
                DDLogError(@"Mising highlights information");
            }
            [_pdfDoc Unlock];

            [self.searchResults addObject:result];
            
            DDLogDebug(@"PDF Search: %@ found at page %d, context : %@",result.GetMatch, result.GetPageNumber, result.GetAmbientString);

            dispatch_sync(dispatch_get_main_queue(), ^{
                if (self.viewerDelegate && [self.viewerDelegate respondsToSelector:@selector(viewer:didMatchString:)]) {
                    [self.viewerDelegate viewer:self didMatchString:searchResult];
                }
            });
            
            while (![_pdfDoc TryLock:0]) {
                [NSThread sleepForTimeInterval:1.0f];
            }
            result = [textSearch Run];
        }
    }
    [_pdfDoc Unlock];
    _isSearching = NO;
});

The problem is that Highlights actually exposes the iterator pattern. Here is the code from TextSearcj objective-c sample.

PTHighlights *hlts = [result GetHighlights]; [hlts Begin: doc]; while ( [hlts HasNext] ) { NSLog(@"The current highlight is from page: %d", [hlts GetCurrentPageNumber]); [hlts Next]; }

If you call [hlts GetCurrentQuads] you will get the quads.

Thanks for that, I can now see that I do get highlights data, but it still does not display in the viewer, isn't the method SelectWithHighlights on the PTPDFViewCtrl class (_pdfView below) suppose to take care of that?

in the code below I get an execution exception when trying to call GetCurrentQuads on hlts, but I was able to get call GetCurrentPageNumber and get the proper value for it.

<code>
    if ([selection.resultObject isKindOfClass:[PTSearchResult class]]) {
        PTSearchResult* result = (PTSearchResult*) selection.resultObject;
        DDLogDebug(@"Showing Search Results at page %d (%@).",result.GetPageNumber,result.GetHighlights.GetCurrentQuads);
        [_pdfView SetCurrentPage:result.GetPageNumber];
        PTHighlights *hlts = [result GetHighlights];
        [hlts Begin: _pdfDoc];
        if (![_pdfView SelectWithHighlights:hlts]) {
            DDLogDebug(@"Could not create selection from Highlights object");
        }
        while ([hlts HasNext]) {
            DDLogDebug(@"The current highlight is from page: %d", [hlts GetCurrentPageNumber]);
            PTVectorQuadPoint* vQuads = [hlts GetCurrentQuads];
            if (![vQuads isEmpty]) {
                for (int i =0; i < 8; i++) {
                    PTQuadPoint* qp = [vQuads get:i];
                    DDLogDebug(@"Quad Point1: %f, %f",qp.getP1.getX,qp.getP1.getY);
                    DDLogDebug(@"Quad Point2: %f, %f",qp.getP2.getX,qp.getP2.getY);
                    DDLogDebug(@"Quad Point3: %f, %f",qp.getP3.getX,qp.getP3.getY);
                    DDLogDebug(@"Quad Point4: %f, %f",qp.getP4.getX,qp.getP4.getY);
                }
            }
            [hlts Next];
        }
    }
</code>

I had the same issue - here’s my solution.

When a user selects a Search Result cell in my table view of results, this is the delegate callback in my PDF Controller:

  • (void)searchController:(OBDPDFSearchViewController *)searchController didSelectSearchResult:(PTSearchResult *)searchResult

{

[searchController dismissViewControllerAnimated:YES completion:nil];

[self clearSearchHighlights];

[self jumpToPage:searchResult.GetPageNumber];

PTHighlights *highlights = [searchResult GetHighlights];

[highlights Begin:self.pdfView.GetDoc];

[self.pdfView SelectWithHighlights:highlights];

PTSelection *selection = [self.pdfView GetSelection:self.pdfView.GetCurrentPage];

if (selection != nil)

{

[self.pdfView highlightSelection:selection withColor:[[UIColor obd_colorForType:OBDColorTypeRed] colorWithAlphaComponent:0.5]];

}

}

  • (void)clearSearchHighlights

{

[self.pdfView ClearSelection];

[self.pdfView hideSelectedTextHighlights];

}

If alternatively, you wanted to highlight ALL the results of a search, should be something like this… (note that PDF view will only select items on the current page, so as you change pages, you have to update it)

  • (void)searchFinished:(NSArray <PTSearchResult *> *)results

{

PTHighlights *allHighlights = [[PTHighlights alloc] init];

for (PTSearchResult *aResult in results)

{

[allHighlights Add:[aResult GetHighlights]];

}

[allHighlights Begin:self.pdfView.GetDoc];

[self.pdfView SelectWithHighlights:allHighlights];

[self updateHighlights];

}

  • (void)pageNumberChangedFrom:(int)oldPageNumber To:(int)newPageNumber

{

[self updateHighlights];

}

  • (void)updateHighlights

{

PTSelection *selection = [self.pdfView GetSelection:self.pdfView.GetCurrentPage];

if (selection != nil)

{

[self.pdfView highlightSelection:selection withColor:[[UIColor obd_colorForType:OBDColorTypeRed] colorWithAlphaComponent:0.5]];

}

}


Hope that is helpful

On Thursday, March 12, 2015 at 12:46:41 PM UTC-4, Mario Couture wrote:

On Wednesday, March 11, 2015 at 5:17:47 PM UTC-4, Ryan wrote:

The problem is that Highlights actually exposes the iterator pattern. Here is the code from TextSearcj objective-c sample.

PTHighlights *hlts = [result GetHighlights];
[hlts Begin: doc];
while ( [hlts HasNext] )
{
NSLog(@“The current highlight is from page: %d”, [hlts GetCurrentPageNumber]);
[hlts Next];
}
If you call [hlts GetCurrentQuads] you will get the quads.

Thanks for that, I can now see that I do get highlights data, but it still does not display in the viewer, isn’t the method SelectWithHighlights on the PTPDFViewCtrl class (_pdfView below) suppose to take care of that?

in the code below I get an execution exception when trying to call GetCurrentQuads on hlts, but I was able to get call GetCurrentPageNumber and get the proper value for it.

if ([selection.resultObject isKindOfClass:[PTSearchResult class]]) { PTSearchResult* result = (PTSearchResult*) selection.resultObject; DDLogDebug(@"Showing Search Results at page %d (%@).",result.GetPageNumber,result.GetHighlights.GetCurrentQuads); [_pdfView SetCurrentPage:result.GetPageNumber]; PTHighlights *hlts = [result GetHighlights]; [hlts Begin: _pdfDoc]; if (![_pdfView SelectWithHighlights:hlts]) { DDLogDebug(@"Could not create selection from Highlights object"); } while ([hlts HasNext]) { DDLogDebug(@"The current highlight is from page: %d", [hlts GetCurrentPageNumber]); PTVectorQuadPoint* vQuads = [hlts GetCurrentQuads]; if (![vQuads isEmpty]) { for (int i =0; i < 8; i++) { PTQuadPoint* qp = [vQuads get:i]; DDLogDebug(@"Quad Point1: %f, %f",qp.getP1.getX,qp.getP1.getY); DDLogDebug(@"Quad Point2: %f, %f",qp.getP2.getX,qp.getP2.getY); DDLogDebug(@"Quad Point3: %f, %f",qp.getP3.getX,qp.getP3.getY); DDLogDebug(@"Quad Point4: %f, %f",qp.getP4.getX,qp.getP4.getY); } } [hlts Next]; } }