MS 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 クラスのインスタンスを初期化します。
  • 入力引数として入力Volume 1.docxファイル名とコメントインデックス値を指定して、GetCommentRequestクラスのオブジェクトを作成します。
  • 最後に、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 オブジェクトを作成します。
  • RangeStart 値と RangeEnd 値を定義する DocumentPosition クラスの新しいオブジェクトが作成されます。
  • DocumentPosition インスタンス値とコメント内で更新されるテキストを割り当てることで、コメントのイニシャル、作成者名、RangeStart および RangeEnd の詳細を定義する CommentUpdate オブジェクトを作成します。
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: 更新されたコメントのプレビュー

結果文書第1巻-更新.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 クラスのインスタンスを作成します。
  • 最後に、DeleteCommentRequest オブジェクトを引数として持つ DeleteComment(…) メソッドです。
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: 削除されたコメントのプレビュー