Skip to content
DemoAPI 参考文档FAQ

PDF 生成模板编辑器

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

查看 GitHub
Guides

内容编辑事件与历史状态

内容编辑使用 CPDFEvent 通知选区变化,并通过内容编辑 History Manager 通知撤销/重做状态。

选区事件

事件事件数据触发时机
CPDFEvent.EDITOR_SELECTION_SELECTEDCPDFEditArea选中可编辑的文本、图片或路径区域。
CPDFEvent.EDITOR_SELECTION_DESELECTEDCPDFEditArea | null清除内容编辑选区。

历史状态

场景注册 API回调参数替换规则
内容编辑撤销/重做状态reader._editManager.historyManager.
setOnHistoryStateChangedListener()
pageIndex: number, canUndo: boolean, canRedo: boolean再次设置会覆盖旧回调。
tsx
const reader = pdfReaderRef.current;
if (reader) {
  const editorHistory = reader._editManager.historyManager;
  const initialCanUndo = await editorHistory.canUndo();
  const initialCanRedo = await editorHistory.canRedo();
  console.log("Initial editor history:", initialCanUndo, initialCanRedo);

  editorHistory.setOnHistoryStateChangedListener(
    (pageIndex, canUndo, canRedo) => {
      console.log("Editor history:", pageIndex, canUndo, canRedo);
    }
  );
}

EDITOR_SELECTION_SELECTED 的运行时类型可以是 CPDFEditTextAreaCPDFEditImageAreaCPDFEditArea,路径类型通过 type === CPDFEditType.PATH 判断。Android 通常在取消选择时发送 null,iOS 可能发送原编辑区域。RN 没有专用的路径区域类。

每个 History Manager 只保存一个回调,并且不会主动发送初始值。初始化控件时,先调用 canUndo()canRedo()。在 iOS 上,切换页面后也可能触发内容编辑 History Listener,用于报告新页面的撤销/重做状态。

当前 TypeScript 类公开 _annotationsHistoryManager_editManager,但下划线命名表示其稳定性低于正式公共 getter。SDK 后续提供公共入口时,应迁移到新 API。

撤销和重做接口参见撤销与重做