Guides
自定义菜单按钮
除了配置预定义按钮外,ComPDFKit_Tools 还支持:
- 修改默认按钮的图标与文本
- 添加全新的自定义按钮并响应点击事件
注意:一旦使用了
customToolbarLeftItems、customToolbarRightItems或customMoreMenuItems自定义配置,对应位置的toolbarLeftItems、toolbarRightItems、availableMenus将不再生效。
修改默认菜单按钮
通过 customMoreMenuItems 可修改 More 菜单中已有按钮的图标和文本:
json
{
"toolbarConfig": {
"customMoreMenuItems": [
{
"action": "viewSettings",
"icon": "ic_test_settings",
"title": "Settings"
},
{
"action": "save",
"icon": "ic_test_save",
"title": "保存文档"
}
]
}
}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
| 字段 | 说明 |
|---|---|
action | 对应预定义操作的 id(如 viewSettings、save、share 等) |
icon | 图标资源名称,需放置在 res/drawable 目录中 |
title | 菜单显示文本(仅在 customMoreMenuItems 中生效) |
action 支持的预定义值与 availableMenus 相同:viewSettings、documentEditor、documentInfo、save、watermark、security、flattened、share、openDocument、snip。
添加自定义按钮
配置 JSON
将 action 设为 "custom",并指定唯一的 identifier:
json
{
"toolbarConfig": {
"customToolbarLeftItems": [
{
"action": "custom",
"identifier": "custom_download_action",
"icon": "ic_test_download",
"title": "Download"
}
],
"customToolbarRightItems": [],
"customMoreMenuItems": []
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
| 字段 | 说明 |
|---|---|
action | 自定义按钮必须为 "custom" |
identifier | 唯一标识符,用于在回调中区分不同的自定义按钮 |
icon | 图标资源名称,需放置在 res/drawable 目录中 |
title | 按钮文本(customToolbarLeftItems / customToolbarRightItems 中仅显示图标) |
添加事件回调
通过 CPDFCustomEventCallbackHelper 监听自定义按钮的点击事件:
java
CPDFDocumentFragment documentFragment = CPDFDocumentFragment.newInstance(
uri, "password", configuration);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container_view, documentFragment, "documentFragment")
.commit();
// 添加回调
CPDFCustomEventCallbackHelper.getInstance().addCustomEventCallback(extraMap -> {
String customEventType = extraMap.get(
CPDFCustomEventField.CUSTOM_EVENT_TYPE).toString();
if (CPDFCustomEventType.TOOLBAR_ITEM_TAPPED.equals(customEventType)) {
String action = extraMap.get("identifier").toString();
switch (action) {
case "custom_download_action":
CPDFViewCtrl pdfView = documentFragment.pdfView;
pdfView.savePDF(false, true, (filePath, pdfUri) -> {
CToastUtil.showToast(getApplicationContext(), "保存成功");
}, e -> { });
break;
}
}
});
// 页面销毁时移除回调
@Override
protected void onDestroy() {
super.onDestroy();
CPDFCustomEventCallbackHelper.getInstance().removeCustomEventCallback(this);
}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
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
kotlin
val documentFragment = CPDFDocumentFragment.newInstance(
uri, "password", configuration)
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container_view, documentFragment, "documentFragment")
.commit()
CPDFCustomEventCallbackHelper.getInstance().addCustomEventCallback { extraMap ->
val customEventType = extraMap[CPDFCustomEventField.CUSTOM_EVENT_TYPE].toString()
if (CPDFCustomEventType.TOOLBAR_ITEM_TAPPED == customEventType) {
val action = extraMap["identifier"].toString()
when (action) {
"custom_download_action" -> {
documentFragment.pdfView.savePDF(false, true, { filePath, pdfUri ->
CToastUtil.showToast(applicationContext, "保存成功")
}, { })
}
}
}
}
override fun onDestroy() {
super.onDestroy()
CPDFCustomEventCallbackHelper.getInstance().removeCustomEventCallback(this)
}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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
效果示意

完整自定义示例
以下示例展示了同时自定义左侧按钮、右侧按钮和 More 菜单:
json
{
"toolbarConfig": {
"customToolbarLeftItems": [
{
"action": "custom",
"identifier": "custom_back",
"icon": "ic_custom_back",
"title": "返回"
}
],
"customToolbarRightItems": [
{
"action": "search",
"icon": "ic_custom_search",
"title": "搜索"
},
{
"action": "custom",
"identifier": "custom_share",
"icon": "ic_custom_share",
"title": "分享"
}
],
"customMoreMenuItems": [
{
"action": "save",
"icon": "ic_custom_save",
"title": "保存"
},
{
"action": "custom",
"identifier": "custom_print",
"icon": "ic_custom_print",
"title": "打印"
}
]
}
}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
37
38
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
37
38