Java add, reply, modify (replace), delete comment Word

Annotation is a commonly used tool or method to annotate the specific content of the document, played explanation, mark correct role. In this article, we describe how Word annotation methods, including:

1. Add Note: add annotations to text, insert a picture into the comment;

2. Reply to comment;

3. The modified or replaced annotation: replacing text with a text annotation, annotation replace text images, annotations replaced with a picture in the picture;

4. Remove Comments: delete all content specified in the annotation, delete the specified specify in comments

 


 

Use tools: as Free Spire.Doc for the Java (free version)

Jar file import (Reference):

Method 1 : By obtaining official website jar package, and unpack.

      Import Step 1 : Create a Directory directory in the program, and the control package lib folder Spire.Doc.jar file (see below) to the new directory.

     Import Step 2 : Right-click the jar file copy, select "Add as Library", the pop-up dialog box, click "OK", to complete the import.

Method 2 : introduced into the project by adding maven maven dependent, with reference to the import steps .


 

Java sample code

[Example 1 ] add annotations (text, picture)

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) {
         // Load Test document 
        the document DOC = new new the document ( "Test.docx" ); 

        // Get the paragraph 
        Section doc.getSections sec = () GET (0. ); 
        the paragraph para = sec.getParagraphs () GET (. 3. ); 

        // insert annotations to text 
        Comment comment = para.appendComment ( "include the following features in the experimental test samples recorded in the register, and fold weeks record report, observe for subsequent sampling." );
        . comment.getFormat () setAuthor ( "revision group" );
         // insert a picture into the comment 
        comment.getBody () addParagraph () appendPicture ( "tp.png.". ); 

        // save the document 
        doc.saveToFile ( "AddComment the .docx " , FileFormat.Docx_2010); 
    } 
}

Annotations add effects:

[Example 2 ] Reply to comment

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("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);
    }
}

批注回复效果:

 

【示例3】修改或替换批注

import com.spire.doc.*;

public class ModifyComment {
    public static void main(String[] args){
        //加载含有批注的测试文档
        Document doc = new Document("sample.docx");

        //获取第一个批注中的第一段,用文本替换原有批注中的文本
        doc.getComments().get(0).getBody().getParagraphs().get(0).replace("请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样。","参照以下实验方法!",false,false);
        //获取第一个批注中的第二段,用文本替换原有批注中的图片
        doc.getComments().get(0).getBody().getParagraphs().get(1).setText("请上报管理科!");

        //获取第一个批注中的第三段,删除原有图片,再调用方法添加新图片(用图片替换图片)
        doc.getComments().get(0).getBody().getParagraphs().get(2).getChildObjects().removeAt(0);
        doc.getComments().get(0).getBody().getParagraphs().get(2).appendPicture("2.png");

        //保存文档
        doc.saveToFile("ModifyComment.docx",FileFormat.Docx_2010);
    }
}

修改或替换结果:

 

【示例4】删除批注

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

public class DeleteComment{
    public static void main(String[] args) {
        //加载测试文档
        Document doc = new Document("AddComment.docx");

        //调用方法删除指定批注(删除批注中的所有内容)
        doc.getComments().removeAt(0);

        //删除指定批注中的指定段落(删除批注中的部分内容)
        doc.getComments().get(0).getBody().getParagraphs().get(1).getChildObjects().removeAt(0);

        //保存文档
        doc.saveToFile("DeleteComment", FileFormat.Docx_2010);
    }
}

批注删除效果:

 

(本文完)

 转载请注明出处!!!!!

Guess you like

Origin www.cnblogs.com/Yesi/p/10973873.html