Custom Toolbar Items
In addition to configuring predefined buttons, ComPDFKit_Tools also supports:
- Modifying default buttons' icons and text
- Adding entirely new custom buttons and responding to click events
Note: Once you use
customToolbarLeftItems,customToolbarRightItems, orcustomMoreMenuItems, the correspondingtoolbarLeftItems,toolbarRightItems, oravailableMenusconfigurations will no longer take effect.
Modifying Default Menu Buttons
Use customMoreMenuItems to modify the icons and text of existing buttons in the More menu:
{
"toolbarConfig": {
"customMoreMenuItems": [
{
"action": "viewSettings",
"icon": "ic_test_settings",
"title": "Settings"
},
{
"action": "save",
"icon": "ic_test_save",
"title": "Save Document"
}
]
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Field | Description |
|---|---|
action | The id of the predefined action (e.g., viewSettings, save, share, etc.) |
icon | Icon resource name, must be placed in res/drawable directory |
title | Menu display text (only effective in customMoreMenuItems) |
Supported action predefined values are the same as availableMenus: viewSettings, documentEditor, documentInfo, save, watermark, security, flattened, share, openDocument, snip.
Adding Custom Buttons
JSON Configuration
Set action to "custom" and specify a unique identifier:
{
"toolbarConfig": {
"customToolbarLeftItems": [
{
"action": "custom",
"identifier": "custom_download_action",
"icon": "ic_test_download",
"title": "Download"
}
],
"customToolbarRightItems": [],
"customMoreMenuItems": []
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
| Field | Description |
|---|---|
action | Must be "custom" for custom buttons |
identifier | Unique identifier used to distinguish different custom buttons in callbacks |
icon | Icon resource name, must be placed in res/drawable directory |
title | Button text (customToolbarLeftItems / customToolbarRightItems only show icons) |
Adding Event Callbacks
Listen for custom button click events via CPDFCustomEventCallbackHelper:
CPDFDocumentFragment documentFragment = CPDFDocumentFragment.newInstance(
uri, "password", configuration);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container_view, documentFragment, "documentFragment")
.commit();
// Add callback
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(), "Saved successfully");
}, e -> { });
break;
}
}
});
// Remove callback when page is destroyed
@Override
protected void onDestroy() {
super.onDestroy();
CPDFCustomEventCallbackHelper.getInstance().removeCustomEventCallback(this);
}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
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, "Saved successfully")
}, { })
}
}
}
}
override fun onDestroy() {
super.onDestroy()
CPDFCustomEventCallbackHelper.getInstance().removeCustomEventCallback(this)
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Preview

Full Custom Example
The following example shows customizing left buttons, right buttons, and More menu simultaneously:
{
"toolbarConfig": {
"customToolbarLeftItems": [
{
"action": "custom",
"identifier": "custom_back",
"icon": "ic_custom_back",
"title": "Back"
}
],
"customToolbarRightItems": [
{
"action": "search",
"icon": "ic_custom_search",
"title": "Search"
},
{
"action": "custom",
"identifier": "custom_share",
"icon": "ic_custom_share",
"title": "Share"
}
],
"customMoreMenuItems": [
{
"action": "save",
"icon": "ic_custom_save",
"title": "Save"
},
{
"action": "custom",
"identifier": "custom_print",
"icon": "ic_custom_print",
"title": "Print"
}
]
}
}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