Skip to content
Guides

上下文菜单

选中注释、文本、表单或内容编辑对象时,会弹出上下文菜单(Context Menu)以支持对象操作。ContextMenuConfig 可控制菜单的样式及各场景下的菜单项列表。

全局样式

json
{
  "contextMenuConfig": {
    "backgroundColor": "",
    "fontSize": 14,
    "padding": [0, 0, 0, 0],
    "iconSize": 36
  }
}
字段说明
backgroundColor菜单背景颜色(空字符串使用默认)
fontSize菜单文字大小
padding菜单内边距 [left, top, right, bottom]
iconSize图标大小

全局菜单

global 配置适用于所有模式的全局上下文菜单:

json
{
  "contextMenuConfig": {
    "global": {
      "screenshot": [
        { "key": "exit" },
        { "key": "share" }
      ]
    }
  }
}

阅读模式菜单

viewMode 配置阅读模式下的菜单:

json
{
  "contextMenuConfig": {
    "viewMode": {
      "textSelect": [
        { "key": "copy" }
      ]
    }
  }
}

注释模式菜单

annotationMode 包含以下子菜单场景:

文本选中

json
"textSelect": [
  { "key": "copy" },
  { "key": "highlight" },
  { "key": "underline" },
  { "key": "strikeout" },
  { "key": "squiggly" }
]

长按空白区域

json
"longPressContent": [
  { "key": "paste" },
  { "key": "note" },
  { "key": "textBox" },
  { "key": "stamp" },
  { "key": "image" }
]

Markup 注释(高亮/下划线/删除线/波浪线)

json
"markupContent": [
  { "key": "properties" },
  { "key": "note" },
  { "key": "reply" },
  { "key": "viewReply" },
  { "key": "delete" }
]

其他注释类型

场景配置键支持的菜单项
手写(Ink)inkContentpropertiesnotereplyviewReplydelete
形状(Shape)shapeContentpropertiesnotereplyviewReplydelete
文本框(FreeText)freeTextContentpropertieseditreplyviewReplydelete
音频(Sound)soundContentreplyviewReplyplayrecorddelete
图章(Stamp)stampContentnotereplyviewReplydeleterotate
签名图章signStampContentsignHeredeleterotate
链接(Link)linkContenteditdelete

内容编辑模式菜单

contentEditorMode 包含以下场景:

文本区域

json
"editTextAreaContent": [
  { "key": "properties" },
  { "key": "edit" },
  { "key": "cut" },
  { "key": "copy" },
  { "key": "delete" }
]

选中文本

json
"editSelectTextContent": [
  { "key": "properties" },
  { "key": "opacity", "subItems": ["25%", "50%", "75%", "100%"] },
  { "key": "cut" },
  { "key": "copy" },
  { "key": "delete" }
]

文本输入

json
"editTextContent": [
  { "key": "select" },
  { "key": "selectAll" },
  { "key": "paste" }
]

图片区域

json
"imageAreaContent": [
  { "key": "properties" },
  { "key": "rotateLeft" },
  { "key": "rotateRight" },
  { "key": "replace" },
  { "key": "export" },
  { "key": "opacity", "subItems": ["25%", "50%", "75%", "100%"] },
  { "key": "flipHorizontal" },
  { "key": "flipVertical" },
  { "key": "crop" },
  { "key": "delete" },
  { "key": "copy" },
  { "key": "cut" }
]

路径区域

json
"editPathContent": [
  { "key": "delete" }
]

长按空白区域

json
"longPressWithEditTextMode": [
  { "key": "addText" },
  { "key": "paste" },
  { "key": "keepSourceFormatingPaste" }
]
json
"longPressWithEditImageMode": [
  { "key": "addImages" },
  { "key": "paste" }
]

搜索替换

json
"searchReplace": [
  { "key": "replace" }
]

表单模式菜单

formMode 为各表单类型配置菜单:

json
{
  "contextMenuConfig": {
    "formMode": {
      "textField":      [{ "key": "properties" }, { "key": "delete" }],
      "checkBox":       [{ "key": "properties" }, { "key": "delete" }],
      "radioButton":    [{ "key": "properties" }, { "key": "delete" }],
      "listBox":        [{ "key": "options" }, { "key": "properties" }, { "key": "delete" }],
      "comboBox":       [{ "key": "options" }, { "key": "properties" }, { "key": "delete" }],
      "signatureField": [{ "key": "startToSign" }, { "key": "delete" }],
      "pushButton":     [{ "key": "options" }, { "key": "properties" }, { "key": "delete" }]
    }
  }
}

添加自定义菜单项

在任意菜单场景中添加自定义按钮:

1. 配置 JSON

json
{
  "contextMenuConfig": {
    "annotationMode": {
      "markupContent": [
        { "key": "properties" },
        { "key": "note" },
        {
          "key": "custom",
          "identifier": "custom_markup_action_get_text",
          "title": "Get Text",
          "icon": "ic_test_get_text",
          "showType": "text"
        },
        { "key": "delete" }
      ]
    }
  }
}
字段说明
key自定义按钮必须为 "custom"
identifier唯一标识符,在回调中区分不同按钮
title显示文本
icon图标资源名(需放在 res/drawable
showType显示方式:"text""icon"

2. 添加回调

java
CPDFCustomEventCallbackHelper.getInstance().addCustomEventCallback(extraMap -> {
    String customEventType = extraMap.get(
        CPDFCustomEventField.CUSTOM_EVENT_TYPE).toString();
    if (CPDFCustomEventType.CONTEXT_MENU_ITEM_TAPPED.equals(customEventType)) {
        String identifier = extraMap.get(CPDFCustomEventField.IDENTIFIER).toString();
        switch (identifier) {
            case "custom_markup_action_get_text":
                CPDFAnnotation annotation = (CPDFAnnotation) extraMap.get(
                    CPDFCustomEventField.ANNOTATION);
                if (annotation instanceof CPDFMarkupAnnotation) {
                    String text = ((CPDFMarkupAnnotation) annotation).getMarkedText();
                    CToastUtil.showToast(getApplicationContext(), text);
                }
                break;
        }
    }
});
kotlin
CPDFCustomEventCallbackHelper.getInstance().addCustomEventCallback { extraMap ->
    val customEventType = extraMap[CPDFCustomEventField.CUSTOM_EVENT_TYPE].toString()
    if (CPDFCustomEventType.CONTEXT_MENU_ITEM_TAPPED == customEventType) {
        val identifier = extraMap[CPDFCustomEventField.IDENTIFIER].toString()
        when (identifier) {
            "custom_markup_action_get_text" -> {
                val annotation = extraMap[CPDFCustomEventField.ANNOTATION]
                if (annotation is CPDFMarkupAnnotation) {
                    CToastUtil.showToast(applicationContext, annotation.markedText)
                }
            }
        }
    }
}

效果示意

自定义上下文菜单示意