Skip to content

Instantly share code, notes, and snippets.

@macteo
Created August 19, 2011 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macteo/1156455 to your computer and use it in GitHub Desktop.
Save macteo/1156455 to your computer and use it in GitHub Desktop.
Support linked documents in FastPdfKit
- (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];
}
-(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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment