在 Node.js 中使用 REST API 在 Word 文档中添加注释

我们可以轻松地在云端以编程方式对 Word 文档进行批注。我们可以在文档中添加图像、评论、注释或其他类型的外部注释作为注释。在本文中,我们将学习如何在 Node.js 中使用 REST API 在 Word 文档中添加注释。

本文应涵盖以下主题:

Word 文档注释 REST API 和 Node.js SDK

为了注释 DOCDOCX 文件,我们将使用 GroupDocs.Annotation Cloud 的 Node.js SDK API。请在控制台中使用以下命令安装它:

npm install groupdocs-annotation-cloud

在执行上述步骤之前,请从仪表板获取您的客户端 ID 和密码。获得 ID 和密码后,添加如下所示的代码:

global.clientId = "659fe7da-715b-4744-a0f7-cf469a392b73"; 
global.clientSecret = "b377c36cfa28fa69960ebac6b6e36421";
global.myStorage = "";

const configuration = new groupdocs_annotation_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";

在 Node.js 中使用 REST API 注释 Word 文档

我们可以按照下面提到的简单步骤向 DOCX 文件添加注释:

上传文件

首先,我们将使用下面给出的代码示例将 DOCX 文件上传到云端:

// 初始化接口
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);
// 从本地/光盘打开 IOStream 中的文件。
var resourcesFolder = 'C:\\Files\\Annotation\\sample.docx';
fs.readFile(resourcesFolder, (err, fileStream) => {
  // 创建上传请求
  var request = new groupdocs_annotation_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
  // 上传文件
  fileApi.uploadFile(request);
});

因此,上传的 DOCX 文件将在云端仪表板的 文件部分 中可用。

在 Node.js 中向 DOCX 文件添加多个注释

我们可以按照下面给出的步骤以编程方式向 Word 文档添加多个注释:

  • 首先,创建 AnnotateApi 的实例。
  • 接下来,创建 AnnotationInfo 的第一个实例。
  • 然后,为第一个实例设置注释属性,例如位置、类型、文本等。
  • 重复上述步骤以添加 AnnotationInfo 的多个实例。我们将为每个实例设置不同的注解类型和其他属性来添加多个注解。
  • 接下来,创建 FileInfo 的实例并设置输入文件路径。
  • 然后,创建 AnnotateOptions 的一个实例。
  • 现在,将 FileInfo 和定义的注释实例分配给 AnnotateOptions。
  • 另外,设置输出文件路径。
  • 之后,使用 AnnotateOptions 调用 AnnotateRequest 方法。
  • 最后用AnnotateRequest调用AnnotateApi.annotate()方法得到结果。

以下代码示例展示了如何使用 Node.js 中的 REST API 向 Word 文档添加多个注释。

// 初始化接口
let annotateApi = groupdocs_annotation_cloud.注释Api.fromKeys(clientId, clientSecret);

// 定义距离注释
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 100
a1.box.y = 100
a1.box.width = 200
a1.box.height = 100
a1.pageNumber = 0
a1.penColor = 1201033
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 3
a1.opacity = 1
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Distance;
a1.text = "This is distance annotation";
a1.creatorName = "Anonym A.";

// 定义区域注释
let a2 = new groupdocs_annotation_cloud.AnnotationInfo();
a2.annotationPosition = new groupdocs_annotation_cloud.Point();
a2.annotationPosition.x = 1;
a2.annotationPosition.y = 1;
a2.box = new groupdocs_annotation_cloud.Rectangle();
a2.box.x = 80
a2.box.y = 400
a2.box.width = 200
a2.box.height = 100
a2.penColor = 1201033;
a2.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a2.pageNumber = 0;
a2.penWidth = 3;
a2.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Area;
a2.text = "This is area annotation";
a2.creatorName = "Anonym A.";

