Skip to content
ComPDF
Guides

Custom Context Menu

Since ComPDF Flutter SDK 2.6.0, the SDK supports custom context menu items and provides corresponding click event callback mechanisms to respond to user interaction with custom menu buttons.

For information on how to configure and add custom context menu items, please refer to Context Menu.

Listen to Custom Context Menu Item Click Events

When creating CPDFReaderWidget, you can set the onCustomContextMenuItemTappedCallback callback to listen to custom context menu item click events. For example:

dart
CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onCreated: (controller) {},
  onCustomContextMenuItemTappedCallback: (String identifier, dynamic event) {
    debugPrint('Custom context menu item tapped: $identifier');
    // Handle custom context menu item click logic here
  },
);

Callback Function Explanation

The function signature of onCustomContextMenuItemTappedCallback is as follows:

dart
void Function(String identifier, dynamic event)
Parameter NameTypeDescription
identifierStringUnique identifier of the clicked custom menu item. Specified via CPDFContextMenuItem when configuring menu items.
eventdynamicContext data (payload) carried by the click event. Different menu contexts and menu types return different data structures.

Event Data Structure Explanation

Note: event is usually returned in the form of Map<String, dynamic>. When the return value contains CPDFAnnotation, CPDFWidget, and other objects, you can further retrieve detailed attribute information through the corresponding object's API.


1) Global

screenshot (Screenshot-related menu)

dart
event = {
  "identifier": String,
  "image": Uint8List,
};
FieldTypeDescription
identifierStringMenu item identifier
imageUint8ListScreenshot image data

2) View Mode

textSelect (Text selection in view mode)

dart
event = {
  "identifier": String,
  "text": String,
  "rect": CPDFRectF,
  "pageIndex": int,
};
FieldTypeDescription
identifierStringMenu item identifier
textStringSelected text content
rectCPDFRectFText selection area on page
pageIndexintPage index containing the text

3) Annotation Mode

textSelect (Text selection in annotation mode)

dart
event = {
  "identifier": String,
  "text": String,
  "rect": CPDFRectF,
  "pageIndex": int,
};

longPressContent (Long press page content)

dart
event = {
  "identifier": String,
  "point": CPDFPointF,
  "pageIndex": int,
};

markupContent / soundContent / inkContent / shapeContent / freeTextContent / signStampContent / stampContent / linkContent

When the context menu corresponds to an annotation object (such as highlight, ink, shape, free text, signature/stamp, link, etc.), event will return the corresponding annotation instance:

dart
event = {
  "annotation": CPDFAnnotation,
};

4) Content Editor Mode

editTextAreaContent / editSelectTextContent

(Content Editor: Text area / Selected text)

dart
event = {
  "editArea": CPDFEditTextArea,
};

imageAreaContent (Content Editor: Image area)

dart
event = {
  "imageArea": CPDFEditImageArea,
};

editPathContent (Content Editor: Path / Shape area)

dart
event = {
  "editArea": CPDFEditPathArea,
};

longPressWithEditTextMode / longPressWithEditImageMode / longPressWithAllMode

(Long press operations in content editor mode)

dart
event = {
  "point": Offset,
  "pageIndex": int,
};

5) Form Mode

textField / checkBox / radioButton / listBox / comboBox / signatureField / pushButton

(Form mode-related form field menus)

dart
event = {
  "widget": CPDFWidget,
};