Skip to content
Guides

Create a Bookmark in PDF

This sample shows how to create a new bookmark and implement a bookmark jump.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self createBookmark:myDocument];
}

- (void)createBookmark:(CPDFDocument *)oldDocument
  NSString *commandLineStr = @""
  
// Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bookmark"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"CreateBookmarkTest"];
    
    // Save the document in the test PDF file
    NSURL *bookmarkURL = [NSURL fileURLWithPath:writeFilePath];
    [document writeToURL:self.bookmarkURL];
    
    // Open test pdf document
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:bookmarkURL];
    
    // Add bookmark
    [document addBookmark:@"my bookmark" forPageIndex:1];
    
    if ([document bookmarkForPageIndex:1]) {
        commandLineStr = [self.commandLineStr stringByAppendingString:@"Go to page 2\n"];
    }
    
    // Save the add bookmark action in document
    [document writeToURL:bookmarkURL];
  commandLineStr = [self.commandLineStr stringByAppendingString:@"Done.\n"];
    commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in CreateBookmarkTest.pdf\n"];
}