// 定义箭头注释
let a3 = new groupdocs_annotation_cloud.AnnotationInfo();
a3.annotationPosition = new groupdocs_annotation_cloud.Point();
a3.annotationPosition.x = 1;
a3.annotationPosition.y = 1;
a3.box = new groupdocs_annotation_cloud.Rectangle();
a3.box.x = 100;
a3.box.y = 100;
a3.box.width = 200;
a3.box.height = 100;
a3.pageNumber = 0;
a3.penColor = 1201033;
a3.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a3.penWidth = 1;
a3.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Arrow;
a3.text = "This is arrow annotation";
a3.creatorName = "Anonym A.";

// 定义椭圆注释
let a4 = new groupdocs_annotation_cloud.AnnotationInfo();
a4.annotationPosition = new groupdocs_annotation_cloud.Point();
a4.annotationPosition.x = 1;
a4.annotationPosition.y = 1;
a4.box = new groupdocs_annotation_cloud.Rectangle();
a4.box.x = 350;
a4.box.y = 350;
a4.box.width = 200;
a4.box.height = 100;
a4.pageNumber = 0;
a4.penColor = 1201033;
a4.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a4.penWidth = 4;
a4.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Ellipse;
a4.text = "This is ellipse annotation";
a4.creatorName = "Anonym A.";

// 输入文件路径
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// 定义注释选项
let options = new groupdocs_annotation_cloud.注释Options();
options.fileInfo = fileInfo;
options.annotations = [a1, a2, a3, a4];
options.outputPath = "AddMultipleAnnotations.docx";

// 创建注释请求
let request = new groupdocs_annotation_cloud.注释Request(options);

// 注释
let result = await annotateApi.annotate(request);
在 Node.js 中向 DOCX 文件添加多个注释

在 Node.js 中向 DOCX 文件添加多个注释

下载注释文件

上面的代码示例会将带注释的 Word 文档 (DOCX) 保存在云端。可以使用以下代码示例下载它:

// 构造FileApi
var fileApi = new groupdocs_annotation_cloud.FileApi(configuration);

// 创建下载文件请求
let request = new groupdocs_annotation_cloud.DownloadFileRequest("AddMultipleAnnotations.docx", myStorage);
// 下载文件
let response = await fileApi.downloadFile(request);

// 在工作目录中保存文件
fs.writeFile("C:\\Files\\Annotation\\AddMultipleAnnotations.docx", response, "binary", function (err) { });

在 Node.js 中使用 REST API 在 Word 文档中添加图像注释

我们可以通过以下步骤以编程方式在 Word 文档中添加图像注释:

  • 首先,创建 AnnotateApi 的实例。
  • 接下来,创建 AnnotationInfo 的实例。
  • 然后,定义一个矩形并设置它的位置、高度和宽度。
  • 之后,设置注释属性,例如位置、文本、高度、宽度等。
  • 然后,将注释类型设置为图像。
  • 接下来,创建 FileInfo 的实例并设置输入文件路径。
  • 然后,创建 AnnotateOptions 的一个实例。
  • 现在,将 FileInfo 对象和注释分配给 AnnotateOptions。
  • 另外,设置输出文件路径。
  • 之后,通过使用 AnnotateOptions 对象作为参数调用 AnnotateRequest 方法来创建请求。
  • 最后,通过使用 AnnotateRequest 对象调用 AnnotateApi.annotate() 方法来获取结果。

以下代码示例展示了如何使用 Node.js 中的 REST API 将图像注释添加到 Word 文档。请按照前面提到的步骤上传和下载文件。

// 初始化接口
let annotateApi = groupdocs_annotation_cloud.注释Api.fromKeys(clientId, clientSecret);

// 定义图像注释
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 300;
a1.box.y = 320;
a1.box.width = 200;
a1.box.height = 40;
a1.pageNumber = 0;
a1.penColor = 1201033;
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 1;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Image;
a1.text = "This is image annotation";
a1.creatorName = "Anonym A.";
a1.imagePath = "JohnSmith.png";

// 输入文件路径
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// 定义注释选项
let options = new groupdocs_annotation_cloud.注释Options();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddImageAnnotation.docx";

// 创建注释请求
let request = new groupdocs_annotation_cloud.注释Request(options);

// 注释
let result = await annotateApi.annotate(request);
在 Node.js 中使用 REST API 在 Word 文档中添加图像注释

