Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Android Guides Custom toolbar items

Custom Toolbar Items ​

In addition to configuring predefined buttons, ComPDFKit_Tools also supports:

  1. Modifying default buttons' icons and text
  2. Adding entirely new custom buttons and responding to click events

Note: Once you use customToolbarLeftItems, customToolbarRightItems, or customMoreMenuItems, the corresponding toolbarLeftItems, toolbarRightItems, or availableMenus configurations will no longer take effect.

Modifying Default Menu Buttons ​

Use customMoreMenuItems to modify the icons and text of existing buttons in the More menu:

json
{
  "toolbarConfig": {
    "customMoreMenuItems": [
      {
        "action": "viewSettings",
        "icon": "ic_test_settings",
        "title": "Settings"
      },
      {
        "action": "save",
        "icon": "ic_test_save",
        "title": "Save Document"
      }
    ]
  }
}
FieldDescription
actionThe id of the predefined action (e.g., viewSettings, save, share, etc.)
iconIcon resource name, must be placed in res/drawable directory
titleMenu 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:

json
{
  "toolbarConfig": {
    "customToolbarLeftItems": [
      {
        "action": "custom",
        "identifier": "custom_download_action",
        "icon": "ic_test_download",
        "title": "Download"
      }
    ],
    "customToolbarRightItems": [],
    "customMoreMenuItems": []
  }
}
FieldDescription
actionMust be "custom" for custom buttons
identifierUnique identifier used to distinguish different custom buttons in callbacks
iconIcon resource name, must be placed in res/drawable directory
titleButton text (customToolbarLeftItems / customToolbarRightItems only show icons)

Adding Event Callbacks ​

Listen for custom button click events via CPDFCustomEventCallbackHelper:

java
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);
}
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, "Saved successfully")
                }, { })
            }
        }
    }
}

override fun onDestroy() {
    super.onDestroy()
    CPDFCustomEventCallbackHelper.getInstance().removeCustomEventCallback(this)
}

Preview ​

Custom toolbar items preview

Full Custom Example ​

The following example shows customizing left buttons, right buttons, and More menu simultaneously:

json
{
  "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"
      }
    ]
  }
}