在 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 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。

也可以看看