Skip to content

使用配置文件自定义 UI

福昕 PDF SDK 鸿蒙版提供简洁友好的内置 UI,开发者无需耗费过多时间进行界面设计,即可快速构建功能完善的 PDF 应用。此外,SDK 还支持两种灵活的 UI 自定义方式,满足开发者个性化需求:

  1. UI Extensions 组件源代码: 提供即用型 UI 模块,支持深度定制。
  2. JSON 配置文件 (uiextensions_config.json): 高效灵活地修改内置 UI 元素。

以下将介绍如何通过配置文件自定义功能模块、权限管理和 UI 元素。配置文件允许开发者轻松选择功能模块,设置权限和修改 UI 元素属性,无需编写额外代码或重新设计界面。

JSON 配置文件详解

配置文件支持 JSON 文件格式或直接在代码中编写。推荐使用 JSON 文件,其结构清晰,易于查看和配置。

您可以参考福昕 PDF SDK 鸿蒙版包中 samples/complete_pdf_viewer/entry/src/main/resources/rawfile 目录下的 JSON 配置文件,了解其结构和配置选项。其内容如下所示:

[config.json]
json
{
  "modules": {
    "readingbookmark": true,
    "outline": true,
    "annotations": {
      "highlight": true,
      "underline": true,
      "squiggly": true,
      "strikeout": true,
      "insert": true,
      "replace": true,
      "line": true,
      "rectangle": true,
      "oval": true,
      "arrow": true,
      "pencil": true,
      "eraser": true,
      "typewriter": true,
      "textbox": true,
      "callout": true,
      "note": true,
      "stamp": true,
      "polygon": true,
      "cloud": true,
      "polyline": true,
      "measure": true,
      "image": true,
      "audio": true,
      "video": true
    },
    "thumbnail": true,
    "attachment": true,
    "signature": true,
    "search": true,
    "pagenavigation": true,
    "form": true
  },
  "permissions": {
    "runJavaScript": true,
    "enableLink": true
  },
  "uiSettings": {
    "pageMode": "Single",
    "continuous": false,
    "zoomMode": "FitWidth",
    "colorMode": "Normal",
    "mapForegroundColor": "#5d5b71",
    "mapBackgroundColor": "#00001b",
    "enableFormNavigationBar": true,
    "highlightForm": true,
    "highlightFormColor": "#200066cc",
    "highlightLink": true,
    "highlightLinkColor": "#16007fff",
    "annotations": {
      "continuouslyAdd": true,
      "highlight": {
        "color": "#ffff00",
        "opacity": 1.0
      },
      "areaHighlight": {
        "color": "#ffff00",
        "opacity": 1.0
      },
      "underline": {
        "color": "#66cc33",
        "opacity": 1.0
      },
      "squiggly": {
        "color": "#993399",
        "opacity": 1.0
      },
      "strikeout": {
        "color": "#ff0000",
        "opacity": 1.0
      },
      "insert": {
        "color": "#993399",
        "opacity": 1.0
      },
      "replace": {
        "color": "#0000ff",
        "opacity": 1.0
      },
      "line": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2
      },
      "rectangle": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "fillColor": null
      },
      "oval": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "fillColor": null
      },
      "arrow": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2
      },
      "pencil": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "stylusOnly": false
      },
      "polygon": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "fillColor": null
      },
      "cloud": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "fillColor": null
      },
      "polyline": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2
      },
      "typewriter": {
        "textColor": "#0000ff",
        "opacity": 1.0,
        "textFace": "Courier",
        "textSize": 18
      },
      "textbox": {
        "color": "#ff0000",
        "textColor": "#0000ff",
        "opacity": 1.0,
        "textFace": "Courier",
        "textSize": 18
      },
      "callout": {
        "color": "#ff0000",
        "textColor": "#0000ff",
        "opacity": 1.0,
        "textFace": "Courier",
        "textSize": 18
      },
      "note": {
        "color": "#ff0000",
        "opacity": 1.0,
        "icon": "Comment"
      },
      "attachment": {
        "color": "#ff0000",
        "opacity": 1.0,
        "icon": "PushPin"
      },
      "image": {
        "rotation": 0,
        "opacity": 1.0
      },
      "measure": {
        "color": "#ff0000",
        "opacity": 1.0,
        "thickness": 2,
        "scaleFromUnit": "inch",
        "scaleToUnit": "inch",
        "scaleFromValue": 1.0,
        "scaleToValue": 1.0
      }
    },
    "form": {
      "textField": {
        "textColor": "#000000",
        "textFace": "Courier",
        "textSize": 0
      },
      "checkBox": {
        "textColor": "#000000"
      },
      "radioButton": {
        "textColor": "#000000"
      },
      "comboBox": {
        "textColor": "#000000",
        "textFace": "Courier",
        "textSize": 0,
        "customText": false
      },
      "listBox": {
        "textColor": "#000000",
        "textFace": "Courier",
        "textSize": 0,
        "multipleSelection": false
      }
    },
    "signature": {
      "color": "#000000",
      "thickness": 8
    }
  }
}

