PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
注释是用户添加到 PDF 上的标记对象——高亮、形状、文本框、图章、签名、密文等。本指南介绍 UI 侧的注释功能和 Core 注释 API。
注释的渲染、数据模型和 PDF 坐标属于 Core(
instance.Core.Annotations、instance.Core.annotationManager)。UI 通过工具创建注释,并在其 store 中同步选择状态和权限。
注释类位于 instance.Core.Annotations:
| 分类 | 类 |
|---|---|
| 文本标记 | TextHighlightAnnotation、TextUnderlineAnnotation、TextStrikeoutAnnotation、TextSquigglyAnnotation |
| 形状 | RectangleAnnotation、EllipseAnnotation、LineAnnotation(含箭头)、PolylineAnnotation、PolygonAnnotation(含云朵)、ArcAnnotation |
| 文本框 | FreeTextAnnotation(含标注) |
| 手绘 | FreeHandAnnotation |
| 便签 | StickyAnnotation |
| 图章与图片 | StampAnnotation |
| 链接 | Link |
| 文件与声音 | FileAttachmentAnnotation、SoundAnnotation |
| 表单 | TextWidgetAnnotation、CheckButtonWidgetAnnotation、RadioButtonWidgetAnnotation、ListWidgetAnnotation、ChoiceWidgetAnnotation、PushButtonWidgetAnnotation、SignatureWidgetAnnotation(参见表单) |
| 安全 | RedactionAnnotation |
测量注释是带有测量数据的直线、折线、多边形、矩形、椭圆和弧注释。
注释面板中有导入和导出注释的按钮。可以通过它们的 data-element 隐藏:
UI.disableElements(['annotationExportButton', 'annotationImportButton']);
UI.enableElements(['annotationExportButton', 'annotationImportButton']);instance.Core.annotationManager.setCurrentUser('alice'); // 新注释的作者
UI.setAuthor('alice'); // 设置弹窗中的“作者”字段
UI.enableFeatures([UI.Feature.LinkHighlighting]); // 高亮链接初始用户通过 ComPDFKitViewer() 的 annotationUser 选项设置。
UI.disableFeatures([UI.Feature.Annotations]);注释右键菜单可以定制——参见弹出菜单。
instance.Core.annotationManager 提供注释 API:
| 方法 | 用途 |
|---|---|
getAnnotationsList() | 列出注释。 |
addAnnotation(annotation) / addAnnotations(annotations) | 添加注释。 |
deleteAnnotation(annotation) / deleteAnnotations(annotations) | 删除注释。 |
selectAnnotation(annotation) / getSelectedAnnotations() / deselectAllAnnotations() | 选择。 |
redrawAnnotation(annotation) | 修改属性后重绘。 |
exportAnnotations() | 导出为 XFDF 字符串。 |
importAnnotations(xfdf) | 从 XFDF 字符串、File 或 Blob 导入。 |
setCurrentUser(name) / getCurrentUser() | 新注释的作者。 |
enableReadOnlyMode() / disableReadOnlyMode() | 只读模式。 |
setPermissionCheckCallback(fn) / canModify(annotation) | 权限。 |
| 状态 | 用途 |
|---|---|
| 选中的注释 | 驱动注释弹出菜单和样式面板。 |
| 工具样式 | 各注释工具的默认样式。 |
| 当前用户 / 管理员 | 同步 annotationManager.getCurrentUser() 和管理员标志,用于权限判断。 |
UI 根据 annotationSelected、annotationChanged 和 toolModeUpdated 事件更新这些状态。
isAdminUser(选项,默认 true)—— 用户可以修改所有注释。isReadOnly(选项)—— 以只读模式打开文档,无法创建或修改注释。annotationManager.setPermissionCheckCallback((author, annotation) => boolean) —— 逐个注释决定当前用户能否修改。annotationManager.canModify(annotation) 返回判断结果。UI.disableElements(['annotationExportButton']);
instance.Core.annotationManager.setCurrentUser('alice');const xfdf = await instance.Core.annotationManager.exportAnnotations();
console.log(xfdf);const annotations = instance.Core.annotationManager.getAnnotationsList();
console.log(annotations.length);