在 Word 文檔上直接添加快速評論是協作審閱工作流程中的常見需求。 GroupDocs.Annotation Cloud SDK for Node.JS 讓您能在不離開伺服器環境的情況下以程式方式添加豐富的註釋。本指南將向您展示如何在 Node.JS 中於 Word 頁面上建立便利貼,涵蓋設定、詳細步驟說明、完整程式碼範例以及等效的 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 Download.

使用您的憑證建立配置物件(摘自完整範例):

const { Configuration } = require("@groupdocs.annotation-cloud");

const config = new Configuration({
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
});

安裝好 SDK 並準備好憑證後,我們可以繼續實作。

在 Node.JS 中於 Word 頁面建立便利貼:逐步操作說明

在本教學中,我們將使用 SDK 的註釋類別在 Node.JS 中的 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 - 背景顏色的 HEX 值(例如 "FFFF00" 代表黃色)。

您也可以通过 AnnotationInfo 模型设置可选字段,例如 author、fontFamily 或 opacity。请参阅 API 参考 以获取可配置属性的完整列表。

結論

按照上述步驟,您現在已了解如何在 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 屬性設定為 HEX 值,並調整 rect 的尺寸(width、height)以控制貼紙的大小和位置。

Read More