Skip to content
Guides

Custom Menu

The CPDFView supports a right-click menu feature. Right-clicking within the CPDFView area will trigger a custom menu.

You can set different custom menus for users in various contexts. For instance, setting the page zoom level when right-clicking in a blank area, enabling copy options when right-clicking on text, images, or annotations, etc.

The steps for setting up a custom menu are as follows:

  1. Register for the custom menu event.
  2. Take action based on the command type (e.CommandType). When the command type is Context, create a context menu and add menu items (Copy, Cut, Paste, Delete).
  3. When the command type is a basic command (Copy, Cut, Paste, Delete), directly call the e.DoCommand() method to execute the corresponding command.

This example shows how to create a custom menu:

C#
// Register custom menu event
myCPDFViewer.AnnotCommandHandler += PdfViewer_AnnotCommandHandler;
private void myCPDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
{

// Operate based on the command type (`e.CommandType`). When the command type is `Context`, create a context menu and add menu items (Copy, Cut, Paste, Delete).
    switch (e.CommandType)
    {
        case CommandType.Context:
            e.Handle = true;
            e.PopupMenu = new ContextMenu();
            e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
            e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
            e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
            e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
            break;
            
// When the command type is a basic command (Copy, Cut, Paste, Delete), directly invoke the `e.DoCommand()` method to execute the corresponding command.
        case CommandType.Copy:
        case CommandType.Cut:
        case CommandType.Paste:
        case CommandType.Delete:
            e.DoCommand();
            break;
        default:
            break;
    }
}