Annotations are normally used to mentioned errors in documents or to discuss their content. With annotations, you can avoid long and never-ending discussions within emails threads. In this article, you will learn how to programmatically add and remove annotations to markup Word documents in Java.
The following are the topics discussed briefly below:
Java API to Annotate and Markup Word Files
GroupDocs.Annotation provides the Java API to deal with the annotations. The API allows the addition, removal, and extraction of annotations from Word documents and many other file formats. Supported document formats include; spreadsheets, presentations, images, PDF files, webpages, email messages, Visio drawings.
Download or Configure
Download the JAR file from the downloads section, or just get the latest repository and dependency configurations for the pom.xml of your maven-based Java applications.
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-annotation</artifactId>
<version>21.7.2</version>
</dependency>
Add Annotations to Word in Java
Let’s add different types of annotations to the Word documents. Annotations are of many types, so we will be covering only a few here.
Add Arrow Annotation to Word in Java
The following are the steps to add an arrow annotation to a Word document in Java.
- Load the document using the Annotator class.
- Initialize arrow annotation with ArrowAnnotation class.
- Adjust the position, size, page number of the arrow annotation.
- Add the created arrow annotation using the add() method.
- Save the annotated Word document to the path using appropriate save() method.
The following Java code sample shows how to add an arrow annotation to a Word document.
Insert Rectangle or Area Annotation to Word in Java
The following are the steps to add rectangle or area annotation to a DOC/DOCX document with some customizations. It is similar to adding Arrow annotations but it uses AreaAnnotation.
- Load the Word DOC/DOCX document using the Annotator class.
- Initialize rectangle annotation using AreaAnnotation class.
- Adjust the position, size, and color of the rectangle.
- Set other properties like page number, background, opacity, style, pen width, messages, and time.
- Add the created rectangle annotation to the Annotator.
- Save the annotated Word document to the path using the save() method.
The following Java code sample shows how to add rectangle/area annotation to a Word document.
Add Oval or Ellipse Annotation to Word in Java
The following are the steps to add oval/ellipse annotation to a document in Java.
- Load the DOC/DOCX document using the Annotator class.
- Initialize ellipse annotation using EllipseAnnotation class.
- Set the position and size of the initialized ellipse annotation.
- Add the created ellipse annotation to the Annotator.
- Save the annotated Word file using the appropriate save() method.
The following Java code sample shows how to add oval/ellipse annotation to any Word document.
Insert Distance Annotation to Word in Java
Likewise, you can mention the distance between two points using the distance annotation. The following are the steps to add distance annotation to the document in Java.
- After loading the Word document, initialize distance annotation using DistanceAnnotation class.
- Set the appearance of the annotation.
- Add the distance annotation to the Annotator object.
- Save the annotated document at the given location or use the right save() mehod,
The following Java code sample shows how to add distance annotation to a DOC/DOCX document.
Remove Annotations from Word DOC/DOCX files in Java
There are many ways to remove annotations from Word documents. You can remove specific annotations by providing the indexes to remove the specific annotations. Additionally, you can remove all the annotations at once. Details and Java source code for removing annotations are discussed in a separate article.
The following are the steps to remove all the annotations from a Word file.
- Load the document.
- Initialize saving options.
- Set the annotation type to None.
- Save the Word file. It will be free of annotations.
The following code shows how to remove annotations from a Word file in Java.
Conclusion
To sum up, you have learned how to add annotations to Word documents within Java applications. Specifically, we added arrow, ellipse, area, and distance annotations to the Word DOC/DOCX file using GroupDocs.Annotation for Java. Further, you have also seen how to remove all the annotations from any Word file. Now, you can try building your own document annotator Java application.
Learn more about the API from the documentation and the GitHub repository. For further queries, contact the support on the forum.