**提示:**

  • 上述 JSON 文件中的值是配置项的默认值。如果某些配置项不在 JSON 文件中,则将使用其默认值。例如,如果您注释掉 "highlight": true,, 但高亮功能仍然是可用的。

JSON 配置项描述

JSON 配置文件包括三个部分:功能模块,权限管理和 UI 设置 (例如,UI元素属性) 。本节将详细介绍这些配置项。

功能模块配置项

功能模块配置项的值类型是 bool,其中 true 表示启用该功能模块,false 表示将禁用该功能模块。默认值为 true

功能模块描述
readingbookmark用户定义书签
outlinePDF 文档书签
annotations (highlight, underline, squiggly, strikeout, insert, replace, line, rectangle, oval, arrow, pencil, eraser, typewriter, textbox, callout, note, stamp, polygon, cloud, polyline, measure, image, audio, video)注释模块集合
thumbnailPDF页面缩略图显示和页面管理
attachmentPDF文档附件和附件注释
Signature电子签名和手写签名
search文本搜索
pagenavigationPDF 页面导航
form表单填写和表单数据导入导出

权限管理配置项

权限管理配置项的值类型为 bool,其中 true 表示将启用该权限,false 表示将禁用该权限。runJavaScriptenableLink 的默认值为 true

权限管理描述
runJavaScript是否允许执行 JavaScript
enableLink是否启用超链接

基础 UI 配置

参数类型说明
页面显示设置
pageModeString页面显示模式
可选值:Single / Facing / CoverLeft / CoverMiddle / CoverRight / Reflow
默认值:Single
备注:动态 XFA 文件不支持 Reflow 模式
continuousBool是否连续显示单页
可选值:true / false
默认值:false
备注:在 Reflow 模式下无效
zoomModeString页面缩放模式
可选值:FitWidth / FitPage
默认值:FitWidth
颜色与显示模式
colorModeString页面颜色显示模式
可选值:Normal / Night / Map
默认值:Normal
备注:Night 是特殊的 Map 模式
mapForegroundColorRGB页面前景色(仅 Map 模式生效)
默认值:#5d5b71
mapBackgroundColorRGB页面背景色(仅 Map 模式生效)
默认值:#00001b
表单与链接设置
enableFormNavigationBarBool是否启用表单导航栏
可选值:true / false
默认值:true
highlightFormBool是否高亮表单域
可选值:true / false
默认值:true
highlightFormColorARGB表单高亮颜色
默认值:#200066cc
备注:包含 alpha 通道,对动态 XFA 文件无效
highlightLinkBool是否高亮超链接
可选值:true / false
默认值:true
highlightLinkColorARGB超链接高亮颜色
默认值:#16007fff
备注:包含 alpha 通道

注释配置(annotations)

