コメントの操作
オンラインで試す
あなたは私たちのこの機能を試すことができます 無料オンライン注釈を削除.
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-Java | |
String dataDir = Utils.getDataDir(AddComments.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Some text is added."); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date()); | |
builder.getCurrentParagraph().appendChild(comment); | |
comment.getParagraphs().add(new Paragraph(doc)); | |
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text.")); | |
doc.save(dataDir + "output.doc"); |
次のコード例は、テキストの領域とCommentRangeStartクラスとCommentRangeEndクラスを使用して段落にコメントを追加する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(AnchorComment.class); | |
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.getFirstSection().getBody().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.getFirstSection().getBody().appendChild(para2); | |
Comment comment = new Comment(doc, "Awais Hafeez", "AH", new Date()); | |
comment.getParagraphs().add(new Paragraph(doc)); | |
comment.getFirstParagraph().getRuns().add(new Run(doc, "Comment text.")); | |
CommentRangeStart commentRangeStart = new CommentRangeStart(doc, comment.getId()); | |
CommentRangeEnd commentRangeEnd = new CommentRangeEnd(doc, comment.getId()); | |
run1.getParentNode().insertAfter(commentRangeStart, run1); | |
run3.getParentNode().insertAfter(commentRangeEnd, run3); | |
commentRangeEnd.getParentNode().insertAfter(comment, commentRangeEnd); | |
doc.save(dataDir + "output.doc"); |
コメントの抽出または削除
Word文書でコメントを使用する(変更の追跡に加えて)ことは、文書をレビューするとき、特に複数のレビュー担当者がいる場合に一般的な方法です。 あなたが文書から必要とする唯一のものがコメントである状況があるかもしれません。 レビュー結果のリストを生成したい場合や、ドキュメントからすべての有用な情報を収集し、不要なコメントを削除したい場合などです。 特定のレビュー担当者のコメントを表示または削除することができます。
このサンプルでは、ドキュメント内のコメントから情報を収集し、ドキュメントからコメントを削除するための簡単な方法をいくつか見ていきます。 具体的には、次の方法について説明します:
- ドキュメントからすべてのコメントを抽出するか、特定の作成者によって作成されたコメントのみを抽出します。
- ドキュメントからすべてのコメントを削除するか、特定の作成者からのみ削除します。
コメントを抽出または削除する方法
このサンプルのコードは非常に単純で、すべてのメソッドは同じアプローチに基づいています。 Word文書内のコメントは、Aspose.Wordsドキュメントオブジェクトモデル内のComment
オブジェクトによって表されます。 ドキュメント内のすべてのコメントを収集するには、最初のパラメータをNodeType.Comment
に設定したgetChildNodesメソッドを使用します。 getChildNodesメソッドの2番目のパラメータがtrueに設定されていることを確認してください:これにより、getChildNodesは直接の子のみを収集するのではなく、すべての子ノードか
ドキュメントからコメントを抽出して削除する方法を説明するために、次の手順を実行します:
- Documentクラスを使用してWord文書を開く
- ドキュメントのすべてのコメントをコレクションに取得する
- コメントを抽出するには:
- 演算子のためにを使用してコレクションを通過します
- すべてのコメントの著者名、日付と時刻、テキストを抽出してリストします
- 特定の著者、この場合は著者’ks’によって書かれたコメントの著者名、日付と時刻とテキストを抽出してリストします
- コメントを削除するには:
- For the演算子を使用してコレクションを逆方向に移動します
- コメントを削除する
- 変更を保存します。
この演習では、次のWord文書を使用します:
ご覧のとおり、イニシャル「pm」と「ks」を持つ2人の著者からのいくつかのコメントが含まれています。
すべてのコメントを抽出する方法
getChildNodesメソッドは非常に便利で、任意のタイプのドキュメントノードのリストを取得する必要があるたびに使用できます。 このコレクション内の項目を列挙またはアクセスする場合にのみ、ノードがこのコレクション内で選択されるため、結果のコレクションでは即時オーバ
次のコード例は、ドキュメント内のすべてのコメントの作成者名、日付と時刻、およびテキストを抽出する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
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. | |
for (Comment comment : (Iterable<Comment>) comments) { | |
collectedComments.add(comment.getAuthor() + " " + comment.getDateTime() + " " + comment.toString(SaveFormat.TEXT)); | |
} | |
return collectedComments; |
指定された著者のコメントを抽出する方法
コレクションにコメントノードを選択したら、必要な情報を抽出するだけです。 このサンプルでは、著者のイニシャル、日付、時刻、およびコメントのプレーンテキストが1つの文字列に結合されています。代わりに、他の方法で保存することもできます。
特定の著者からコメントを抽出するオーバーロードされたメソッドはほぼ同じですが、配列に情報を追加する前に著者の名前をチェックするだけです。
次のコード例は、指定された作成者によるコメントの作成者名、日付と時刻、およびテキストを抽出する方法を示しています:
コメントを削除する方法
すべてのコメントを削除する場合は、コメントを1つずつ削除するコレクション内を移動する必要はありません。commentsコレクションでclearを呼び出すことで、それらを削除できます。
次のコード例は、ドキュメント内のすべてのコメントを削除する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ProcessComments.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
for (String comment : (Iterable<String>) extractComments(doc)) | |
System.out.print(comment); | |
// Remove comments by the "pm" author. | |
removeComments(doc, "pm"); | |
System.out.println("Comments from \"pm\" are removed!"); | |
// Extract the information about the comments of the "ks" author. | |
for (String comment : (Iterable<String>) extractComments(doc, "ks")) | |
System.out.print(comment); | |
//Read the comment's reply and resolve them. | |
System.out.println("Read the comment's reply and resolve them."); | |
CommentResolvedandReplies(doc); | |
// Remove all comments. | |
removeComments(doc); | |
System.out.println("All comments are removed!"); | |
// Save the document. | |
doc.save(dataDir + "output.doc"); |
起動すると、サンプルには次の結果が表示されます。 まず、すべての著者によるすべてのコメントを一覧表示し、次に選択した著者によるコメントのみを一覧表示します。 最後に、すべてのコメントを削除するコード。
出力Word文書からコメントが削除されました:
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RemoveCommentRegionText.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
CommentRangeStart commentStart = (CommentRangeStart) doc.getChild(NodeType.COMMENT_RANGE_START, 0, true); | |
CommentRangeEnd commentEnd = (CommentRangeEnd) doc.getChild(NodeType.COMMENT_RANGE_END, 0, true); | |
Node currentNode = commentStart; | |
Boolean isRemoving = true; | |
while (currentNode != null && isRemoving) { | |
if (currentNode.getNodeType() == NodeType.COMMENT_RANGE_END) | |
isRemoving = false; | |
Node nextNode = currentNode.nextPreOrder(doc); | |
currentNode.remove(); | |
currentNode = nextNode; | |
} | |
doc.save(dataDir + "output.doc"); |
コメントの返信を追加または削除する
addReplyメソッドはこのコメントに返信を追加します。 既存のMSオフィスの制限により、文書内では1つのレベルの返信のみが許可されていることに注意してください。 このメソッドが既存の返信コメントに対して呼び出されると、タイプInvalidOperationExceptionの例外が発生します。
このコメントに対する指定された返信を削除するには、removeReplyメソッドを使用できます。
次のコード例は、コメントに返信を追加し、コメントの返信を削除する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
Comment comment = (Comment) doc.getChild(NodeType.COMMENT, 0, true); | |
//Remove the reply | |
comment.removeReply(comment.getReplies().get(0)); | |
//Add a reply to comment | |
comment.addReply("John Doe", "JD", new Date(), "New reply"); | |
dataDir = dataDir + "TestFile_Out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
コメントの返信を読む
Aspose.Wordsコメントの返信を読むためのサポート。 Repliesプロパティは、指定されたコメントの直接の子であるCommentオブジェクトのコレクションを返します。
次のコード例は、コメントの返信を反復処理して解決する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
static void CommentResolvedandReplies(Document doc) { | |
NodeCollection<Comment> comments = doc.getChildNodes(NodeType.COMMENT, true); | |
Comment parentComment = (Comment) comments.get(0); | |
for (Comment childComment : parentComment.getReplies()) { | |
// Get comment parent and status. | |
System.out.println(childComment.getAncestor().getId()); | |
System.out.println(childComment.getDone()); | |
// And update comment Done mark. | |
childComment.setDone(true); | |
} | |
} |