Skip to content
ComPDF
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Guides

Custom Context Menu

Configure custom context menu items in Context Menu, then handle taps with onCustomContextMenuItemTappedCallback.

dart
CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onCustomContextMenuItemTappedCallback: (identifier, event) {
    if (event is! Map) {
      return;
    }

    final data = Map<String, dynamic>.from(event);
    final annotation = data['annotation'];
    if (annotation is CPDFAnnotation) {
      debugPrint('$identifier: ${annotation.type}');
    }
  },
);

The callback receives the custom item's identifier and a dynamic event object. The Flutter SDK currently converts the event object to a Map<String, dynamic> whose fields depend on the context:

ContextMain fields and runtime types
Screenshotimage: Uint8List
Selected texttext: String, rect: CPDFRectF, pageIndex: int
Long-pressed pagepoint: Map<String, double> containing x and y, pageIndex: int
Annotationannotation: CPDFAnnotation
Content edit areaeditArea: CPDFEditArea; text and image areas use their corresponding subclasses
Form fieldwidget: CPDFWidget

Use the callback's first parameter to identify the menu item. Do not depend on the event Map also containing identifier.

For menu configuration, see Main Toolbar and Context Menu.