Links between documents

FastPdfKit supports links between pdf documents and when you clink one of them you get one of these two callbacks:

documentViewController:didReceiveRequestToGoToPage:ofFile:

documentViewController:didReceiveRequestToGoToDestinationNamed:ofFile:

With the first one you get directly the page number of the destination document, in the second case you need to call the MFDocumentManager's pageNumberForDestinationNamed: method to get the number.

The Simple target provides some useful implementation of these callbacks. In the MenuViewController class there are two new methods to get the params from the DocumentViewController, that has the MenuViewControllers as delegate (set it even in the actionOpenPlainDocument: method).

MenuViewController.m

-(IBAction)actionOpenPlainDocument:(id)sender {
    //  We are using NSBundle to lookup the file for us, but if you store the pdf somewhere else than the application
    //  bundle, you should use the NSFileManager instead or be able to provide the right file path for the file.

        NSString *filePath = [[NSBundle mainBundle]pathForResource:DOC_PLAIN ofType:@"pdf"];
    NSURL *documentUrl = [NSURL fileURLWithPath:filePath];

    //
    // Now that we have the URL, we can allocate an istance of the MFDocumentManager class (a wrapper) and use
    // it to initialize an MFDocumentViewController subclass    
    MFDocumentManager *aDocManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl];

    DocumentViewController *aDocViewController = [[DocumentViewController alloc]initWithDocumentManager:aDocManager];
    [aDocViewController setDocumentId:DOC_PLAIN];   // We use the filename as an ID. You can use whaterver you like, like the id entry in a database or the hash of the document.

    // This delegate has been added just to manage the links between pdfs, skip it if you just need standard visualization
    [aDocViewController setDelegate:self];

    //  In this example we use a navigation controller to present the document view controller but you can present it
    //  as a modal viewcontroller or just show a single PDF right from the beginning
    // [self presentModalViewController:aDocViewController animated:YES]; 

    [[self navigationController]pushViewController:aDocViewController animated:YES];

    [aDocViewController release];
    [aDocManager release];
}   

/* This method should be called from the DocumentViewController when you get a link to another document */

-(void)setLinkedDocument:(NSString *)documentName withPage:(NSUInteger)destinationPage orDestinationName:(NSString *)destinationName{
    NSArray *params = [NSArray arrayWithObjects:documentName,destinationName,[NSNumber numberWithInt:destinationPage], nil];
    [self performSelector:@selector(openDocumentWithParams:) withObject:params afterDelay:0.5];
}

/* This method opens a linked document after a delay to let you pop the controller */

-(void)openDocumentWithParams:(NSArray *)params{

    // Depending on the link format you need to manage the destination path accordingly to your own application path
    // In this example we are assuming that every document is placed in your application bundle at the same level
    NSString *filePath = [[NSBundle mainBundle]pathForResource:[[[params objectAtIndex:0] lastPathComponent] stringByDeletingPathExtension] ofType:@"pdf"];
    NSURL *documentUrl = [NSURL fileURLWithPath:filePath];    

    MFDocumentManager *aDocManager = [[MFDocumentManager alloc]initWithFileUrl:documentUrl];

    DocumentViewController *aDocViewController = [[DocumentViewController alloc]initWithDocumentManager:aDocManager];
    [aDocViewController setDocumentId:[params objectAtIndex:0]];

    int page;
    if([[params objectAtIndex:2] intValue] != -1){
        page = [[params objectAtIndex:2] intValue];
    } else {
        // We need to parse the pdf to get the correct page
        page = [aDocManager pageNumberForDestinationNamed:[params objectAtIndex:1]];
    }

    [aDocViewController setPage:page];
    [aDocViewController setDocumentDelegate:aDocViewController];
    [aDocViewController setDelegate:self];

    [[self navigationController]pushViewController:aDocViewController animated:YES];

    [aDocViewController release];
    [aDocManager release];

}

In the DocumentViewController.m you just need to pass the parameters to the root controller and dismiss the current document.

- (void)documentViewController:(MFDocumentViewController *)dvc didReceiveRequestToGoToDestinationNamed:(NSString *)destinationName ofFile:(NSString *)fileName{

    // We set the parameters to the MenuViewController that will open the other document as soon as this one is dismissed

    [(MenuViewController *)delegate setLinkedDocument:fileName withPage:-1 orDestinationName:destinationName];

    // We need to dismiss the document
    [self actionDismiss:nil];

}
- (void)documentViewController:(MFDocumentViewController *)dvc didReceiveRequestToGoToPage:(NSUInteger)pageNumber ofFile:(NSString *)fileName{

    // We set the parameters to the MenuViewController that will open the other document as soon as this one is dismissed

    [(MenuViewController *)delegate setLinkedDocument:fileName withPage:pageNumber orDestinationName:@""];    

    // We need to dismiss the document
    [self actionDismiss:nil];
}