Word ドキュメントのコメント機能は、ドキュメントの内容に影響を与えることなく、ユーザーがコメントすることを可能にします。 ドキュメントの作成者はこの機能を使って内容に注釈をつけ、読者はこの機能を使って内容に対するコメントや質問をすることができます。 また、コメント機能では、追加されたコメントに対して返信を行うことができ、ユーザーがドキュメントについて議論しやすくなっています。 本記事では、Spire.Doc for Java を使用して Word ドキュメントにコメントを追加、返信、または削除する方法について説明します。 Word ドキュメント内の段落にコメントを追加する Word ドキュメント内のテキストにコメントを追加する Word ドキュメント内のコメントを返信する Word ドキュメントからコメントを削除する Spire.Doc for Java をインストールします まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.2.3</version> </dependency> </dependencies> Word ドキュメント内の段落にコメントを追加する Paragraph.appendComment() メソッドを使用して、段落にコメントを追加することができます。詳しい手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get() メソッドを使用して、ドキュメントの最初のセクションを取得します。 Section.getParagraphs().get() メソッドを使用して、セクションの最初の段落を取得します。 Paragraph.appendComment() メソッドを使用して、段落にコメントを追加します。 Comment.getFormat().setAuthor() メソッドを使用して、コメントの作成者を設定します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; public class addCommentParagraph { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("生まれてこのように.docx"); //ドキュメントの最初のセクションを取得する Section section = document.getSections().get(0); //セクションの2つの段落を取得する Paragraph paragraph = section.getParagraphs().get(1); //段落にコメントを追加する Comment comment = paragraph.appendComment("この段落は、記事の導入部分です。"); comment.getFormat().setAuthor("宇部 勝久"); //ドキュメントを保存する document.saveToFile("段落へのコメントの追加.docx"); } } Word ドキュメント内のテキストにコメントを追加する Spire.Doc for Java は、Word ドキュメント内のコメントを表す Comment クラスと、コメントされているテキストの開始または終了マーカーを表す CommentMark クラスを提供します。 Wordドキュメント内のテキストにコメントを追加する場合、カスタムの insertComment() メソッドを作成し、コメントを追加し、コメントされているテキストの開始位置と終了位置を設定することができます。 以下は、そのための手順です。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して、Word ドキュメントを読み込みます。 カスタムの insertComment() メソッドを使用して、ドキュメントにコメントを追加します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.*; import com.spire.doc.documents.CommentMark; import com.spire.doc.documents.CommentMarkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.Comment; import com.spire.doc.fields.TextRange; public class addCommentText { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document= new Document(); //Wordドキュメントを読み込む document.loadFromFile("生まれてこのように.docx"); //カスタマイズしたメソッドを使用して、Wordドキュメントにコメントを追加する insertComments(document, "気質"); //ドキュメントを保存する document.saveToFile("コメントの追加.docx", FileFormat.Docx); } private static void insertComments(Document doc, String keystring) { //コメントを付けるテキストを探す TextSelection find = doc.findString(keystring, false, true); //Comment クラスのオブジェクトを作成する Comment comment = new Comment(doc); //コメントのテキストを設定する comment.getBody().addParagraph().setText("気質とは、その人の精神活動の原動力の総和を意味します。"); //コメントの作成者を設定する comment.getFormat().setAuthor("厚川 徳辰"); //コメントを付けたいテキストがある段落を取得する TextRange range = find.getAsOneRange(); Paragraph para = range.getOwnerParagraph(); //段落にコメントを追加する para.getChildObjects().add(comment); //コメントの開始と終了のマーカーを作成する CommentMark commentMarkStart = new CommentMark(doc, comment.getFormat().getCommentId(), CommentMarkType.Comment_Start); CommentMark commentMarkEnd = new CommentMark(doc, comment.getFormat().getCommentId(), CommentMarkType.Comment_End); //段落に開始マークと終了マークを挿入して、コメントの開始位置と終了位置を設定する int index = para.getChildObjects().indexOf(range); para.getChildObjects().insert(index, commentMarkStart); para.getChildObjects().insert(index + 2, commentMarkEnd); } } Word ドキュメント内のコメントを返信する コメントへの返信は、Spire.Doc for Java が提供する Comment.replyToComment() を用いて、あるコメントを他のコメントへの返信として設定することが可能です。 以下は、その方法について説明します。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getComments().get() メソッドを使用して、ドキュメント内の最初のコメントを取得します。 Comment オブジェクトを作成し、コメント作成者を設定し、コメントのテキストを追加します。 Comment.replyToComment() メソッドを使用して、作成したコメントをドキュメントの最初のコメントへの返信として設定します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.*; import com.spire.doc.FileFormat; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.*; public class replyToComment { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("コメントの追加.docx"); //最初のコメントを取得する Comment comment = document.getComments().get(0); //Commentオブジェクトを作成し、コメント作成者を設定し、コメントのテキストを追加する Comment replyComment = new Comment(document); replyComment.getFormat().setAuthor("竹内 徠新"); Paragraph paragraph = replyComment.getBody().addParagraph(); paragraph.appendText("これは心理学の概念である。"); //このコメントを最初のコメントへの返信として設定する comment.replyToComment(replyComment); //ドキュメントを保存する document.saveToFile("コメントへの返信.docx", FileFormat.Docx); } } Word ドキュメントからコメントを削除する Document.getComments().removeAt() メソッドを使用して指定したコメントを削除するか、Document.getComments().clear() メソッドを使用してすべてのコメントを削除できます。 以下は、コメントを削除する手順です。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して、Word ドキュメントを読み込みます。 Document.getComments().clear() メソッドを使用して、すべてのコメントを削除します。 または、Document.getComments().removeAt() メソッドを使用して、指定されたコメントを削除します。 Document.saveToFile() メソッドを使用して、ドキュメントを保存します。 Java import com.spire.doc.*; import com.spire.doc.FileFormat; public class deleteComment { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document= new Document(); //Wordドキュメントを読み込む document.loadFromFile("コメントへの返信.docx"); //すべてのコメントを削除する document.getComments().clear(); //指定したコメントを削除する //document.getComments().removeAt(1); //ドキュメントを保存する document.saveToFile("コメントの削除.docx", FileFormat.Docx); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。