微軟 Word 文件中的評論預覽

在文件建立和協作的動態領域中,在 Microsoft Word 中新增和管理註解的能力是不可或缺的。評論充當虛擬討論板,允許合作者交換想法,提出改進建議並提供回饋,而無需改變原始內容。此功能不僅有利於有效溝通,而且還增強了協作編輯流程,使其更加簡化和高效。因此,在本指南中,我們將深入研究新增和刪除註解的基本面,揭示使用 .NET REST API 在 MS Word 中這項重要功能的潛力和好處。

使用 .NET REST API 管理評論

使用 Aspose.Words Cloud SDK for .NET 操作和新增評論帶來了許多好處和多種功能,而不僅僅是評論管理。除此之外,SDK還提供廣泛的功能,包括Word文件生成,Word到JPG,Word到PDF,Word到ODT等轉換,文字和格式處理,影像處理和各種功能,使您能夠增強各種場景的文件處理能力。

從 DOCX 檔案取得註釋

您可以利用閱讀文件中的全部或特定的評論。 GetComments 方法提供從 DOCX 檔案讀取使用者評論的功能。下面指定的是一個 cURL 命令,您可以使用它來閱讀命令提示字元上的註解。

cURL 指令

請注意,為了閱讀評論,您需要提供有關儲存在雲端儲存上的文件的詳細信息,並且只有授權使用者才能獲得存取 API 的權限。因此,您需要先檢索 JWT 身份驗證令牌,並在上述命令中使用它。

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=xxxxxxx-1c8e-4ea4-a948-3857547232fa&client_secret=xxxxxx" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accpet: application/json"

一旦產生了 JWT 令牌,請執行下列命令以取得 Word 文件中的所有註解。

curl -v "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments" \
-X GET \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>"

請求 URL

https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments

C#.NET

為了讀取文件中的特定評論,請按照以下步驟操作:

  • 初始化WordsApi類別的實例。
  • 建立一個 GetCommentRequest 類別的對象,其中我們提供輸入 Volume 1.docx 檔案名稱和註解索引值作為輸入參數。
  • 最後,透過傳遞 GetComment(..) 方法傳回的值來初始化 CommentResponse 物件。
const string clientID = "xxxxxxxxx";   // Get AppKey and AppSID from https://dashboard.aspose.cloud/
const string clientSecret = "xxxxxxxx";    // Get AppKey and AppSID from https://dashboard.aspose.cloud/

// 初始化WordsApi對象
Aspose.Words.Cloud.Sdk.WordsApi wordsApi = new Aspose.Words.Cloud.Sdk.WordsApi(clientID, clientSecret);

// 輸入檔名
String filename= "Volume 1.docx";
// 評論索引
int commentIndex = 0;
// create an object of GetCommentRequest where we pass 輸入檔名 and comment index value
Aspose.Words.Cloud.Sdk.Model.Requests.GetCommentRequest request = new Aspose.Words.Cloud.Sdk.Model.Requests.GetCommentRequest(filename, commentIndex, null, null, null, null);
// 建立 CommentResponse 實例並從文件中讀取評論
Aspose.Words.Cloud.Sdk.Model.CommentResponse response = wordsApi.GetComment(request);
// 在控制台中顯示評論
Console.Write(response.Comment);

獲取所有評論

API 還提供從文件中獲取所有評論的功能(以防我們不想在個人之間遍歷)。對於此要求,您需要建立 GetCommentsRequest、CommentsResponse 類別的實例,並呼叫 WordsApi 物件的 GetComments(..) 方法。

在 Word 文件中插入註釋

該API也能夠在Word文件中新增/插入新的註釋物件。

cURL 指令

