Skip to content
Guides

Custom Menu

When viewing a PDF, selecting text and performing a long press in a blank area triggers the PDF context to enter the corresponding interactive state. In different interactive states, a context menu similar to UIMenuController will appear, allowing you to execute relevant operations through the context menu options.

This example shows how to set a custom menu:

swift
func menuItems(at point: CGPoint, for page: CPDFPage) -> [UIMenuItem] {
    var items = super.menuItems(at: point, for: page) ?? []

    var menuItems = [UIMenuItem]()
    
    if let currentSelection = self.currentSelection {
        let textNoteItem = UIMenuItem(title: NSLocalizedString("Note", comment: ""),
                                      action: #selector(textNoteItemAction(_:)))
        let textShareItem = UIMenuItem(title: NSLocalizedString("Share", comment: ""),
                                       action: #selector(textShareItemAction(_:)))
        let defineItem = UIMenuItem(title: NSLocalizedString("Define", comment: ""),
                                    action: #selector(defineItemAction(_:)))
        let linkItem = UIMenuItem(title: NSLocalizedString("Link", comment: ""),
                                  action: #selector(linkItemAction(_:)))
        let searchItem = UIMenuItem(title: NSLocalizedString("Search", comment: ""),
                                    action: #selector(searchItemAction(_:)))
        
        menuItems.append(textNoteItem)
        menuItems += [textShareItem, defineItem, linkItem, searchItem]
    } else {
        let textNoteItem = UIMenuItem(title: NSLocalizedString("Note", comment: ""),
                                      action: #selector(textNoteItemAction(_:)))
        let textItem = UIMenuItem(title: NSLocalizedString("Text", comment: ""),
                                  action: #selector(textItemAction(_:)))
        let pasteItem = UIMenuItem(title: NSLocalizedString("Paste", comment: ""),
                                   action: #selector(pasteItemAction(_:)))
        
        menuItems += [textNoteItem, textItem]
        
        let textType = kUTTypeText as String
        let urlType = kUTTypeURL as String
        let urlFileType = kUTTypeFileURL as String
        let jpegImageType = kUTTypeJPEG as String
        let pngImageType = kUTTypePNG as String
        let rawImageType = "com.apple.uikit.image"
        
        let isPasteboardValid = UIPasteboard.general.contains(pasteboardTypes: [textType, urlType, urlFileType, jpegImageType, pngImageType, rawImageType])
        if isPasteboardValid {
            menuItems.append(pasteItem)
        }
    }
    
    return menuItems + items
}
objective-c
- (NSArray<UIMenuItem *> *)menuItemsAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
    NSArray *items = [super menuItemsAtPoint:point forPage:page];
    
    NSMutableArray *menuItems = [NSMutableArray arrayWithArray:items];
    if (self.currentSelection) {
        UIMenuItem *textNoteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", nil)
                                                              action:@selector(textNoteItemAction:)];
        UIMenuItem *textShareItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Share", nil)
                                                               action:@selector(textShareItemAction:)];
        UIMenuItem *defineItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Define", nil)
                                                            action:@selector(defineItemAction:)];
        UIMenuItem *linkItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Link", nil)
                                                          action:@selector(linkItemAction:)];
        UIMenuItem *searchItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Search", nil)
                                                            action:@selector(searchItemAction:)];
        [menuItems insertObject:textNoteItem atIndex:0];
        [menuItems addObject:textShareItem];
        [menuItems addObject:defineItem];
        [menuItems addObject:linkItem];
        [menuItems addObject:searchItem];
    } else {
        UIMenuItem *textNoteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", nil)
                                                              action:@selector(textNoteItemAction:)];
        UIMenuItem *textItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Text", nil)
                                                          action:@selector(textItemAction:)];
        UIMenuItem *pasteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Paste", nil)
                                                           action:@selector(pasteItemAction:)];
        [menuItems addObject:textNoteItem];
        [menuItems addObject:textItem];
        
        NSString *textType = (NSString *)kUTTypeText;
        NSString *urlType  = (NSString*)kUTTypeURL;
        NSString *urlFileType  = (NSString*)kUTTypeFileURL;
        NSString *jpegImageType = (NSString *)kUTTypeJPEG;
        NSString *pngImageType = (NSString *)kUTTypePNG;
        NSString *rawImageType = @"com.apple.uikit.image";
        BOOL isPasteboardValid = [[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:textType, urlType, urlFileType, jpegImageType, pngImageType, rawImageType, nil]];
        if (isPasteboardValid) {
            [menuItems addObject:pasteItem];
        }
    }
    return menuItems;
}