PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
内容编辑使用 CPDFEvent 通知选区变化,并通过内容编辑 History Manager 通知撤销/重做状态。
| 事件 | 事件数据 | 触发时机 |
|---|---|---|
CPDFEvent.EDITOR_SELECTION_SELECTED | CPDFEditArea | 选中可编辑的文本、图片或路径区域。 |
CPDFEvent.EDITOR_SELECTION_DESELECTED | CPDFEditArea | null | 清除内容编辑选区。 |
| 场景 | 注册 API | 回调参数 | 替换规则 |
|---|---|---|---|
| 内容编辑撤销/重做状态 | reader._editManager.historyManager. | pageIndex: number, canUndo: boolean, canRedo: boolean | 再次设置会覆盖旧回调。 |
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 的运行时类型可以是 CPDFEditTextArea、CPDFEditImageArea 或 CPDFEditArea,路径类型通过 type === CPDFEditType.PATH 判断。Android 通常在取消选择时发送 null,iOS 可能发送原编辑区域。RN 没有专用的路径区域类。
每个 History Manager 只保存一个回调,并且不会主动发送初始值。初始化控件时,先调用 canUndo() 和 canRedo()。在 iOS 上,切换页面后也可能触发内容编辑 History Listener,用于报告新页面的撤销/重做状态。
当前 TypeScript 类公开 _annotationsHistoryManager 和 _editManager,但下划线命名表示其稳定性低于正式公共 getter。SDK 后续提供公共入口时,应迁移到新 API。
撤销和重做接口参见撤销与重做。