curl -v "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments" \
-X POST \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{\"RangeStart\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 2\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"RangeEnd\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/5/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 2\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"Author\":\"Nayyer Shahbaz\",\"Initial\":\"MOU\",\"DateTime\":\"2020-12-07T04:10:03.942Z\",\"Text\":\"Comments Inserted using Aspose.Words Cloud API\"}"

C#.NET

const string clientID = "xxxxxxxxx";   // Get AppKey and AppSID from https://dashboard.aspose.cloud/
const string clientSecret = "xxxxxxxx";    // Get AppKey and AppSID from https://dashboard.aspose.cloud/

// 初始化WordsApi對象
Aspose.Words.Cloud.Sdk.WordsApi wordsApi = new Aspose.Words.Cloud.Sdk.WordsApi(clientID, clientSecret);

// 輸入檔名
String filename = "Volume 1.docx";
// 評論索引
int commentIndex = 0;

Aspose.Words.Cloud.Sdk.Model.NodeLink link = new Aspose.Words.Cloud.Sdk.Model.NodeLink()
{
    NodeId = "0.1.0.1.1.1.1"
};
Aspose.Words.Cloud.Sdk.Model.DocumentPosition documentPosition = new Aspose.Words.Cloud.Sdk.Model.DocumentPosition()
{
    Node = link,
    Offset = 0
};
Aspose.Words.Cloud.Sdk.Model.CommentInsert commentInsert = new Aspose.Words.Cloud.Sdk.Model.CommentInsert()
{
    Initial = "Aspose",
    Author = "Nayyer Shahbaz",
    RangeStart = documentPosition,
    RangeEnd = documentPosition,
    Text = "Comment added through API"
};


// create an object of GetCommentRequest where we pass 輸入檔名 and comment index value
Aspose.Words.Cloud.Sdk.Model.Requests.InsertCommentRequest request = 
  new Aspose.Words.Cloud.Sdk.Model.Requests.InsertCommentRequest(filename, commentInsert, null, null, null);
// 建立 CommentResponse 實例並從文件中讀取評論
Aspose.Words.Cloud.Sdk.Model.CommentResponse response = wordsApi.InsertComment(request);
插入評論預覽。

圖 1:新插入評論的預覽

更新 Word 文件中的註釋

除了解析和新增註解之外,此API還支援更新Word文件中現有註釋的功能。

cURL 指令

curl -X PUT "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments/1" \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{\"RangeStart\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"self\",\"Type\":\"string\",\"Title\":\"Heading 1\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"RangeEnd\":{\"Node\":{\"link\":{\"Href\":\"https://api.aspose.cloud/v4.0/words/Volume 1.docx/sections/0/body/tables/0/rows/1/cells/1/paragraphs/1/runs/0\",\"Rel\":\"string\",\"Type\":\"string\",\"Title\":\"Heading 1\"},\"NodeId\":\"0.1.0.1.1.1.1\"},\"Offset\":0},\"Author\":\"Nayyer Shahbaz\",\"Initial\":\"Aspose.Words\",\"DateTime\":\"2020-12-07T06:18:31.348Z\",\"Text\":\"Comments updated using Aspose.Words Cloud API\"}"

C#.NET

為了以程式設計方式更新 Word 文件中的註釋,請按照下面指定的說明進行操作。

  • 建立WordsApi類別的實例。
  • 建立一個定義註解NodeID的NodeLink物件。
  • 建立了 DocumentPosition 類別的新對象,用於定義 RangeStart 和 RangeEnd 值。
  • 建立一個 CommentUpdate 對象,透過指派 DocumentPosition 實例值和註解中要更新的文字來定義註解首字母、作者姓名、RangeStart 和 RangeEnd 詳細資訊。
const string clientID = "xxxxxxxxx";   // Get AppKey and AppSID from https://dashboard.aspose.cloud/
const string clientSecret = "xxxxxxxx";    // Get AppKey and AppSID from https://dashboard.aspose.cloud/

// 初始化WordsApi對象
Aspose.Words.Cloud.Sdk.WordsApi wordsApi = new Aspose.Words.Cloud.Sdk.WordsApi(clientID, clientSecret);

// 輸入檔名
String filename = "Volume 1.docx";
// 評論索引
int commentIndex = 1;

Aspose.Words.Cloud.Sdk.Model.NodeLink link = new Aspose.Words.Cloud.Sdk.Model.NodeLink()
{
    NodeId = "0.1.0.1.1.1.1"
};
Aspose.Words.Cloud.Sdk.Model.DocumentPosition documentPosition = new Aspose.Words.Cloud.Sdk.Model.DocumentPosition()
{
    Node = link,
    Offset = 0
};
Aspose.Words.Cloud.Sdk.Model.CommentUpdate commentUpdate = new Aspose.Words.Cloud.Sdk.Model.CommentUpdate()
{
    Initial = "Aspose",
    Author = "Nayyer Shahbaz",
    RangeStart = documentPosition,
    RangeEnd = documentPosition,
    Text = "Comments updated using Aspose.Words Cloud API"
};

// create an object of GetCommentRequest where we pass 輸入檔名 and comment index value
Aspose.Words.Cloud.Sdk.Model.Requests.UpdateCommentRequest request =
                    new Aspose.Words.Cloud.Sdk.Model.Requests.UpdateCommentRequest(filename, commentIndex, commentUpdate, null, null, null);
// 建立 CommentResponse 實例並從文件中讀取評論
Aspose.Words.Cloud.Sdk.Model.CommentResponse response = wordsApi.UpdateComment(request);
更新評論預覽

圖 2:更新後的評論預覽

結果文件 Volume 1 - updated.docx 也附於此。

從 Word 文件中刪除註釋

您也可以使用 REST 雲端 API 從 word 檔案中刪除評論。

cURL 指令

curl -X DELETE "https://api.aspose.cloud/v4.0/words/Volume%201.docx/comments/0" \
-H "accept: /" \
-H "Authorization: Bearer <JWT Token>"

C#.NET

  • 建立WordsApi類別的物件。
  • 建立 DeleteCommentRequest 類別的實例,該類別以輸入檔案和評論索引作為參數。
  • 最後,DeleteComment(…) 方法以 DeleteCommentRequest 物件作為參數。
const string clientID = "xxxxxxxxx";   // Get AppKey and AppSID from https://dashboard.aspose.cloud/
const string clientSecret = "xxxxxxxx";    // Get AppKey and AppSID from https://dashboard.aspose.cloud/

// 初始化WordsApi對象
Aspose.Words.Cloud.Sdk.WordsApi wordsApi = new Aspose.Words.Cloud.Sdk.WordsApi(clientID, clientSecret);

// 輸入檔名
String filename = "Volume 1.docx";
// 評論索引
int commentIndex = 0;


Aspose.Words.Cloud.Sdk.Model.Requests.DeleteCommentRequest request = 
  new Aspose.Words.Cloud.Sdk.Model.Requests.DeleteCommentRequest(filename, commentIndex, null, null, null, null, null, null, null);
wordsApi.DeleteComment(request);
已刪除評論預覽

圖 3:已刪除評論的預覽