参数类型说明
通用配置
continuouslyAddBool是否连续添加某个注释
可选值:true / false
默认值:true
文本高亮类注释
highlight.colorRGB高亮颜色
默认值:#ffff00
highlight.opacitynumeric高亮透明度
可选值:[0.0-1.0]
默认值:1.0
areaHighlight.colorRGB区域高亮颜色
默认值:#ffff00
areaHighlight.opacitynumeric区域高亮透明度
可选值:[0.0-1.0]
默认值:1.0
备注:区域超出页面时使用默认配置
underline.colorRGB下划线颜色
默认值:#66cc33
underline.opacitynumeric下划线透明度
可选值:[0.0-1.0]
默认值:1.0
squiggly.colorRGB波浪线颜色
默认值:#993399
squiggly.opacitynumeric波浪线透明度
可选值:[0.0-1.0]
默认值:1.0
strikeout.colorRGB删除线颜色
默认值:#ff0000
strikeout.opacitynumeric删除线透明度
可选值:[0.0-1.0]
默认值:1.0
insert.colorRGB插入标记颜色
默认值:#993399
insert.opacitynumeric插入标记透明度
可选值:[0.0-1.0]
默认值:1.0
replace.colorRGB替换标记颜色
默认值:#0000ff
replace.opacitynumeric替换标记透明度
可选值:[0.0-1.0]
默认值:1.0
图形类注释
line.colorRGB直线颜色
默认值:#ff0000
line.opacitynumeric直线透明度
可选值:[0.0-1.0]
默认值:1.0
line.thicknessnumeric直线宽度
可选值:[1-12]
默认值:2
rectangle.colorRGB矩形边框颜色
默认值:#ff0000
rectangle.opacitynumeric矩形透明度
可选值:[0.0-1.0]
默认值:1.0
rectangle.thicknessnumeric矩形边框宽度
可选值:[1-12]
默认值:2
rectangle.fillColorRGB矩形填充色
默认值:null
oval.colorRGB椭圆形边框颜色
默认值:#ff0000
oval.opacitynumeric椭圆形透明度
可选值:[0.0-1.0]
默认值:1.0
oval.thicknessnumeric椭圆形边框宽度
可选值:[1-12]
默认值:2
oval.fillColorRGB椭圆形填充色
默认值:null
arrow.colorRGB箭头颜色
默认值:#ff0000
arrow.opacitynumeric箭头透明度
可选值:[0.0-1.0]
默认值:1.0
arrow.thicknessnumeric箭头线宽
可选值:[1-12]
默认值:2
pencil.colorRGB画笔颜色
默认值:#ff0000
pencil.opacitynumeric画笔透明度
可选值:[0.0-1.0]
默认值:1.0
pencil.thicknessnumeric画笔粗细
可选值:[1-12]
默认值:2
pencil.addHistoricalPointsBool是否添加当前帧的历史坐标点
可选值:true / false
默认值:true
polygon.colorRGB多边形边框颜色
默认值:#ff0000
polygon.opacitynumeric多边形透明度
可选值:[0.0-1.0]
默认值:1.0
polygon.thicknessnumeric多边形边框宽度
可选值:[1-12]
默认值:2
polygon.fillColorRGB多边形填充色
默认值:null
cloud.colorRGB云形边框颜色
默认值:#ff0000
cloud.opacitynumeric云形透明度
可选值:[0.0-1.0]
默认值:1.0
cloud.thicknessnumeric云形边框宽度
可选值:[1-12]
默认值:2
cloud.fillColorRGB云形填充色
默认值:null
polyline.colorRGB折线颜色
默认值:#ff0000
polyline.opacitynumeric折线透明度
可选值:[0.0-1.0]
默认值:1.0
polyline.thicknessnumeric折线宽度
可选值:[1-12]
默认值:2
文本类注释
typewriter.textColorRGB打字机文本颜色
默认值:#0000ff
typewriter.opacitynumeric打字机透明度
可选值:[0.0-1.0]
默认值:1.0
typewriter.textFaceString打字机字体
可选值:Courier / Helvetica / Times
默认值:Courier
备注:非法值时使用默认字体
typewriter.textSizeInteger打字机字号
可选值:>=1
默认值:18
textbox.colorRGB文本框边框颜色
默认值:#ff0000
textbox.textColorRGB文本框文本颜色
默认值:#0000ff
textbox.opacitynumeric文本框透明度
可选值:[0.0-1.0]
默认值:1.0
textbox.textFaceString文本框字体
可选值:Courier / Helvetica / Times
默认值:Courier
备注:非法值时使用默认字体
textbox.textSizeInteger文本框字号
可选值:>=1
默认值:18
callout.colorRGB标注边框颜色
默认值:#ff0000
callout.textColorRGB标注文本颜色
默认值:#0000ff
callout.opacitynumeric标注透明度
可选值:[0.0-1.0]
默认值:1.0
callout.textFaceString标注字体
可选值:Courier / Helvetica / Times
默认值:Courier
备注:非法值时使用默认字体
callout.textSizeInteger标注字号
可选值:>=1
默认值:18
其他注释
note.colorRGB批注颜色
默认值:#ff0000
note.opacitynumeric批注透明度
可选值:[0.0-1.0]
默认值:1.0
note.iconString批注图标类型
可选值:Comment / Key / Note / Help / NewParagraph / Paragraph / Insert
默认值:Comment
备注:非法值时使用默认图标
attachment.colorRGB附件颜色
默认值:#ff0000
attachment.opacitynumeric附件透明度
可选值:[0.0-1.0]
默认值:1.0
attachment.iconString附件图标类型
可选值:Graph / PushPin / Paperclip / Tag
默认值:PushPin
备注:非法值时使用默认图标
image.rotationnumeric图片旋转角度
可选值:0 / 90 / 180 / 270
默认值:0
image.opacitynumeric图片透明度
可选值:[0.0-1.0]
默认值:1.0
image.textSizeInteger图片文本标注字号
可选值:>=1
默认值:12
measure.colorRGB测量线颜色
默认值:#ff0000
measure.opacitynumeric测量线透明度
可选值:[0.0-1.0]
默认值:1.0
measure.thicknessnumeric测量线宽度
可选值:[1-12]
默认值:2
measure.scaleFromUnitString缩放基准单位
可选值:pt / m / cm / mm / inch / p / ft / yd
默认值:inch
备注:缩放计算的基准单位
measure.scaleToUnitString缩放目标单位
可选值:pt / m / cm / mm / inch / p / ft / yd
默认值:inch
备注:缩放计算的目标单位
measure.scaleFromValuenumeric缩放基准数值
默认值:1.0
备注:缩放计算的基准数值
measure.scaleToValuenumeric缩放目标数值
默认值:1.0
备注:缩放计算的目标数值

