Guides
上下文菜单
在创建 CPDFReaderWidget 时,你可以通过 CPDFConfiguration 对象中的 contextMenuConfig 字段,来自定义当注释、文本、图片或表单字段被选中时弹出的上下文菜单。
默认上下文菜单
默认情况下,当用户选中一个高亮注释时,ComPDF 会显示包含常用选项的上下文菜单,例如“备注”、“删除”和“属性”:
| Android | iOS |
|---|---|
![]() | ![]() |
配置菜单项
你可以通过指定要包含的菜单项来自定义上下文菜单。以下示例仅为高亮注释保留 属性 和 删除 两个选项:
dart
CPDFReaderWidget(
document: widget.documentPath,
password: widget.password,
configuration: CPDFConfiguration(
contextMenuConfig: const CPDFContextMenuConfig(
annotationMode: CPDFAnnotationModeContextMenu(
markup: [
CPDFContextMenuItem(CPDFAnnotationMarkupMenuKey.properties),
CPDFContextMenuItem(CPDFAnnotationMarkupMenuKey.delete),
]
)
)
),
onCreated: (controller) {
},
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Android | iOS |
|---|---|
![]() | ![]() |
自定义菜单项
ComPDF Flutter SDK 在 2.6.0 版本中引入了添加自定义菜单项的功能。以下示例演示如何在高亮注释的上下文菜单中添加自定义菜单项:
- 创建
CPDFContextMenuItem实例,配置自定义菜单项:
dart
CPDFConfiguration(
contextMenuConfig: const CPDFContextMenuConfig(
fontSize: 14, // 自定义菜单字体大小
iconSize: 36, // 自定义菜单图标大小(仅支持 Android)
padding: [0, 4, 0, 4], // 菜单内边距
annotationMode: CPDFAnnotationModeContextMenu(
markup: [
CPDFContextMenuItem(
CPDFAnnotationMarkupMenuKey.custom,
identifier: 'custom_show_annotation_properties_action', // 必填:唯一标识符
title: 'Custom Action', // 必填:菜单标题(当 showType 为 text 时)
icon: 'ic_test_properties', // 可选:图标名称(仅 Android,当 showType 为 icon 时必填)
showType: CPDFContextMenuShowType.text, // 显示类型: icon 或 text
)
]
),
)
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 通过
onCustomContextMenuItemTappedCallback回调处理自定义菜单项的点击事件:
dart
CPDFReaderWidget(
document: documentPath,
configuration: configuration,
onCreated: (controller) {
setState(() {
_controller = controller;
});
},
onCustomContextMenuItemTappedCallback: (String identifier, dynamic event) {
if (identifier == 'custom_show_annotation_properties_action') {
// 处理自定义菜单项的点击事件
// 例如:显示自定义对话框、执行特定操作等
}
},
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15



