コメントの操作

Aspose.Wordsはユーザーがコメントを操作できるようにします–Aspose.Wordsのドキュメント内のコメントはCommentクラスで表されます。 また、CommentRangeStartクラスとCommentRangeEndクラスを使用して、コメントに関連付けるテキストの領域を指定します。

コメントを追加する

Aspose.Wordsでは、いくつかの方法でコメントを追加できます:

  1. Commentクラスの使用
  2. CommentRangeStartクラスとCommentRangeEndクラスの使用

次のコード例は、Commentクラスを使用して段落にコメントを追加する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Write(u"Some text is added.");
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
builder->get_CurrentParagraph()->AppendChild(comment);
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::String outputPath = outputDataDir + u"AddComments.doc";
// Save the document.
doc->Save(outputPath);

次のコード例は、テキストの領域とCommentRangeStartクラスとCommentRangeEndクラスを使用して段落にコメントを追加する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<Paragraph> para1 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run1 = System::MakeObject<Run>(doc, u"Some ");
System::SharedPtr<Run> run2 = System::MakeObject<Run>(doc, u"text ");
para1->AppendChild(run1);
para1->AppendChild(run2);
doc->get_FirstSection()->get_Body()->AppendChild(para1);
System::SharedPtr<Paragraph> para2 = System::MakeObject<Paragraph>(doc);
System::SharedPtr<Run> run3 = System::MakeObject<Run>(doc, u"is ");
System::SharedPtr<Run> run4 = System::MakeObject<Run>(doc, u"added ");
para2->AppendChild(run3);
para2->AppendChild(run4);
doc->get_FirstSection()->get_Body()->AppendChild(para2);
System::SharedPtr<Comment> comment = System::MakeObject<Comment>(doc, u"Awais Hafeez", u"AH", System::DateTime::get_Today());
comment->get_Paragraphs()->Add(System::MakeObject<Paragraph>(doc));
comment->get_FirstParagraph()->get_Runs()->Add(System::MakeObject<Run>(doc, u"Comment text."));
System::SharedPtr<CommentRangeStart> commentRangeStart = System::MakeObject<CommentRangeStart>(doc, comment->get_Id());
System::SharedPtr<CommentRangeEnd> commentRangeEnd = System::MakeObject<CommentRangeEnd>(doc, comment->get_Id());
run1->get_ParentNode()->InsertAfter(commentRangeStart, run1);
run3->get_ParentNode()->InsertAfter(commentRangeEnd, run3);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);
System::String outputPath = outputDataDir + u"AnchorComment.doc";
// Save the document.
doc->Save(outputPath);

コメントの抽出または削除

Word文書でコメントを使用する(変更の追跡に加えて)ことは、文書をレビューするとき、特に複数のレビュー担当者がいる場合に一般的な方法です。 あなたが文書から必要とする唯一のものがコメントである状況があるかもしれません。 レビュー結果のリストを生成したい場合や、ドキュメントからすべての有用な情報を収集し、不要なコメントを削除したい場合などです。 特定のレビュー担当者のコメントを表示または削除することができます。

このサンプルでは、ドキュメント内のコメントから情報を収集し、ドキュメントからコメントを削除するための簡単な方法をいくつか見ていきます。 具体的には、次の方法について説明します:

  • ドキュメントからすべてのコメントを抽出するか、特定の作成者によって作成されたコメントのみを抽出します。
  • ドキュメントからすべてのコメントを削除するか、特定の作成者からのみ削除します。

コメントを抽出または削除する方法

このサンプルのコードは実際には非常に単純であり、すべてのメソッドは同じアプローチに基づいています。 Word文書内のコメントは、Aspose.Wordsドキュメントオブジェクトモデル内のCommentオブジェクトによって表されます。 ドキュメント内のすべてのコメントを収集するには、最初のパラメータをNodeType.Commentに設定してGetChildNodesメソッドを使用します。 GetChildNodesメソッドの2番目のパラメータがtrueに設定されていることを確認してください:これにより、GetChildNodesは直接の子のみを収集するのではなく、すべての子ノードか

ドキュメントからコメントを抽出して削除する方法を説明するために、次の手順を実行します:

  1. Documentクラスを使用してWord文書を開く
  2. ドキュメントのすべてのコメントをコレクションに取得する
  3. コメントを抽出するには:
    1. Foreach演算子を使用してコレクションを調べます
    2. すべてのコメントの著者名、日付と時刻、テキストを抽出してリストします
    3. 特定の著者、この場合は著者’ks’によって書かれたコメントの著者名、日付と時刻とテキストを抽出してリストします
  4. コメントを削除するには:
    1. For the演算子を使用してコレクションを逆方向に移動します
    2. コメントを削除する
  5. 変更を保存する

すべてのコメントを抽出する方法

GetChildNodesメソッドは非常に便利で、任意のタイプのドキュメントノードのリストを取得する必要があるたびに使用できます。 このコレクション内の項目を列挙またはアクセスする場合にのみ、ノードがこのコレクション内で選択されるため、結果のコレクションでは即時オーバ

次のコード例は、ドキュメント内のすべてのコメントの作成者名、日付と時刻、およびテキストを抽出する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about them.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
return collectedComments;
}

指定された著者のコメントを抽出する方法

コレクションにコメントノードを選択したら、必要な情報を抽出するだけです。 このサンプルでは、作成者のイニシャル、日付、時刻、およびコメントのプレーンテキストが1つの文字列に結合されています。代わりに、他の方法で保存することもできます。

特定の著者からコメントを抽出するオーバーロードされたメソッドはほぼ同じですが、配列に情報を追加する前に著者の名前をチェックするだけです。