表单控件配置(form)

参数类型说明
form.textField 配置
form.textField.textColorRGB文本颜色
默认值:#000000
form.textField.textFaceString文本字体名称
可选值:Courier / Helvetica / Times
默认值:Courier
说明:若设置为非法值,则使用默认字体
form.textField.textSizeInteger文本字号
可选值:>=0
默认值:0
说明:0 表示自动调整字体大小
form.checkBox 配置
form.checkBox.textColorRGB复选框文本颜色
默认值:#000000
form.radioButton 配置
form.radioButton.textColorRGB单选按钮文本颜色
默认值:#000000
form.comboBox 配置
form.comboBox.textColorRGB下拉框文本颜色
默认值:#000000
form.comboBox.textFaceString下拉框文本字体名称
可选值:Courier / Helvetica / Times
默认值:Courier
说明:若设置为非法值,则使用默认字体
form.comboBox.textSizeInteger下拉框文本字号
可选值:>=0
默认值:0
说明:0 表示自动调整字体大小
form.comboBox.customTextBool下拉框是否允许自定义文本
可选值:true / false
默认值:false
说明:True 表示允许自定义文本,False 表示不允许自定义文本
form.listBox 配置
form.listBox.textColorRGB列表框文本颜色
默认值:#000000
form.listBox.textFaceString列表框文本字体名称
可选值:Courier / Helvetica / Times
默认值:Courier
说明:若设置为非法值,则使用默认字体
form.listBox.textSizeInteger列表框文本字号
可选值:>=0
默认值:0
说明:0 表示自动调整字体大小
form.listBox.multipleSelectionBool列表框是否支持多选
可选值:true / false
默认值:false
说明:True 表示支持多选,False 表示不支持多选
signature(签名)配置
signature.colorRGB签名颜色
默认值:#000000
signature.thicknessnumeric签名线条粗细
可选值:[1-12]
默认值:8

通过配置文件实例化 UIExtensionsManager 对象

快速构建中,我们介绍了如何使用 UI Extensions 组件实例化 UIExtensionsManager 对象,并默认加载所有内置 UI 框架。本节将介绍如何使用配置文件实例化 UIExtensionsManager ,以便开发者根据需求轻松自定义 UI。

前提条件:

  • 您已将名为 uiextensions_config.json 的 JSON 配置文件放置于 PDFReader/entry/src/main/resources/rawfile 目录下。

操作步骤:

  • PDFReader/entry/src/main/ets/pages/Index.ets 文件中,添加以下代码:
ts
import { UIExtensionsManager, UIExtensionsComponent, ConfigModel } from 'uiextensions';

// ...

// @State uiextensionsManager: UIExtensionsManager = new UIExtensionsManager(getContext(this));
@State
uiextensionsManager: UIExtensionsManager = new UIExtensionsManager(
    getContext(this),
    new ConfigModel(getContext(this), 'uiextensions_config.json')
);

通过配置文件自定义 UI 示例

以下将演示如何通过修改配置文件,在项目中自定义功能模块、权限管理和 UI 设置(例如,UI 元素属性)。

为便于理解,我们将以 samples 文件夹下的 complete_pdf_viewer 示例为例进行演示。

打开 complete_pdf_viewer 示例步骤:

  1. 在 DevEco Studio 中打开 complete_pdf_viewer 示例工程。
  2. samples/complete_pdf_viewer/entry/src/main/resources/rawfile 目录下找到配置文件 uiextensions_config.json

示例 1:禁用 searchpagenavigation 功能模块

在 JSON 配置文件中,将 searchpagenavigation 的值设置为 false,如下所示:

json
"search": false,
"pagenavigation": false

重新编译并运行示例,对比效果如下:

sample_1.pngsample_2.png

示例2: 禁止执行 JavaScript

在 JSON 配置文件中,将 permissions 对象的 runJavaScript 属性设置为 false,如下所示:

json
{
  "permissions": {
    "runJavaScript": false
  }
}

重新编译并运行示例,此时点击包含 JavaScript 脚本的按钮将无任何响应。

示例3: 将高亮颜色从黄色设置为红色。

在 JSON 配置文件中,将 highlight 对象的 color 属性设置为 #ff0000(红色),如下所示:

json
{
  "highlight": {
    "color": "#ff0000",
    "opacity": 1.0
  }
}

重新编译并运行示例,对比效果如下: sample_3.pngsample_4.png