注释回复
ComPDF React Native SDK 提供注释回复接口,用于为 PDF 注释添加、读取、更新和删除回复。你也可以为注释或注释回复设置标记状态和审阅状态。这些接口适用于文档审阅、协作批注、反馈跟踪和审批流程等场景。
API 概览
注释回复接口位于 CPDFDocument:
addAnnotationReply(
annotation: CPDFAnnotation,
options: { content: string; title?: string }
): Promise<CPDFReplyAnnotation | null>;
getAnnotationReplies(
annotation: CPDFAnnotation
): Promise<CPDFReplyAnnotation[]>;
updateAnnotationReply(
reply: CPDFReplyAnnotation,
options: { content: string; title?: string }
): Promise<boolean>;
removeAnnotationReply(reply: CPDFReplyAnnotation): Promise<boolean>;
removeAllAnnotationReplies(annotation: CPDFAnnotation): Promise<boolean>;
setAnnotationMarkState(
annotation: CPDFAnnotation,
state: CPDFAnnotationMarkState
): Promise<boolean>;
getAnnotationMarkState(
annotation: CPDFAnnotation
): Promise<CPDFAnnotationMarkState>;
setAnnotationReviewState(
annotation: CPDFAnnotation,
state: CPDFAnnotationReviewState
): Promise<boolean>;
getAnnotationReviewState(
annotation: CPDFAnnotation
): Promise<CPDFAnnotationReviewState>;接口说明:
| API | 说明 |
|---|---|
addAnnotationReply | 为注释添加一条普通回复 |
getAnnotationReplies | 获取注释下的全部普通回复 |
updateAnnotationReply | 更新普通回复的内容和可选标题 |
removeAnnotationReply | 删除单条普通回复 |
removeAllAnnotationReplies | 删除某个注释下的全部普通回复 |
setAnnotationMarkState | 设置注释或回复的标记状态 |
getAnnotationMarkState | 获取注释或回复的标记状态 |
setAnnotationReviewState | 设置注释或回复的审阅状态 |
getAnnotationReviewState | 获取注释或回复的审阅状态 |
回复模型
注释回复使用 CPDFReplyAnnotation 表示。
class CPDFReplyAnnotation extends CPDFAnnotation {
modifyDate: Date | null;
}由于 CPDFReplyAnnotation 继承自 CPDFAnnotation,它也包含审阅流程中常用的注释字段:
| 属性 | 说明 |
|---|---|
page | 页面索引,从 0 开始 |
title | 回复作者或标题 |
content | 回复内容 |
createDate | 回复创建时间,如果原生 SDK 提供 |
modifyDate | 回复修改时间,如果原生 SDK 提供 |
markState | 当前标记状态 |
reviewState | 当前审阅状态 |
调用更新、删除或状态接口时,请使用 addAnnotationReply 或 getAnnotationReplies 返回的回复对象。不要手动构造 CPDFReplyAnnotation 对象后传入这些接口。
标记状态和审阅状态
标记状态:
enum CPDFAnnotationMarkState {
MARKED = "marked",
UNMARKED = "unmarked",
}审阅状态:
enum CPDFAnnotationReviewState {
ACCEPTED = "accepted",
REJECTED = "rejected",
CANCELLED = "cancelled",
COMPLETED = "completed",
NONE = "none",
ERROR = "error",
}标记状态和审阅状态既可以设置到父注释,也可以设置到回复注释:
await document.setAnnotationMarkState(
annotation,
CPDFAnnotationMarkState.MARKED
);
await document.setAnnotationReviewState(
annotation,
CPDFAnnotationReviewState.ACCEPTED
);获取注释对象
大多数回复操作都从一个已有注释开始。下面示例获取第 1 页的第一条注释。
const page = document.pageAtIndex(0);
const annotations = await page.getAnnotations();
if (!annotations.length) {
return;
}
const annotation = annotations[0];页面索引从 0 开始。
添加回复
使用 addAnnotationReply 为注释添加一条普通回复。
const reply = await document.addAnnotationReply(annotation, {
content: "Please review this highlight.",
title: "Alex",
});
if (!reply) {
throw new Error("Add annotation reply failed.");
}
console.log("Reply content:", reply.content);content 为必填参数。title 为可选参数,通常用于表示回复作者名称。
读取回复列表
使用 getAnnotationReplies 获取注释下的全部普通回复。
const replies = await document.getAnnotationReplies(annotation);
for (const reply of replies) {
console.log("Author:", reply.title);
console.log("Content:", reply.content);
console.log("Mark state:", reply.markState);
console.log("Review state:", reply.reviewState);
}返回列表只包含普通回复。内部用于保存标记状态和审阅状态的状态回复不会作为单独回复项暴露。
更新回复
使用 updateAnnotationReply 更新已有回复的内容和可选标题。
const replies = await document.getAnnotationReplies(annotation);
if (replies.length) {
const success = await document.updateAnnotationReply(replies[0], {
content: "Updated reply content.",
title: "Alex",
});
if (!success) {
throw new Error("Update annotation reply failed.");
}
}reply 参数应来自 getAnnotationReplies 或 addAnnotationReply 的返回值。
删除单条回复
使用 removeAnnotationReply 删除一条普通回复。
const replies = await document.getAnnotationReplies(annotation);
if (replies.length) {
const success = await document.removeAnnotationReply(replies[0]);
if (!success) {
throw new Error("Remove annotation reply failed.");
}
}删除全部回复
使用 removeAllAnnotationReplies 删除某个注释下的全部普通回复。
const success = await document.removeAllAnnotationReplies(annotation);
if (!success) {
throw new Error("Remove all annotation replies failed.");
}该接口只删除普通回复。标记状态和审阅状态信息会被保留。
设置回复的标记状态和审阅状态
你可以为某条回复设置标记状态或审阅状态。
const replies = await document.getAnnotationReplies(annotation);
if (replies.length) {
const reply = replies[0];
await document.setAnnotationMarkState(
reply,
CPDFAnnotationMarkState.MARKED
);
await document.setAnnotationReviewState(
reply,
CPDFAnnotationReviewState.COMPLETED
);
}设置回复状态后,如果界面需要展示最新数据,请再次调用 getAnnotationReplies 读取回复列表。
设置父注释状态
你也可以直接设置父注释本身的标记状态和审阅状态。
await document.setAnnotationMarkState(
annotation,
CPDFAnnotationMarkState.MARKED
);
await document.setAnnotationReviewState(
annotation,
CPDFAnnotationReviewState.ACCEPTED
);
const markState = await document.getAnnotationMarkState(annotation);
const reviewState = await document.getAnnotationReviewState(annotation);
console.log("Mark:", markState);
console.log("Review:", reviewState);在 CPDFReaderView 中使用
在阅读器页面中,可以通过 pdfReaderRef.current?._pdfDocument 调用相同接口。
import {
CPDFReaderView,
CPDFAnnotationReviewState,
} from "@compdfkit_pdf_sdk/react_native";
async function addReplyToFirstAnnotation(reader: CPDFReaderView) {
const document = reader._pdfDocument;
const pageIndex = await reader.getCurrentPageIndex();
const annotations = await document.pageAtIndex(pageIndex).getAnnotations();
if (!annotations.length) {
return;
}
const reply = await document.addAnnotationReply(annotations[0], {
content: "Reviewed in the reader.",
title: "Reviewer",
});
if (reply) {
await document.setAnnotationReviewState(
reply,
CPDFAnnotationReviewState.COMPLETED
);
}
}这个方式适用于自定义审阅面板、注释列表和协作工具栏等场景。
完整示例
下面示例演示如何在阅读器中获取第 1 页第一条注释,为它添加回复,设置该回复的审阅状态,并重新读取回复列表。
import {
CPDFReaderView,
CPDFAnnotation,
CPDFReplyAnnotation,
CPDFAnnotationReviewState,
} from "@compdfkit_pdf_sdk/react_native";
async function reviewFirstAnnotation(reader: CPDFReaderView) {
const document = reader._pdfDocument;
const page = document.pageAtIndex(0);
const annotations: CPDFAnnotation[] = await page.getAnnotations();
if (!annotations.length) {
return;
}
const annotation = annotations[0];
const reply: CPDFReplyAnnotation | null =
await document.addAnnotationReply(annotation, {
content: "Please verify this annotation.",
title: "Reviewer",
});
if (!reply) {
throw new Error("Add annotation reply failed.");
}
await document.setAnnotationReviewState(
reply,
CPDFAnnotationReviewState.COMPLETED
);
const replies = await document.getAnnotationReplies(annotation);
console.log("Reply count:", replies.length);
}注意事项
- 更新或删除回复时,请使用 SDK 返回的
CPDFReplyAnnotation对象。 - 不建议在业务逻辑中依赖内部注释标识。需要操作注释或回复时,请直接传入 SDK 返回的注释对象或回复对象。
getAnnotationReplies只返回普通回复。状态回复由 SDK 内部处理。removeAllAnnotationReplies会删除普通回复,并保留标记状态和审阅状态信息。- 设置标记状态或审阅状态后,如果界面需要最新模型数据,请重新读取注释或回复。
- 注释和回复接口使用从 0 开始的页面索引。
- 如果需要将修改持久化到文件,请在操作完成后保存文档。