Search code examples
iosobjective-cpdftronpdfnet

How to stop a PDFNet Action from executing?


Basically I'm trying intercept taps on links to a remote file and instead open a local version that I already have stored.

I'm able to get to the action of the link after it is tapped, but I can't figure out how to stop it from executing the default behavior while keeping the URL intact.

Here's the relevant code:

- (void)pdfScrollViewTap:(UITapGestureRecognizer *)gestureRecognizer
{

Annot *annotation;

@try {
    // If they are clicking an annotation, we don't want to process this
    [self.pdfView DocLockRead];
    CGPoint down = [gestureRecognizer locationInView:self.pdfView];
    annotation = [self.pdfView GetAnnotationAt:down.x y:down.y];
}
@catch (NSException *exception) {
    // something is wrong with the annotation, it may not be selected properly
}
@finally {
    [self.pdfView DocUnlockRead];
}

// This is how we find out a Link was tapped
if ([annotation IsValid]) {
    if (annotation.GetType == e_Link) { // Its a link
        Link *link = [[Link alloc] initWithAnn:annotation]; // here's the link
        Action *action = link.GetAction; // links have an action, heres that
        if ([action IsValid]) { // hopefully its valid
            if (action.GetType == e_URI) { // URI is the URL the link points to
                if ([self.delegate respondsToSelector:@selector(shouldOpenURLforLinkAnnotation:withViewController:)]) { // Check if the delegate implements this
                    NSString *origURL = [action.GetSDFObj FindObj:@"URI"].GetAsPDFText;
                    if (![self.delegate shouldOpenURLforLinkAnnotation:origURL withViewController:self]) {
                        // The delegate handles finding and opening the local file

                        // Find a away to disable or stop the action here

                    }
                }
            }
        }
    }
    return;
}
}

I've tried simply removing the action with:

[link RemoveAction];

This works the first time, but as expected the action is never called again after removing it.


Solution

  • The link action is being executed by the tools code,which implements all of the user-interaction features such as text selection, form filling, annotation creation/editing, and link following. To customize the link handling behavior you should modify AnnotEditTool.m's source in /Lib/src/PDFViewCtrlTools/Tools and compile a new copy of libTools.a The relevant section of code is

    else if( actionType == e_URI )
    {
        Obj* sdfObj = [action GetSDFObj];
        Obj* uriObj = [sdfObj FindObj:@"URI"];
    
        if( uriObj != Nil )
        {
            NSString* uriDestination = [uriObj GetAsPDFText];
    
            // appends http if no scheme is present
            uriDestination = [Link GetNormalizedUrl:uriDestination];
    
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.4 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString: uriDestination]];
            });
    
    
    
    
            return YES;
        }
    }
    

    You will want to do something other than openURL: