PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
使用以下回调同步注释属性面板,并监听注释创建、选中和撤销/重做状态。注释历史使用专用监听器,而不是 CPDFEvent。
| 回调或 API | 参数 | 说明 |
|---|---|---|
onAnnotationStyleDialogDismissedCallback | CPDFAnnotationType type | 注释属性面板关闭时触发。 |
controller.annotationHistoryManager. | bool canUndo, bool canRedo | 注释历史状态变化时触发;再次设置会覆盖旧回调。 |
CPDFReaderWidgetController.addEventListener() 支持为同一事件注册多个回调。添加第一个监听器时,Flutter SDK 开始接收对应的原生事件;移除最后一个监听器时停止接收。
void addEventListener(CPDFEvent event, Function(dynamic) callback);
bool removeEventListener(CPDFEvent event, Function(dynamic) callback);
void removeAllEventListeners([CPDFEvent? event]);事件索引
| 事件 | 事件数据 | 触发时机 |
|---|---|---|
CPDFEvent.annotationsCreated | CPDFAnnotation | 创建非表单注释。 |
CPDFEvent.annotationsSelected | CPDFAnnotation | 选中注释。 |
CPDFEvent.annotationsDeselected | CPDFAnnotation? | 取消选中注释。 |
CPDFEvent.pencilDrawingCompleted | Map<String, dynamic> | Pencil 绘制完成并保存(仅 iOS)。 |
CPDFEvent.pencilDrawingDiscarded | Map<String, dynamic> | 放弃 Pencil 绘制(仅 iOS)。 |
Reader 创建后,注册注释事件和注释 History Manager:
void _onReaderCreated(CPDFReaderWidgetController controller) {
controller.addEventListener(
CPDFEvent.annotationsCreated,
(annotation) => debugPrint('Created: $annotation'),
);
controller.addEventListener(
CPDFEvent.annotationsSelected,
(annotation) => debugPrint('Selected: $annotation'),
);
controller.addEventListener(
CPDFEvent.annotationsDeselected,
(annotation) => debugPrint('Deselected: $annotation'),
);
controller.annotationHistoryManager.setOnHistoryChangedListener(
(canUndo, canRedo) {
debugPrint('Annotation history: undo=$canUndo, redo=$canRedo');
},
);
}当前 CPDFEvent 只覆盖创建和选中状态,不提供注释更新、删除事件,也不提供保存失败事件。需要主动保存并判断结果时,使用对应的保存 API,而不是依赖 onSaveCallback。
Pencil 事件仅在 iOS 上触发。事件数据包含 type 和 pageIndex,可用于识别绘制来源与页面。