Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add, reply, modify and delete Word annotations by Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "Java how to add, reply, modify, delete Word comments", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to add, reply, modify, delete Word annotations" bar!

Annotation is a tool or method commonly used to annotate the content of a specific document, which plays the role of explanation, marking and correction. In this article, you will show how to manipulate Word annotations, including:

1. Add comments: add text to comments, insert pictures to comments

1.1 add comments to the specified paragraph

1.2 add an annotation to the specified string

two。 Reply to an annotation

3. Modify or replace annotations: replace text content in annotations with text, pictures in annotations with text, pictures in annotations with pictures

4. Delete comment: deletes all content in the specified annotation, deletes the specified content in the specified annotation

Using tools: Free Spire.Doc for Java (free version)

Jar file acquisition and import:

Method 1: obtain the jar package through the official website and decompress it. After decompressing, import the Spire.Doc.jar file under the lib folder into the java program.

Method 2: by adding maven dependency import, refer to the import steps.

Java sample code

[example 1] add annotations to paragraphs (text, pictures)

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 the test document Document doc = new Document ("test.docx"); / / get the specified paragraph Section sec = doc.getSections () .get (0); Paragraph para= sec.getParagraphs () .get (3) / / insert the text into the annotation Comment comment = para.appendComment ("Please record the experimental samples with the following characteristics in the experiment and organize the weekly record report for follow-up observation and sampling.") ; comment.getFormat () .setAuthor ("revision group"); / / insert a picture into the annotation comment.getBody (). AddParagraph (). AppendPicture ("tp.png"); / / Save document doc.saveToFile ("AddComment.docx", FileFormat.Docx_2010);}}

Annotations add effect:

[example 2] add annotations to the specified string

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 main (String [] args) {/ / load test document Document doc = new Document (); doc.loadFromFile ("test.docx") / / find the specified string TextSelection [] selections = doc.findAllString ("wrinkled thick film", true, false); / / get the paragraph Paragraph para = selections [0] .getAsOneRange (). GetOwnerParagraph (); int index = para.getChildObjects (). IndexOf (selections [0] .getAsOneRange ()); / / add the annotation ID CommentMark start = new CommentMark (doc) Start.setCommentId (1); start.setType (CommentMarkType.Comment_Start); CommentMark end = new CommentMark (doc); end.setType (CommentMarkType.Comment_End); end.setCommentId (1); / / add annotation content String str = "add annotations to the specified string"; Comment comment = new Comment (doc); comment.getFormat (). SetCommentId (1) Comment.getBody (). AddParagraph (). AppendText (str); comment.getFormat (). SetAuthor ("author:"); comment.getFormat (). SetInitial ("CM"); para.getChildObjects (). Insert (index, start); para.getChildObjects (). Insert (index + 1, selections [0] .getAsOneRange ()); para.getChildObjects (). Insert (index + 2 grade end) Para.getChildObjects () .insert (index + 3, comment); / / Save document doc.saveToFile ("string annotation .docx", FileFormat.Docx_2013); doc.dispose ();}}

Annotations add effect:

[example 3] reply to comments

Import com.spire.doc.*; import com.spire.doc.fields.Comment; public class ReplyComment {public static void main (String [] args) throws Exception {/ / load test document Document doc = new Document ("AddComment.docx"); / / get the specified annotation Comment comment = doc.getComments () .get (0); / / reply annotation Comment relyC= new Comment (doc) RelyC.getFormat (). SetAuthor ("experimental group"); relyC.getBody (). AddParagraph (). AppendText ("completed.") ; comment.replyToComment (relyC); / / Save document doc.saveToFile ("ReplyComment.docx", FileFormat.Docx_2010);}}

Comment reply effect:

[example 4] modify or replace annotations

Import com.spire.doc.*; public class ModifyComment {public static void main (String [] args) {/ / load the test document Document doc = new Document ("sample.docx") with annotations / / get the first paragraph of the first comment and replace the text in the original note with the text doc.getComments (). Get (0). GetBody (). GetParagraphs (). Get (0). Replace ("Please record the experimental samples with the following characteristics in the experiment, and organize the weekly record report for follow-up observation and sampling." , "refer to the following experimental method!", false,false); / / get the second paragraph in the first comment and replace the picture doc.getComments (). Get (0). GetBody (). GetParagraphs (). Get (1). SetText ("Please report to the Management Section!") ; / / get the third paragraph in the first comment, delete the original picture, and then call the method to add a new picture (replace the picture with a picture) doc.getComments (). Get (0). GetBody (). GetParagraphs (). Get (2). GetChildObjects (). RemoveAt (0); doc.getComments (). Get (0). GetBody (). GetParagraphs (). Get (2). AppendPicture ("2.png") / / Save the document doc.saveToFile ("ModifyComment.docx", FileFormat.Docx_2010);}}

Modify or replace the result:

[example 5] Delete comments

Import com.spire.doc.*; import com.spire.doc.FileFormat; public class DeleteComment {public static void main (String [] args) {/ / load the test document Document doc = new Document ("AddComment.docx"); / / call the method to delete the specified annotation (delete everything in the annotation) doc.getComments () .removeAt (0) / delete the specified paragraph in the specified comment (delete part of the comment) doc.getComments (). Get (0). GetBody (). GetParagraphs (). Get (1). GetChildObjects (). RemoveAt (0); / / Save document doc.saveToFile ("DeleteComment", FileFormat.Docx_2010);}}

Comment deletion effect:

Thank you for your reading, the above is the "Java how to add, reply, modify, delete Word annotations" content, after the study of this article, I believe you on how to add, reply, modify, delete Word annotations of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report