在 Node.js 中使用 REST API 在 Word 文档中添加图像注释

在 Node.js 中使用 REST API 在 Word 文档中添加文本字段注释

我们可以按照前面提到的步骤以编程方式在 Word 文档中添加文本字段注释。但是,我们需要将注释类型设置为 TextField。

以下代码示例展示了如何使用 Node.js 中的 REST API 将文本字段注释添加到 Word 文档。请按照前面提到的步骤上传和下载文件。

// 初始化接口
let annotateApi = groupdocs_annotation_cloud.注释Api.fromKeys(clientId, clientSecret);

// 定义文本字段注释
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 300;
a1.box.y = 310;
a1.box.width = 200;
a1.box.height = 50;
a1.pageNumber = 0;
a1.fontColor = 3093151;
a1.fontSize = 12;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.TextField;
a1.text = "Text field text";
a1.creatorName = "Anonym A.";

// 输入文件路径
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// 定义注释选项
let options = new groupdocs_annotation_cloud.注释Options();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddTextFieldAnnotation.docx";

// 创建注释请求
let request = new groupdocs_annotation_cloud.注释Request(options);

// 注释
let result = await annotateApi.annotate(request);
在 Node.js 中使用 REST API 在 Word 文档中添加注释

在 Node.js 中使用 REST API 在 Word 文档中添加文本字段注释

在 Node.js 中使用 REST API 的 Word 文档中的水印注释

我们可以按照前面提到的步骤以编程方式在Word文档中添加水印注释。但是,我们需要将注释类型设置为 Watermark。

以下代码示例展示了如何在 Node.js 中使用 REST API 向 Word 文档添加水印注释。请按照前面提到的步骤上传和下载文件。

// 初始化接口
let annotateApi = groupdocs_annotation_cloud.注释Api.fromKeys(clientId, clientSecret);

// 定义水印注释
let a1 = new groupdocs_annotation_cloud.AnnotationInfo();
a1.annotationPosition = new groupdocs_annotation_cloud.Point();
a1.annotationPosition.x = 1;
a1.annotationPosition.y = 1;
a1.box = new groupdocs_annotation_cloud.Rectangle();
a1.box.x = 100;
a1.box.y = 700;
a1.box.width = 500;
a1.box.height = 100;
a1.pageNumber = 0;
a1.penColor = 1201033;
a1.penStyle = groupdocs_annotation_cloud.AnnotationInfo.PenStyleEnum.Solid;
a1.penWidth = 2;
a1.fontSize = 24;
a1.angle = 75;
a1.type = groupdocs_annotation_cloud.AnnotationInfo.TypeEnum.Watermark;
a1.text = "This is a watermark annotation";
a1.creatorName = "Anonym A.";

// 输入文件路径
let fileInfo = new groupdocs_annotation_cloud.FileInfo();
fileInfo.filePath = "sample.docx";

// 定义注释选项
let options = new groupdocs_annotation_cloud.注释Options();
options.fileInfo = fileInfo;
options.annotations = [a1];
options.outputPath = "AddWatermarkAnnotation.docx";

// 创建注释请求
let request = new groupdocs_annotation_cloud.注释Request(options);

// 注释
let result = await annotateApi.annotate(request);
在 Node.js 中使用 REST API 的 Word 文档中的水印注释

在 Node.js 中使用 REST API 的 Word 文档中的水印注释

在线试用

请试用以下使用上述API开发的免费在线DOCX标注工具。 https://products.groupdocs.app/annotation/docx

结论

在本文中,我们学习了如何为云端Word文档添加批注。我们还了解了如何使用 Node.js 中的 REST API 将图像和文本字段注释添加到 DOCX 文件。本文还介绍了如何以编程方式将 DOCX 文件上传到云端,然后从云端下载编辑后的文件。此外,您可以使用 文档 了解有关 GroupDocs.Annotation Cloud API 的更多信息。我们还提供了一个 API 参考 部分,让您可以直接通过浏览器可视化我们的 API 并与之交互。如有任何歧义,请随时在论坛上与我们联系。

也可以看看