1
0

More than 1 year has passed since last update.

Javaを使用してWordコメントの追加、返信、変更(置換)、削除する方法

Last updated at Posted at 2022-03-04

Wordコメントは、特定のドキュメントのコンテンツに注釈を付けるためによく使用されるツールまたは方法であり、問題の説明とマーク修正の役割を果たします。この記事では、Wordのコメントを操作する方法を紹介します。

1.コメントの追加:コメントにテキストを追加し、コメントに画像を挿入します。

2.コメントに返信します。

3.コメントの変更または置換:コメントのテキストコンテンツをテキストに置き換え、コメントの画像をテキストに置き換え、コメントの画像を画像に置き換えます。

4.コメントの削除:指定されたコメントのすべてのコンテンツを削除し、指定されたコメントの指定されたコンテンツを削除します。

使用したツール:Free Spire.Doc for Java(無料版)
JARファイルのインポート(参照):

方法1:公式Webサイトからjarパッケージを入手し、解凍します。

インポート手順1:プログラムに新しいディレクトリディレクトリを作成し、コントロールパッケージのlibフォルダにあるSpire.Doc.jarファイル(下図のように)を新しく作成したディレクトリにコピーします。
00.png

インポート手順2:コピーしたjarファイルを右クリックし、「ライブラリとして追加」を選択し、ポップアップダイアログボックスで「OK」をクリックしてインポートを完了します。

方法2:Mavenの依存関係を追加してMavenプロジェクトにインポートします。

Javaサンプルコード

【例1】 コメント追加(テキスト、画像)

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;

public class AddComment {
    public static void main(String[] args) {
        //テストドキュメントをロードする
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\test.docx");

        //指定された段落を取得する
        Section sec = doc.getSections().get(0);
        Paragraph para= sec.getParagraphs().get(4);

        //コメントにテキストを挿入する
        Comment comment = para.appendComment("ウェルシュコーギーは、1107年にフランダースの労働者によって持ち込まれた品種です");
        comment.getFormat().setAuthor("管理者");
        //コメントに画像を挿入する
        comment.getBody().addParagraph().appendPicture("C:\\Users\\Administrator\\Desktop\\tp.png");

        //ドキュメントを保存する
        doc.saveToFile("AddComment.docx", FileFormat.Docx_2010);
    }
}

コメントの追加した結果:
01.png

【例2】コメントへの返信

import com.spire.doc.*;
import com.spire.doc.fields.Comment;

public class ReplyComment {
    public static void main(String[] args) throws Exception{
        //テストドキュメントをロードする
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\AddComment.docx");

        //指定されたコメントを取得する
        Comment comment = doc.getComments().get(0);

        //コメントに返信する
        Comment relyC= new Comment(doc);
        relyC.getFormat().setAuthor("編集者");
        relyC.getBody().addParagraph().appendText("編集済み");
        comment.replyToComment(relyC);

        //ドキュメントを保存する
        doc.saveToFile("ReplyComment.docx",FileFormat.Docx_2010);
    }
}

コメントに返信した結果:
02.png

【例3】コメントの変更または置換

import com.spire.doc.*;

public class ModifyComment {
    public static void main(String[] args){
        //コメント付きのテストドキュメントを読み込む
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\AddComment.docx");

        //最初のコメントの最初の段落を取得し、元のコメントのテキストをテキストに置き換えます
        doc.getComments().get(0).getBody().getParagraphs().get(0).replace("ウェルシュコーギーは、1107年にフランダースの労働者によって持ち込まれた品種です","口の鋭い犬の先祖と関係あると思われる",false,false);
        //最初の注釈の2番目の段落を取得し、元の注釈の画像をテキストに置き換える
        doc.getComments().get(0).getBody().getParagraphs().get(4).setText("管理者に知らせる");

        //最初のコメントの3番目の段落を取得し、元の画像を削除してから、メソッドを呼び出して新しい画像を追加する(画像を画像に置き換える)
        doc.getComments().get(0).getBody().getParagraphs().get(0).getChildObjects().removeAt(0);
        doc.getComments().get(0).getBody().getParagraphs().get(4).appendPicture("C:\\Users\\Administrator\\Desktop\\2.png");

        //ドキュメントを保存する
        doc.saveToFile("ModifyComment.docx",FileFormat.Docx_2010);
    }
}

変更または置換した結果:
03.png

【例4】コメントを削除する

import com.spire.doc.*;
import com.spire.doc.FileFormat;

public class DeleteComment{
    public static void main(String[] args) {
        //テストドキュメントをロードする
        Document doc = new Document("C:\\Users\\Administrator\\Desktop\\AddComment.docx");

        //メソッドを呼び出して、指定されたコメントを削除する(コメントのすべての内容を削除する)
        doc.getComments().removeAt(0);

        //指定された注釈の指定された段落を削除する(注釈のコンテンツの一部を削除する)
        doc.getComments().get(0).getBody().getParagraphs().get(4).getChildObjects().removeAt(4);

        //ドキュメントを保存する 
        doc.saveToFile("DeleteComment.docx", FileFormat.Docx_2010);
    }
}

全てのコメントを削除した結果:
04.png

結語

今回のコメントの追加、返信、変更(置換)、削除する方法は以上でした、最後まで読んでいただきありがとうございます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0