Skip to content
全新发布

PDF SDK 与 AI 文档处理

在 GitHub 获取完整的私有化部署SDK 包及 AI 智能文档处理能力,一键部署,快速构建您的文档处理工作流。

Guides

注释

ComPDF Flutter SDK 支持注释相关回调和监听事件,用于处理注释创建、属性编辑、点击拦截、选中状态和 iOS Pencil 绘制。

可用回调和事件

类型名称触发时机返回值
阅读器回调onAnnotationCreationPreparedCallback准备创建签名、图章、超链接、图片或 Note 注释CPDFAnnotationType annotationType, CPDFAnnotation? annotation
阅读器回调onAnnotationStyleDialogDismissedCallback注释属性弹窗关闭CPDFAnnotationType type
阅读器回调onInterceptAnnotationActionCallback开启拦截后点击已有 Note 或 Link 注释CPDFAnnotation annotation
监听事件CPDFEvent.annotationsCreated注释创建CPDFAnnotation
监听事件CPDFEvent.annotationsSelected注释被选中CPDFAnnotation
监听事件CPDFEvent.annotationsDeselected注释取消选中CPDFAnnotation?
监听事件CPDFEvent.pencilDrawingCompletediOS Pencil 绘制完成包含 typepageIndexMap<String, dynamic>
监听事件CPDFEvent.pencilDrawingDiscardediOS Pencil 绘制被放弃包含 typepageIndexMap<String, dynamic>

注释创建准备回调

当用户点击签名、图章、超链接或图片工具,或调用 controller.setAnnotationMode() 进入创建模式时,会触发 onAnnotationCreationPreparedCallback。关闭 autoShowNoteEditDialog 后,自定义 Note 创建流程也会使用该回调。

如需替换默认选择弹窗,请在 CPDFConfiguration 中关闭对应选项:

dart
CPDFConfiguration(
  annotationsConfig: const CPDFAnnotationsConfig(
    autoShowSignPicker: false,
    autoShowLinkDialog: false,
    autoShowPicPicker: false,
    autoShowStampPicker: false,
  ),
);
dart
CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onCreated: (controller) {},
  onAnnotationCreationPreparedCallback: (
    CPDFAnnotationType annotationType,
    CPDFAnnotation? annotation,
  ) {
    debugPrint('准备创建注释: $annotationType');
  },
);

插入签名和图章

dart
await controller?.prepareNextSignature(signImagePath);

await controller?.prepareNextStamp(standardStamp: stamp);
await controller?.prepareNextStamp(imagePath: imagePath);
await controller?.prepareNextStamp(
  textStamp: CPDFTextStamp(
    content: 'ComPDF-Flutter',
    date: CPDFDate.getTextStampDate(timeSwitch: false, dateSwitch: true),
    shape: CPDFTextStampShape.none,
    color: CPDFTextStampColor.white,
  ),
);

注释属性弹窗关闭回调

dart
CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onAnnotationStyleDialogDismissedCallback: (CPDFAnnotationType type) {
    debugPrint('注释属性弹窗已关闭: ${type.name}');
  },
);

注释点击拦截回调

开启 interceptNoteActioninterceptLinkAction 后,SDK 会把点击到的 Note 或 Link 注释发送到 Flutter,而不是执行默认的原生行为。你可以用它展示自定义 Note 编辑器、自行处理网页链接,或替换页面跳转逻辑。

监听 onInterceptAnnotationActionCallback 前,请先开启拦截:

dart
CPDFConfiguration(
  annotationsConfig: const CPDFAnnotationsConfig(
    interceptNoteAction: true,
    interceptLinkAction: true,
  ),
);
dart
CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onCreated: (controller) {},
  onInterceptAnnotationActionCallback: (CPDFAnnotation annotation) {
    debugPrint('拦截到注释点击: ${annotation.type}');
  },
);

注释生命周期事件

创建 CPDFReaderWidget 并确保 CPDFReaderWidgetController 可用后,再注册监听事件。

dart
late CPDFReaderWidgetController readerController;

void onAnnotationCreated(dynamic annotation) {
  debugPrint('注释已创建: $annotation');
}

void onAnnotationSelected(dynamic annotation) {
  debugPrint('注释已选中: $annotation');
}

void onAnnotationDeselected(dynamic annotation) {
  debugPrint('注释已取消选中: $annotation');
}

CPDFReaderWidget(
  document: documentPath,
  configuration: configuration,
  onCreated: (controller) {
    readerController = controller;
    readerController.addEventListener(
      CPDFEvent.annotationsCreated,
      onAnnotationCreated,
    );
    readerController.addEventListener(
      CPDFEvent.annotationsSelected,
      onAnnotationSelected,
    );
    readerController.addEventListener(
      CPDFEvent.annotationsDeselected,
      onAnnotationDeselected,
    );
  },
);

Pencil 绘制事件

返回值字段:

字段类型说明
typeString绘制类型,通常为 pencil
pageIndexint从 0 开始的页面索引
dart
void onPencilDrawingCompleted(dynamic event) {
  debugPrint('Pencil 绘制完成,页面: ${event['pageIndex']}');
}

void onPencilDrawingDiscarded(dynamic event) {
  debugPrint('Pencil 绘制已放弃,页面: ${event['pageIndex']}');
}

readerController.addEventListener(
  CPDFEvent.pencilDrawingCompleted,
  onPencilDrawingCompleted,
);
readerController.addEventListener(
  CPDFEvent.pencilDrawingDiscarded,
  onPencilDrawingDiscarded,
);

注意: Pencil 绘制事件仅支持 iOS。返回的 pageIndex 从 0 开始计数。

移除监听

移除监听时,需要传入与 addEventListener() 注册时相同的回调引用。

dart
readerController.removeEventListener(
  CPDFEvent.annotationsCreated,
  onAnnotationCreated,
);
readerController.removeAllEventListeners(CPDFEvent.annotationsCreated);
readerController.removeAllEventListeners();