Core API 参考

⚠️ 公开 API 范围 InkLayer 只承诺对以下两个组件提供稳定的公开 API:

  • PdfAnnotator(React / Vue)
  • PdfViewer(React / Vue)

所有 Hook、Composable、Store、Adapter、内部工具函数均属于内部实现细节,不保证跨版本兼容,不推荐直接使用

本篇文档覆盖 PdfAnnotatorPdfViewer 的完整 Props 参考,以及 Annotation 数据模型(持久化存储的唯一格式)。

顶层导出

// React — inklayer-react
export { PdfAnnotator, PdfViewer } from 'inklayer-react'
export type { PdfAnnotatorProps, PdfViewerProps, Annotation, User } from 'inklayer-react'

// Vue — inklayer-vue
export { PdfAnnotator, PdfViewer } from 'inklayer-vue'
export type { PdfAnnotatorProps, PdfViewerProps, Annotation, User } from 'inklayer-vue'

注意inklayer-vue 额外导出了 PdfViewerProvider,这是 Vue 组合式 API 的实现细节,一般无需直接使用。

PdfAnnotator Props

Prop类型默认值说明
urlstring | ArrayBuffer | Uint8Array必填PDF 文档地址或数据
userUser必填当前用户信息
initialAnnotationsAnnotation[][]初始批注数据
layoutStyleReact.CSSProperties / Vue.StyleValueReact: undefined
Vue: { width: '100vw', height: '100vh' }
容器样式
defaultOptionsDefaultOptions见下文默认工具选项
themeThemeConfig见下文主题配置
enableRangebooleanfalse是否启用框选多选
defaultShowAnnotationsSidebarbooleanfalse是否默认显示批注侧边栏
titlestringReact: undefined
Vue: 'PDF ANNOTATOR'
页面标题
data (React only)stringundefined等同于 url,优先级更低
onSave(annotations: Annotation[]) => void保存回调
onDocumentLoaded (PdfViewer)(pdfDocument: PDFDocumentProxy) => voidPDF 文档加载完成
onEventBusReady (PdfViewer)(eventBus: EventBus) => voidEventBus 就绪
onAnnotationChanged(annotations: Annotation[], type: 'add' | 'update' | 'delete') => void批注变更回调
onPageChanged(pageIndex: number) => void页面切换回调
onZoomChanged(scale: number) => void缩放变更回调
onError(error: Error) => void错误回调
actionsReact.ReactNode | ((props: ActionsProps) => React.ReactNode) / Vue: #actions自定义操作区
toolbarReact.ReactNode | ((props: ToolbarProps) => React.ReactNode) / Vue: #toolbar自定义工具栏
sidebarReact.ReactNode | ((props: SidebarProps) => React.ReactNode) / Vue: #sidebar-search-sidebar自定义侧边栏

DefaultOptions

interface DefaultOptions {
  highlight?: {
    strokeColor?: string    // 默认高亮颜色
    opacity?: number        // 默认透明度
  }
  freetext?: {
    fontSize?: number       // 默认字体大小
    fontFamily?: string     // 默认字体
    strokeColor?: string    // 默认文字颜色
  }
  rectangle?: {
    strokeColor?: string    // 默认边框颜色
    fillColor?: string      // 默认填充颜色
    strokeWidth?: number    // 默认边框宽度
  }
  circle?: {
    strokeColor?: string
    fillColor?: string
    strokeWidth?: number
  }
  signature?: {
    type?: 'Draw' | 'Enter' | 'Upload'  // 默认签名方式
    strokeColor?: string
    fontSize?: number
    fontFamily?: string
    maxSize?: number        // 最大文件大小(字节)
    defaultSignature?: string  // 默认签名内容
  }
  stamp?: {
    stamps?: Array<{ id: string; url: string; name: string }>  // 预设印章列表
  }
}

ThemeConfig

interface ThemeConfig {
  token?: {
    colorPrimary?: string       // 主色
    colorSuccess?: string      // 成功色
    colorWarning?: string      // 警告色
    colorError?: string        // 错误色
    fontSize?: number          // 基础字号
    borderRadius?: number     // 圆角
  }
  components?: {
    Button?: object
    Input?: object
    // 支持大部分 antd / varlet 组件的 token 覆盖
  }
}

PdfViewer Props

PdfViewer 是只读 PDF 查看器,不支持批注编辑,其余 Props 与 PdfAnnotator 一致(不含批注相关回调)。

Prop类型默认值说明
urlstring | ArrayBuffer | Uint8Array必填PDF 文档地址或数据
userUser用户信息(只读场景可选)
initialPagenumber0初始页码
initialScalenumber | 'auto''auto'初始缩放,'auto' = 自适应宽度
layoutStyleReact.CSSProperties / Vue.StyleValue同 PdfAnnotator容器样式
onDocumentLoaded(pdfDocument: PDFDocumentProxy) => voidPDF 文档加载完成
onEventBusReady(eventBus: EventBus) => voidEventBus 就绪
onPageChanged(pageIndex: number) => void页面切换回调
onZoomChanged(scale: number) => void缩放变更回调
onError(error: Error) => void错误回调

ActionsProps 接口

actions 以函数形式传入时,接收以下参数:

interface ActionsProps {
  save: () => void                              // 触发保存
  getAnnotations: () => Annotation[]             // 获取当前批注
  exportToExcel: () => void                     // 导出为 Excel(仅 Vue)
  exportToPdf: () => void                       // 导出为 PDF
}

PdfViewerContextValue 接口(React)

通过 PdfViewerContext 获取:

interface PdfViewerContextValue {
  pageIndex: number              // 当前页码
  scale: number                  // 当前缩放
  pdfDocument: PDFDocumentProxy | null  // PDF 文档实例
  goToPage: (pageIndex: number) => void
  zoomTo: (scale: number | 'auto') => void
  getAnnotations: () => Annotation[]
  save: () => void
}

SidebarPanel 类型(React)

type SidebarPanel = 'search' | 'annotations' | 'outline' | 'thumbnails'

Annotation 数据模型

详见 批注系统。核心要点:

interface Annotation {
  id: string                  // 全局唯一 UUID
  kind: AnnotationKind        // 语义分类
  target: AnnotationTarget   // 锚点信息(含 geometry + pageIndex + coordinateSystem)
  payload?: AnnotationPayload // 语义内容
  appearance?: AnnotationAppearance  // 外观属性
  relations?: AnnotationRelations     // 关系
  meta?: AnnotationMeta             // 元数据
}

持久化建议onSave 回调直接给出完整的 Annotation[],你可以直接将此数组发送到自己的后端。无需使用任何内部存储工具函数。