Track Changes is a built-in feature in Microsoft Word which allows you to see all changes that were made to the document, and you can decide whether to accept or reject those changes. It is very useful especially when you are collaborating with multiple people on the same contracts or school assignments. In this article, you will learn how to programmatically accept or reject all tracked changes in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Accept All Tracked Changes in a Word document
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Accept all changes in the document using Document.acceptChanges() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class AcceptTrackedChanges { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load the sample Word document doc.loadFromFile("test file.docx"); //Accept all changes in the document doc.acceptChanges(); //Save the document doc.saveToFile("AcceptAllChanges.docx", FileFormat.Docx); } }
Reject All Tracked Changes in a Word document
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Reject all changes in the document using Document.rejectChanges() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RejectTrackedChanges { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load the sample Word document doc.loadFromFile("test file.docx"); //Reject all changes in the document doc.rejectChanges(); //Save the document doc.saveToFile("RejectAllChanges.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.