Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub
Guides

自定义上下文菜单

上下文菜单中配置自定义菜单项,再通过 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}');
    }
  },
);

回调会返回自定义菜单项的 identifier 和动态事件对象。当前 Flutter SDK 会将事件对象转换为 Map<String, dynamic>,字段取决于菜单所在的上下文:

上下文主要字段和运行时类型
截图image: Uint8List
选中文本text: Stringrect: CPDFRectFpageIndex: int
长按页面包含 xypoint: Map<String, double>pageIndex: int
注释annotation: CPDFAnnotation
内容编辑区域editArea: CPDFEditArea,文本和图片区域使用对应的子类
表单字段widget: CPDFWidget

请使用回调的第一个参数识别菜单项,不要依赖事件 Map 中同时包含 identifier

菜单配置请参阅主工具栏上下文菜单