Guides
主工具栏
ComPDF 的主工具栏设计灵活且高度可配置。本指南将展示如何定义主工具栏选项以及添加完全自定义的菜单选项。
本章节涵盖以下内容:
- 配置已有的工具栏按钮项
- 添加更多菜单选项
- 自定义菜单选项
默认工具栏
默认工具栏包含以下工具。
| Android | iOS |
|---|---|
![]() | ![]() |
自定义工具栏按钮
您可以在显示 PDF 时使用 toolbarLeftItems、toolbarRightItems 属性自定义主工具栏按钮。以下示例展示了如何在 iOS 中隐藏导航栏(主工具栏)中的菜单按钮,以及如何自定义 Android 工具栏菜单项:
tsx
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
onIOSClickBackPressed={handleBack}
configuration={ComPDFKit.getDefaultConfig({
toolbarConfig: {
toolbarLeftItems: Platform.OS === "android" ? [
'back', 'thumbnail'
] : [
'back'
],
toolbarRightItems: Platform.OS === "android" ? [
'search',
'bota'
] : [
'search',
'thumbnail',
'bota',
'menu'
]
}
})}
/>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
说明: 不同平台的工具栏默认交互存在差异,示例中根据平台分别配置左右工具栏按钮。
自定义工具栏将如下所示。
| Android | iOS |
|---|---|
![]() | ![]() |
可用的工具栏选项类型
| 工具栏按钮项 | 描述 |
|---|---|
| back | 显示关闭按钮项。 |
| thumbnail | 显示缩略图按钮项。 |
| search | 显示搜索按钮项。 |
| bota | 显示大纲、书签、注释列表按钮项。 |
| menu | 显示菜单按钮项。 |
| viewSettings | 打开设置视图并设置滚动方向、显示模式、主题颜色等相关设置。 |
| documentEditor | 打开文档缩略图列表,可以在视图中删除、旋转和添加文档页面。 |
| documentInfo | 打开文档信息视图以显示基本文档信息和权限信息。 |
| watermark | 打开水印编辑视图以添加文本和图像水印,并将其保存为新文档。 |
| security | 打开安全设置视图,设置文档打开密码和权限密码。 |
| flattened | 将文档中的注释扁平化,注释将不可编辑。 |
| save | 保存 PDF 文档。 |
| share | 打开系统共享功能。 |
| openDocument | 打开系统文件选择器并打开新 PDF 文档。 |
| custom | 自定义菜单选项 |
注意:请参考
CPDFToolbarAction获取相关选项。
更多菜单
通过在主工具栏中配置 CPDFToolbarAction.menu,可以展示更多菜单选项。点击后将弹出菜单,供用户选择更多功能。更多菜单使用 availableMenus 进行配置,配置方式与上述工具栏按钮相同。
以下是配置示例:
tsx
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
onIOSClickBackPressed={handleBack}
configuration={ComPDFKit.getDefaultConfig({
toolbarConfig: {
availableMenus: [
CPDFToolbarAction.VIEW_SETTINGS,
CPDFToolbarAction.DOCUMENT_EDITOR,
CPDFToolbarAction.DOCUMENT_INFO,
CPDFToolbarAction.WATERMARK,
CPDFToolbarAction.SECURITY,
CPDFToolbarAction.FLATTENED,
CPDFToolbarAction.SAVE,
CPDFToolbarAction.SHARE,
CPDFToolbarAction.OPEN_DOCUMENT,
CPDFToolbarAction.SNIP,
]
}
})}
/>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
自定义菜单
在 2.6.0 及更高版本中,您可以通过 customToolbarLeftItems、customToolbarRightItems、customMoreMenuItems 属性添加自定义菜单选项。
SDK 提供两种自定义方式:
- 修改已有菜单选项的图标和标题
- 添加完全自定义的菜单选项
以下是配置示例:
tsx
const customToolbarConfig: CPDFToolbarConfig = {
customToolbarLeftItems: [
{
action: "back"
},
{
action: "custom", // 自定义按钮,必须为 custom
identifier: "custom_editor_action", // 自定义标识符,必须配置
icon: "ic_test_edit", // 自定义图标
},
],
customToolbarRightItems: [
{
action: "bota",
icon: "ic_test_book",
},
{
action: "custom",
identifier: "custom_text_search_action",
icon: "ic_test_search",
},
{
action: "menu",
icon: "ic_test_more",
},
],
customMoreMenuItems: [
{
action: "viewSettings"
},
{
action: "custom", // 自定义按钮,必须为 custom
title: "Custom Share", // 更多菜单中选项名称
icon: "ic_test_share", // 更多菜单中的图标
identifier: "custom_share", // 自定义菜单的标识符,必须配置
}
],
};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
参数说明
| 参数 | 说明 |
|---|---|
| action | 必选参数,指定要自定义的菜单选项,参考 CPDFToolbarAction。 |
| identifier | 必选参数(仅自定义菜单),自定义菜单选项的唯一标识符。 |
| icon | 可选参数,指定菜单选项的图标。支持本地资源图片名称或文件路径。 |
| title | 可选参数,指定菜单选项的标题文本。仅在更多菜单中生效。 |
自定义菜单点击事件
仅当使用 action = custom 自定义菜单选项时,您需要通过 onCustomToolbarItemTapped 回调处理点击事件:
tsx
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
configuration={ComPDFKit.getDefaultConfig({
toolbarConfig: customToolbarConfig,
})}
onIOSClickBackPressed={handleBack}
onCustomToolbarItemTapped={(identifier) =>{
if (identifier === 'custom_text_search_action') {
console.log('Custom Search Action Tapped');
// 处理自定义搜索操作
} else if (identifier === 'custom_editor_action') {
console.log('Custom Editor Action Tapped');
// 处理自定义编辑操作
} else if (identifier === 'custom_share') {
console.log('Custom Share Action Tapped');
// 处理自定义分享操作
}
}}
/>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
添加自定义图标资源
要自定义菜单图标,您需要在 Android 和 iOS 平台中分别添加图标资源:
- Android:将图标文件(如
ic_test_search.png)放入android/app/src/main/res/drawable目录。 - iOS:将图标文件(如
ic_test_search.png)添加到ios/Runner/Assets.xcassets资源集。



