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;
public class AddCommentToCharacters {
public static void mainString[] args) {
//加载测试文档
Document doc = new Document);
doc.loadFromFile”test.docx”);
//查找指定字符串
TextSelection[] selections = doc.findAllString”皱状厚膜”, true, false);
//获取关键字符串所在段落
Paragraph para = selections[0].getAsOneRange).getOwnerParagraph);
int index = para.getChildObjects).indexOfselections[0].getAsOneRange));
//添加批注ID
CommentMark start = new CommentMarkdoc);
start.setCommentId1);
start.setTypeCommentMarkType.Comment_Start);
CommentMark end = new CommentMarkdoc);
end.setTypeCommentMarkType.Comment_End);
end.setCommentId1);
//添加批注内容
String str = “给指定字符串添加批注”;
Comment comment = new Commentdoc);
comment.getFormat).setCommentId1);
comment.getBody).addParagraph).appendTextstr);
comment.getFormat).setAuthor”作者:”);
comment.getFormat).setInitial”CM”);
para.getChildObjects).insertindex, start);
para.getChildObjects).insertindex + 1, selections[0].getAsOneRange));
para.getChildObjects).insertindex + 2,end);
para.getChildObjects).insertindex + 3, comment);
//保存文档
doc.saveToFile”字符串批注.docx”,FileFormat.Docx_2013);
doc.dispose);
}
}