Guides
自定义菜单
在查看 PDF 时,当用户选择文本或在空白区域长按时,PDF 会进入相应的交互状态。此时,系统会弹出上下文菜单(Context Menu),用于执行与当前交互状态相关的操作。
通过继承 CPDFContextMenuShowHelper 并重写对应方法,您可以自定义菜单布局、内容和交互行为。例如,高亮注释的上下文菜单可以包含“属性修改”、“复制文本”、“删除注释”等操作。
操作流程示意
tsx
用户操作
│
│ 选择文本 / 空白区域长按
▼
PDF 进入相应交互状态
│
│ 弹出上下文菜单
▼
自定义菜单项点击
│
│ 执行对应操作(修改属性 / 复制文本 / 删除注释等)
▼
刷新 PDF 页面 / 更新界面1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
示意图(高亮注释为例):
tex
+---------------------+
| 高亮注释 |
| Lorem ipsum... |
+---------------------+
│
▼ 长按 / 点击菜单
+---------------------+
| 属性 | 复制 | 删除 | <- 自定义上下文菜单
+---------------------+1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
示例:高亮注释自定义菜单
t_markup_annot_menu_layout.xml:
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_context_menu_window"
android:orientation="horizontal">
<TextView
android:id="@+id/attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="属性" />
<TextView
android:id="@+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复制" />
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
java
public class TContextMenuHelper extends CPDFContextMenuShowHelper {
@Override
public View getMarkupContentView(CPDFPageView pageView, CPDFBaseAnnotImpl annotImpl, LayoutInflater layoutInflater) {
View contentView = layoutInflater.inflate(R.layout.t_markup_annot_menu_layout, null);
CPDFMarkupAnnotImpl markupAnnotImpl = (CPDFMarkupAnnotImpl) annotImpl;
CPDFMarkupAnnotation markupAnnotation = markupAnnotImpl.onGetAnnotation();
// 设置菜单点击事件
invokeOnClickListener(contentView, v -> {
int id = v.getId();
if (id == R.id.attr) { // 修改属性
markupAnnotation.setColor(Color.RED);
markupAnnotation.setAlpha(100);
markupAnnotation.updateAp();
markupAnnotImpl.onAnnotAttrChange();
pageView.invalidate();
} else if (id == R.id.copy) { // 复制文本
String markedText = markupAnnotation.getMarkedText();
if (!TextUtils.isEmpty(markedText)) {
CPDFTextUtils.setClipData(context, "ComPDF", markedText);
}
} else if (id == R.id.delete) { // 删除注释
pageView.deleteAnnotation(annotImpl);
}
popupWindow.dismiss();
}, R.id.attr, R.id.copy, R.id.delete);
return contentView;
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
kotlin
class TContextMenuHelper(readerView: CPDFReaderView?) : CPDFContextMenuShowHelper(readerView) {
override fun getMarkupContentView(
pageView: CPDFPageView,
annotImpl: CPDFBaseAnnotImpl<*>,
layoutInflater: LayoutInflater
): View {
val contentView = layoutInflater.inflate(R.layout.t_markup_annot_menu_layout, null)
val markupAnnotImpl = annotImpl as CPDFMarkupAnnotImpl
val markupAnnotation = markupAnnotImpl.onGetAnnotation()
// 设置菜单点击事件
invokeOnClickListener(contentView, { v ->
when (v.id) {
R.id.attr -> { // 修改属性
markupAnnotation.color = Color.RED
markupAnnotation.alpha = 100
markupAnnotation.updateAp()
markupAnnotImpl.onAnnotAttrChange()
pageView.invalidate()
}
R.id.copy -> { // 复制文本
val markedText = markupAnnotation.markedText
if (markedText.isNotEmpty()) {
CPDFTextUtils.setClipData(context, "ComPDF", markedText)
}
}
R.id.delete -> { // 删除注释
pageView.deleteAnnotation(annotImpl)
}
}
popupWindow.dismiss()
}, R.id.attr, R.id.copy, R.id.delete)
return contentView
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
设置 CPDFReaderView 使用自定义上下文菜单:
java
getCPdfReaderView().setContextMenuShowListener(new TContextMenuHelper(readerView));1
可自定义上下文菜单方法列表
| 方法名 | 适用注释/操作类型 | 说明 |
|---|---|---|
| getShapeContentView | 图形注释 | 自定义图形类注释的上下文菜单 |
| getFreetextContentView | 自由文本注释 | 自由文本注释的上下文菜单 |
| getMarkupContentView | 高亮/下划线/波浪线注释 | 标记类注释的上下文菜单 |
| getInkContentView | 手绘/签名注释 | 手绘注释的上下文菜单 |
| getStampContentView | 印章注释 | 印章注释的上下文菜单 |
| getLinkContentView | 链接注释 | 链接注释的上下文菜单 |
| getNoteContentView | 批注/便签 | 便签注释的上下文菜单 |
| getSoundContentView | 音频注释 | 音频注释的上下文菜单 |
| getCheckboxContentView | 表单-复选框 | 复选框表单的上下文菜单 |
| getComboboxContentView | 表单-下拉框 | 下拉框表单的上下文菜单 |
| getListboxContentView | 表单-列表框 | 列表框表单的上下文菜单 |
| getPushbuttonContentView | 表单-按钮 | 按钮表单的上下文菜单 |
| getRadiobuttonContentView | 表单-单选按钮 | 单选按钮表单的上下文菜单 |
| getSignatureContentView | 数字签名 | 签名区域上下文菜单 |
| getTextfieldContentView | 表单-文本框 | 文本框表单上下文菜单 |
| getSelectTextContentView | 文字选择 | 选中文字的上下文菜单 |
| getEditImageAreaContentView | 图片编辑区域 | 编辑图片区域的上下文菜单 |
| getLongPressContentView | 空白区域长按 | 空白区域长按菜单 |
| getEditLongPressContentView | 编辑模式长按 | 编辑模式下长按菜单 |
| getEditTextAreaContentView | 文本区域编辑 | 文本编辑区域菜单 |
| getEditSelectTextContentView | 编辑模式选中文本 | 文本选择菜单 |
| getEditTextContentView | 编辑文本 | 文本编辑菜单 |
| getRedactContentView | 涂黑/隐藏内容 | 涂黑操作菜单 |
| getCropImageAreaContentView | 图片裁剪 | 裁剪图片区域菜单 |
| getSearchReplaceContentView | 查找/替换 | 查找替换菜单 |
| getEditPathAreaContentView | 路径编辑 | 路径编辑菜单 |
| getScreenShotContentView | 截图区域 | 截图操作菜单 |
说明:表格中列出的所有方法均可通过继承 CPDFContextMenuShowHelper 并重写实现自定义菜单。不同方法对应不同注释类型或操作类型,开发者可根据需求重写菜单布局和行为。