Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub
Guides

注释

使用以下回调同步注释属性面板,并监听注释创建、选中和撤销/重做状态。注释历史使用专用监听器,而不是 CPDFEvent

回调或 API参数说明
onAnnotationStyleDialogDismissedCallbackCPDFAnnotationType type注释属性面板关闭时触发。
controller.annotationHistoryManager.
setOnHistoryChangedListener()
bool canUndo, bool canRedo注释历史状态变化时触发;再次设置会覆盖旧回调。

CPDFReaderWidgetController.addEventListener() 支持为同一事件注册多个回调。添加第一个监听器时,Flutter SDK 开始接收对应的原生事件;移除最后一个监听器时停止接收。

dart
void addEventListener(CPDFEvent event, Function(dynamic) callback);

bool removeEventListener(CPDFEvent event, Function(dynamic) callback);

void removeAllEventListeners([CPDFEvent? event]);

事件索引

事件事件数据触发时机
CPDFEvent.annotationsCreatedCPDFAnnotation创建非表单注释。
CPDFEvent.annotationsSelectedCPDFAnnotation选中注释。
CPDFEvent.annotationsDeselectedCPDFAnnotation?取消选中注释。
CPDFEvent.pencilDrawingCompletedMap<String, dynamic>Pencil 绘制完成并保存(仅 iOS)。
CPDFEvent.pencilDrawingDiscardedMap<String, dynamic>放弃 Pencil 绘制(仅 iOS)。

Reader 创建后,注册注释事件和注释 History Manager:

dart
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 上触发。事件数据包含 typepageIndex,可用于识别绘制来源与页面。