次のコード例は、指定された作成者によるコメントの作成者名、日付と時刻、およびテキストを抽出する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
std::vector<System::String> ExtractComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
std::vector<System::String> collectedComments;
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and gather information about those written by the authorName author.
for (System::SharedPtr<Comment> comment : System::IterateOver<System::SharedPtr<Comment>>(comments))
{
if (comment->get_Author() == authorName)
{
collectedComments.push_back(comment->get_Author() + u" " + comment->get_DateTime() + u" " + System::StaticCast<Node>(comment)->ToString(SaveFormat::Text));
}
}
return collectedComments;
}

コメントを削除する方法

すべてのコメントを削除する場合は、コメントを1つずつ削除するコレクション内を移動する必要はありません。commentsコレクションでNodeCollection.Clearを呼び出すことで、それらを削除できます。

次のコード例は、ドキュメント内のすべてのコメントを削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void RemoveComments(const System::SharedPtr<Document>& doc)
{
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Remove all comments.
comments->Clear();
}

コメントを選択的に削除する必要がある場合、プロセスはコメント抽出に使用したコードに似たものになります。

次のコード例は、指定した作成者によるコメントを削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void RemoveComments(const System::SharedPtr<Document>& doc, const System::String& authorName)
{
// Collect all comments in the document
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
// Look through all comments and remove those written by the authorName author.
for (int32_t i = comments->get_Count() - 1; i >= 0; i--)
{
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(comments->idx_get(i));
if (comment->get_Author() == authorName)
{
comment->Remove();
}
}
}

ここで強調する主なポイントは、for演算子の使用です。 単純な抽出とは異なり、ここではコメントを削除します。 適切なトリックは、最後のコメントから最初のコメントまでコレクションを逆方向に反復することです。 この理由最後から開始して後方に移動すると、前の項目のインデックスは変更されず、コレクション内の最初の項目に戻ることができます。

次のコード例は、コメントの抽出と削除のメソッドを示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
// Extract the information about the comments of all the authors.
for (System::String const &comment : ExtractComments(doc))
{
std::cout << comment.ToUtf8String();
}
// Remove comments by the "pm" author.
RemoveComments(doc, u"pm");
std::cout << "Comments from \"pm\" are removed!" << std::endl;
// Extract the information about the comments of the "ks" author.
for (System::String const &comment: ExtractComments(doc, u"ks"))
{
std::cout << comment.ToUtf8String();
}
//Read the comment's reply and resolve them.
CommentResolvedandReplies(doc);
// Remove all comments.
RemoveComments(doc);
std::cout << "All comments are removed!" << std::endl;
System::String outputPath = outputDataDir + u"ProcessComments.doc";
// Save the document.
doc->Save(outputPath);

CommentRangeStartとCommentRangeEndの間のコメントを削除する方法

Aspose.Wordsを使用すると、CommentRangeStartノードとCommentRangeEndノードの間のコメントを削除することもできます。

次のコード例は、CommentRangeStartCommentRangeEndの間のテキストを削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
// Open the document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<CommentRangeStart> commentStart = System::DynamicCast<CommentRangeStart>(doc->GetChild(NodeType::CommentRangeStart, 0, true));
System::SharedPtr<CommentRangeEnd> commentEnd = System::DynamicCast<CommentRangeEnd>(doc->GetChild(NodeType::CommentRangeEnd, 0, true));
System::SharedPtr<Node> currentNode = commentStart;
bool isRemoving = true;
while (currentNode != nullptr && isRemoving)
{
if (currentNode->get_NodeType() == NodeType::CommentRangeEnd)
{
isRemoving = false;
}
System::SharedPtr<Node> nextNode = currentNode->NextPreOrder(doc);
currentNode->Remove();
currentNode = nextNode;
}
System::String outputPath = outputDataDir + u"RemoveRegionText.doc";
// Save the document.
doc->Save(outputPath);

コメントの返信を追加および削除する

AddReplyメソッドはこのコメントに返信を追加します。 既存のMicrosoft Officeの制限により、文書内では1レベルの返信のみが許可されていることに注意してください。 このメソッドが既存の返信コメントに対して呼び出されると、タイプInvalidOperationExceptionの例外が発生します。

このコメントに対する指定された返信を削除するには、RemoveReplyメソッドを使用できます。

次のコード例は、コメントに返信を追加し、コメントの返信を削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithComments();
System::String outputDataDir = GetOutputDataDir_WorkingWithComments();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::SharedPtr<Comment> comment = System::DynamicCast<Comment>(doc->GetChild(NodeType::Comment, 0, true));
//Remove the reply
comment->RemoveReply(comment->get_Replies()->idx_get(0));
//Add a reply to comment
comment->AddReply(u"John Doe", u"JD", System::DateTime(2017, 9, 25, 12, 15, 0), u"New reply");
System::String outputPath = outputDataDir + u"CommentReply.doc";
// Save the document to disk.
doc->Save(outputPath);

コメントの返信を読む

Repliesプロパティは、指定されたコメントの直接の子であるCommentオブジェクトのコレクションを返します。

次のコード例は、コメントの返信を反復処理して解決する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void CommentResolvedandReplies(const System::SharedPtr<Document>& doc)
{
System::SharedPtr<NodeCollection> comments = doc->GetChildNodes(NodeType::Comment, true);
System::SharedPtr<Comment> parentComment = System::DynamicCast<Comment>(comments->idx_get(0));
for (System::SharedPtr<Comment> childComment : System::IterateOver<System::SharedPtr<Comment>>(parentComment->get_Replies()))
{
// Get comment parent and status.
std::cout << childComment->get_Ancestor()->get_Id() << std::endl << childComment->get_Done() << std::endl;
// And update comment Done mark.
childComment->set_Done(true);
}
}