コメントの操作
オンラインで試す
この機能は 無料のオンライン注釈削除 で試すことができます。
Aspose.Words を使用すると、ユーザーはコメントを操作できます。Aspose.Words のドキュメント内のコメントは Comment クラスで表されます。また、CommentRangeStart クラスと CommentRangeEnd クラスを使用して、コメントに関連付けるテキストの領域を指定します。
コメントを追加する
Aspose.Words では、いくつかの方法でコメントを追加できます。
- Comment クラスの使用
- CommentRangeStart クラスと CommentRangeEnd クラスの使用
次のコード例は、Comment クラスを使用して段落にコメントを追加する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Write("Some text is added."); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
builder.CurrentParagraph.AppendChild(comment); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
dataDir = dataDir + "Comments_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
次のコード例は、テキスト領域と CommentRangeStart クラスおよび CommentRangeEnd クラスを使用して段落にコメントを追加する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
Document doc = new Document(); | |
Paragraph para1 = new Paragraph(doc); | |
Run run1 = new Run(doc, "Some "); | |
Run run2 = new Run(doc, "text "); | |
para1.AppendChild(run1); | |
para1.AppendChild(run2); | |
doc.FirstSection.Body.AppendChild(para1); | |
Paragraph para2 = new Paragraph(doc); | |
Run run3 = new Run(doc, "is "); | |
Run run4 = new Run(doc, "added "); | |
para2.AppendChild(run3); | |
para2.AppendChild(run4); | |
doc.FirstSection.Body.AppendChild(para2); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", DateTime.Today); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.Id); | |
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.Id); | |
run1.ParentNode.InsertAfter(commentRangeStart, run1); | |
run3.ParentNode.InsertAfter(commentRangeEnd, run3); | |
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd); | |
dataDir = dataDir + "Anchor.Comment_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
コメントの抽出または削除
Word 文書内で (変更の追跡に加えて) コメントを使用することは、文書をレビューするとき、特に複数のレビュー担当者がいる場合に一般的に行われます。ドキュメントに必要なものがコメントだけである場合があります。レビュー結果のリストを生成したいとします。あるいは、文書からすべての有用な情報を収集し、単に不要なコメントを削除したいとします。特定の査読者のコメントを表示または削除したい場合があります。
このサンプルでは、ドキュメント内のコメントから情報を収集する方法と、ドキュメントからコメントを削除する方法の両方を行う簡単な方法をいくつか見ていきます。具体的には次の方法について説明します。
- ドキュメントからすべてのコメント、または特定の作成者によって作成されたコメントのみを抽出します
- ドキュメントからすべてのコメントを削除するか、特定の作成者からのみコメントを削除します
コメントを抽出または削除する方法
このサンプルのコードは実際には非常に単純で、すべてのメソッドは同じアプローチに基づいています。 Word 文書内のコメントは、Aspose.Words 文書オブジェクト モデルの Comment オブジェクトによって表されます。ドキュメント内のすべてのコメントを収集するには、最初のパラメータを NodeType.Comment に設定して GetChildNodes メソッドを使用します。 GetChildNodes メソッドの 2 番目のパラメーターが true に設定されていることを確認します。これにより、GetChildNodes は直接の子だけを収集するのではなく、すべての子ノードから再帰的に選択するようになります。
ドキュメントからコメントを抽出および削除する方法を説明するために、次の手順を実行します。
- Document クラスを使用して Word 文書を開きます
- ドキュメントからすべてのコメントをコレクションに取得します
- コメントを抽出するには:
- foreach 演算子を使用してコレクションを調べます
- すべてのコメントの作成者名、日付と時刻、テキストを抽出してリストします。
- 特定の著者 (この場合は著者「ks」) が書いたコメントの著者名、日付と時刻、テキストを抽出してリストします。
- コメントを削除するには:
- for 演算子を使用してコレクションを逆方向に移動します。
- コメントを削除する
- 変更を保存します
すべてのコメントを抽出する方法
GetChildNodes メソッドは非常に便利で、あらゆる種類のドキュメント ノードのリストを取得する必要があるときはいつでも使用できます。このコレクション内の項目を列挙またはアクセスする場合にのみノードがこのコレクションに選択されるため、結果のコレクションでは即時にオーバーヘッドが発生しません。
次のコード例は、ドキュメント内のすべてのコメントの作成者名、日付と時刻、テキストを抽出する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static ArrayList ExtractComments(Document doc) | |
{ | |
ArrayList collectedComments = new ArrayList(); | |
// Collect all comments in the document | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
// Look through all comments and gather information about them. | |
foreach (Comment comment in comments) | |
{ | |
collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text)); | |
} | |
return collectedComments; | |
} |
指定した作成者のコメントを抽出する方法
コレクションに Comment ノードを選択したら、必要な情報を抽出するだけです。このサンプルでは、作成者のイニシャル、日付、時刻、およびコメントのプレーン テキストが 1 つの文字列に結合されます。代わりに、他の方法で保存することもできます。
特定の作成者からのコメントを抽出するオーバーロードされたメソッドはほぼ同じで、情報を配列に追加する前に作成者の名前をチェックするだけです。
次のコード例は、指定した作成者によるコメントの作成者名、日付と時刻、テキストを抽出する方法を示しています。
コメントを削除する方法
すべてのコメントを削除する場合は、コレクション内を移動してコメントを 1 つずつ削除する必要はありません。コメント コレクションで Clear メソッドを呼び出すことで、それらを削除できます。
次のコード例は、ドキュメント内のすべてのコメントを削除する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void RemoveComments(Document doc) | |
{ | |
// Collect all comments in the document | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
// Remove all comments. | |
comments.Clear(); | |
} |
コメントを選択的に削除する必要がある場合、そのプロセスはコメント抽出に使用したコードにより似たものになります。
次のコード例は、指定した作成者によるコメントを削除する方法を示しています。
ここで強調すべき主な点は、for 演算子の使用です。単純な抽出とは異なり、ここではコメントを削除します。適切な方法は、最後のコメントから最初のコメントまでコレクションを逆方向に反復することです。その理由は、最後から開始して後方に移動すると、前の項目のインデックスは変更されず、コレクション内の最初の項目に戻ることができるためです。
次のコード例は、コメントの抽出と削除のメソッドを示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
// Extract the information about the comments of all the authors. | |
foreach (string comment in ExtractComments(doc)) | |
Console.Write(comment); | |
// Remove comments by the "pm" author. | |
RemoveComments(doc, "pm"); | |
Console.WriteLine("Comments from \"pm\" are removed!"); | |
// Extract the information about the comments of the "ks" author. | |
foreach (string comment in ExtractComments(doc, "ks")) | |
Console.Write(comment); | |
//Read the comment's reply and resolve them. | |
CommentResolvedandReplies(doc); | |
// Remove all comments. | |
RemoveComments(doc); | |
Console.WriteLine("All comments are removed!"); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
// Save the document. | |
doc.Save(dataDir); |
CommentRangeStart と CommentRangeEnd の間のコメントを削除する方法
Aspose.Words を使用すると、CommentRangeStart ノードと CommentRangeEnd ノードの間のコメントを削除することもできます。
次のコード例は、CommentRangeStart と CommentRangeEnd の間のテキストを削除する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithComments(); | |
string fileName = "TestFile.doc"; | |
// Open the document. | |
Document doc = new Document(dataDir + fileName); | |
CommentRangeStart commentStart = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true); | |
Node currentNode = commentStart; | |
Boolean isRemoving = true; | |
while (currentNode != null && isRemoving) | |
{ | |
if (currentNode.NodeType == NodeType.CommentRangeEnd) | |
isRemoving = false; | |
Node nextNode = currentNode.NextPreOrder(doc); | |
currentNode.Remove(); | |
currentNode = nextNode; | |
} | |
dataDir = dataDir + "RemoveRegionText_out.doc"; | |
// Save the document. | |
doc.Save(dataDir); |
コメントの返信を追加または削除する
AddReply メソッドは、このコメントに返信を追加します。 Microsoft Office の既存の制限により、ドキュメント内では 1 レベルの返信のみが許可されることに注意してください。このメソッドが既存の Reply コメントに対して呼び出された場合、InvalidOperationException 型の例外が発生します。
RemoveReply メソッドを使用して、このコメントに対する指定された返信を削除できます。
次のコード例は、コメントに返信を追加し、コメントの返信を削除する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
Comment comment = (Comment)doc.GetChild(NodeType.Comment, 0, true); | |
//Remove the reply | |
comment.RemoveReply(comment.Replies[0]); | |
//Add a reply to comment | |
comment.AddReply("John Doe", "JD", new DateTime(2017, 9, 25, 12, 15, 0), "New reply"); | |
dataDir = dataDir + "TestFile_Out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
コメントの返信を読む
Replies プロパティは、指定されたコメントの直接の子である Comment オブジェクトのコレクションを返します。
次のコード例は、コメントの返信を反復処理して解決する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
static void CommentResolvedandReplies(Document doc) | |
{ | |
NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); | |
Comment parentComment = (Comment)comments[0]; | |
foreach (Comment childComment in parentComment.Replies) | |
{ | |
// Get comment parent and status. | |
Console.WriteLine(childComment.Ancestor.Id); | |
Console.WriteLine(childComment.Done); | |
// And update comment Done mark. | |
childComment.Done = true; | |
} | |
} |