在 Word 文档上直接添加快速评论是协作审阅工作流中的常见需求。 GroupDocs.Annotation Cloud SDK for Node.JS 让您能够在不离开服务器环境的情况下,以编程方式添加丰富的批注。在本指南中,我们将展示如何在 Node.JS 中的 Word 页面上创建便签(Sticky Notes),包括环境设置、详细操作步骤、完整代码示例以及等效的 cURL 调用。
设置 GroupDocs.Annotation Cloud SDK for Node.JS
在开始之前,请确保您已拥有:
- 已安装 Node.js 14+ 和 npm
- 拥有 clientId 和 clientSecret 的 GroupDocs 云账户
- 访问您想要注释的源 DOCX 文件
通过 npm 安装 SDK:
npm install groupdocs-annotation-cloud
如果您更喜欢手动安装,请从官方发布页面下载最新软件包: GroupDocs.Annotation Cloud SDK for Node.JS 下载。
使用您的凭据创建配置对象(摘自完整示例):
const { Configuration } = require("@groupdocs.annotation-cloud");
const config = new Configuration({
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET"
});
安装好 SDK 并准备好凭据后,我们可以继续实现。
在 Node.JS 中为 Word 页面创建便签:逐步演练
在本教程中,我们将在 Node.JS 中使用 SDK 的注释类在 Word 页面上创建便签。
步骤 1:初始化注释 API
首先,导入所需的类并创建 AnnotateApi 的实例。
const { AnnotateApi, models } = require("@groupdocs.annotation-cloud");
const annotateApi = new AnnotateApi(config);
步骤 2:定义源文件和目标文件
指定输入的 DOCX 文件以及带注释的输出路径。
const inputFilePath = "sample.docx";
const outputFilePath = "sample_annotated.docx";
const fileInfo = new models.FileInfo();
fileInfo.filePath = inputFilePath;
第3步:设置便利贴位置
创建一个 矩形,用于确定便利贴在第 1 页出现的位置。
const rect = new models.Rectangle();
rect.x = 100; // distance from left edge (points)
rect.y = 100; // distance from top edge (points)
rect.width = 200; // width of the sticky note
rect.height = 100; // height of the sticky note
步骤 4:构建便签注释对象
配置注释类型、页码、矩形、文本和背景颜色。
const stickyNote = new models.AnnotationInfo();
stickyNote.annotationType = "StickyNote";
stickyNote.pageNumber = 1;
stickyNote.rect = rect;
stickyNote.text = "This is a sticky note added via GroupDocs.Annotation Cloud SDK.";
stickyNote.color = "FFFF00"; // Yellow background
步骤 5:执行注释请求
组装请求选项并调用 annotate 方法。
const annotateOptions = new models.AnnotateOptions();
annotateOptions.fileInfo = fileInfo;
annotateOptions.annotations = [stickyNote];
annotateOptions.outputPath = outputFilePath;
(async () => {
try {
const request = new models.AnnotateRequest(annotateOptions);
const response = await annotateApi.annotate(request);
console.log("Sticky note created successfully. Output saved to:", response.path);
} catch (err) {
console.error("Failed to create sticky note:", err);
}
})();
完整的工作示例:在 Node.JS 中为 Word 页面创建便签
以下示例演示了在 Node.JS 中使用 GroupDocs.Annotation Cloud SDK 在 Word 页面上创建粘性便签的完整实现。
const { AnnotateApi, Configuration, models } = require("@groupdocs.annotation-cloud");
// -----------------------------------------------------------------------------
// Configuration – replace with your actual GroupDocs credentials
// -----------------------------------------------------------------------------
const config = new Configuration({
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET"
});
const annotateApi = new AnnotateApi(config);
// -----------------------------------------------------------------------------
// Define input and output files
// -----------------------------------------------------------------------------
const inputFilePath = "sample.docx";
const outputFilePath = "sample_annotated.docx";
// -----------------------------------------------------------------------------
// Build FileInfo object for the source document
// -----------------------------------------------------------------------------
const fileInfo = new models.FileInfo();
fileInfo.filePath = inputFilePath;
// -----------------------------------------------------------------------------
// Define rectangle (position & size) for the sticky note on page 1
// -----------------------------------------------------------------------------
const rect = new models.Rectangle();
rect.x = 100; // distance from left edge (points)
rect.y = 100; // distance from top edge (points)
rect.width = 200; // width of the sticky note
rect.height = 100; // height of the sticky note
// -----------------------------------------------------------------------------
// Create the sticky‑note annotation
// -----------------------------------------------------------------------------
const stickyNote = new models.AnnotationInfo();
stickyNote.annotationType = "StickyNote";
stickyNote.pageNumber = 1;
stickyNote.rect = rect;
stickyNote.text = "This is a sticky note added via GroupDocs.Annotation Cloud SDK.";
stickyNote.color = "FFFF00"; // Yellow background
// -----------------------------------------------------------------------------
// Assemble the annotate request options
// -----------------------------------------------------------------------------
const annotateOptions = new models.AnnotateOptions();
annotateOptions.fileInfo = fileInfo;
annotateOptions.annotations = [stickyNote];
annotateOptions.outputPath = outputFilePath;
// -----------------------------------------------------------------------------
// Execute the request
// -----------------------------------------------------------------------------
(async () => {
try {
const request = new models.AnnotateRequest(annotateOptions);
const response = await annotateApi.annotate(request);
console.log("Sticky note created successfully. Output saved to:", response.path);
} catch (err) {
console.error("Failed to create sticky note:", err);
}
})();
注意: 此代码示例演示了核心功能。在项目中使用之前,请确保更新所有文件路径和配置值以匹配实际环境,验证所有必需的依赖项已正确安装,并在开发环境中彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
使用 cURL 和 REST API 添加便签注释
REST API 提供相同的功能,无需编写代码。以下是所需的 cURL 调用。
首先,获取访问令牌:
curl -X POST "https://api.groupdocs.cloud/v2.0.0/auth/token" \
-H "Content-Type: application/json" \
-d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'
上传源 DOCX 文件(将 YOUR_ACCESS_TOKEN 替换为上一步中的令牌):
curl -X PUT "https://api.groupdocs.cloud/v2.0.0/storage/file/sample.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @sample.docx
创建便签注释:
curl -X POST "https://api.groupdocs.cloud/v2.0.0/annotation/annotate" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"fileInfo": { "filePath": "sample.docx" },
"annotations": [
{
"annotationType": "StickyNote",
"pageNumber": 1,
"rect": { "x": 100, "y": 100, "width": 200, "height": 100 },
"text": "This is a sticky note added via GroupDocs.Annotation Cloud SDK.",
"color": "FFFF00"
}
],
"outputPath": "sample_annotated.docx"
}'
下载已注释的文档:
curl -X GET "https://api.groupdocs.cloud/v2.0.0/storage/file/sample_annotated.docx?download=true" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o sample_annotated.docx
有关请求负载的更多详细信息,请参阅 API 参考。
配置便签的注释选项
SDK 提供了多个属性,允许您微调便签的外观和行为:
- annotationType - 必须设置为
"StickyNote"(必需)。 - pageNumber - 注释出现的页面。
- rect - 定义位置(
x、y)和大小(width、height)。调整这些值以控制放置位置。 - text - 注释内容。
- color - 背景颜色的十六进制值(例如
"FFFF00"表示黄色)。
您也可以通过 AnnotationInfo 模型设置可选字段,例如 author、fontFamily 或 opacity。请参阅 API reference 以获取可配置属性的完整列表。
结论
通过上述步骤,您现在已经了解如何在 Node.JS 中使用 GroupDocs.Annotation Cloud SDK 在 Word 页面上创建粘性便签。无论您更喜欢原生 Node.js 实现还是基于 REST 的 cURL 工作流,SDK 都提供了一种一致且可靠的方式,以编程方式对 DOCX 文件进行注释。请记得在生产环境中使用有效许可证;您可以从许可证页面获取临时许可证,或在产品页面查看定价选项。祝注释愉快!
常见问题
如何在 Node.JS 中使用 GroupDocs.Annotation Cloud SDK 在 Word 页面上创建粘性便签?
如代码示例所示,使用 AnnotateApi。SDK 会自动处理文件访问、注释创建和输出生成。使用 SDK 支持哪些文件格式进行批注?
SDK 支持 DOCX、PDF、PPTX、XLSX,以及许多其他流行格式。完整列表请参阅官方文档。如何在使用粘贴注释的 REST API 时进行身份验证?
通过使用客户端凭据调用/auth/token端点获取访问令牌,然后在所有后续调用的Authorization: Bearer标头中包含该令牌。详细步骤请参阅 API 参考。我可以自定义便签的颜色或大小吗?
是的。将color属性设置为十六进制值,并调整rect的尺寸(width、height)以控制便签的大小和位置。
