DEV Community

Alexis
Alexis

Posted on

How to Turn On Track Changes, Accept or Reject Changes in Word in Java

The Track Changes function in Microsoft Word makes it possible for you to make tentative edits to a document and then you can choose to accept or reject later. Spire.Doc for java supports to operate the track changes feature from code in Java applications. This article will demonstrate the feature from the following three parts.

  • How to Turn On Track Changes in Word
  • Get All Revisions from Word
  • Accept or Reject Tracked Changes in Word

Install Spire.Doc for Java

Firstly, you need to add the Spire.Doc.jar file as a dependency in your Java application. You can download the Spire.Doc.Jar directly . If you use Maven, please use the following setting to get it.

<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>5.3.16</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

How to Turn On Track Changes in Word

Spire.Doc for Java offers document.setTrackChanges(boolean) method to turn on or turn off the track changes function. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Turn on the track changes function to records and changes using document.setTrackChanges(true) method.
  • Making some changes on the document by adding or removing the paragraphs.
  • Save the document to another file using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class setTrackchanges {
    public static void main(String[] args) throws Exception {

        //Load the sample document
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        //Turn on the track changes function
        document.setTrackChanges(true);

        //Insert a new paragraph
        Paragraph newPara = new Paragraph(document);
        TextRange tr= newPara.appendText("• Died: November 30, 1900 in Paris, France");
        tr.getCharacterFormat().setBold(true);
        document.getSections().get(0).getParagraphs().insert(3,newPara);

        //Remove a paragraph
        document.getSections().get(0).getParagraphs().removeAt(6);

        //Save the document to file
        document.saveToFile("Result.Docx",FileFormat.Docx);
    }
}
Enter fullscreen mode Exit fullscreen mode

Turn on Track Changes

Get All Revisions from Word

Spire.Doc for Java offfers Paragraph.getInsertRevision() method to detect if the paragraph is newly added or not and then EditRevision.getType() method to get the type.

  • Load the sample file using Document.loadFromFile() method.
  • Create a StringBuilder object and using StringBuilder.append() method to log data.
  • Traverse all the sections and every element under body in the section.
  • Determine if the paragraph is an insertion revision or not using Paragraph.isInsertRevision() method. If yes, use Paragraph.getInsertRevision() method to get the insertion revision. Then get the revision type and author using EditRevision.getType() method and EditRevision.getAuthor() method.
  • Determine if the paragraph is a delete revision or not using Paragraph.inDeleteRevision() method.
  • Traverse all the elements in the paragraphs to get the text ranges' revisions.
  • Write the content of StringBuilder to a txt document using FileWriter.write() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.revisions.*;

import java.io.FileWriter;

public class getRevisions {
    public static void main(String[] args) throws Exception {

        //Load file
        Document document = new Document();
        document.loadFromFile("Result.docx");

        StringBuilder insertRevision = new StringBuilder();
        insertRevision.append("Insert revisions:"+"\n");
        int index_insertRevision = 0;
        StringBuilder deleteRevision = new StringBuilder();
        deleteRevision.append("Delete revisions:"+"\n");
        int index_deleteRevision = 0;
        //Traverse sections
        for (Section sec : (Iterable<Section>) document.getSections())
        {
            //Iterate through the element under body in the section
            for(DocumentObject docItem : (Iterable<DocumentObject>)sec.getBody().getChildObjects())
            {
                if (docItem instanceof Paragraph)
                {
                    Paragraph para = (Paragraph)docItem;
                    //Determine if the paragraph is an insertion revision
                    if (para.isInsertRevision())
                    {
                        index_insertRevision++;
                        insertRevision.append("Index: " + index_insertRevision+"\n");
                        //Get insertion revision
                        EditRevision insRevison = para.getInsertRevision();

                        //Get insertion revision type
                        EditRevisionType insType = insRevison.getType();
                        insertRevision.append("Type: " + insType+"\n");
                        //Get insertion revision author
                        String insAuthor = insRevison.getAuthor();
                        insertRevision.append("Author: " + insAuthor + "\n");

                    }
                    //Determine if the paragraph is a delete revision
                    else if (para.isDeleteRevision())
                    {
                        index_deleteRevision++;
                        deleteRevision.append("Index: " + index_deleteRevision +"\n");
                        EditRevision delRevison = para.getDeleteRevision();
                        EditRevisionType delType = delRevison.getType();
                        deleteRevision.append("Type: " + delType+ "\n");
                        String delAuthor = delRevison.getAuthor();
                        deleteRevision.append("Author: " + delAuthor + "\n");
                    }
                    //Iterate through the element in the paragraph
                    for(DocumentObject obj : (Iterable<DocumentObject>)para.getChildObjects())
                    {
                        if (obj instanceof TextRange)
                        {
                            TextRange textRange = (TextRange)obj;
                            //Determine if the textrange is an insertion revision
                            if (textRange.isInsertRevision())
                            {
                                index_insertRevision++;
                                insertRevision.append("Index: " + index_insertRevision +"\n");
                                EditRevision insRevison = textRange.getInsertRevision();
                                EditRevisionType insType = insRevison.getType();
                                insertRevision.append("Type: " + insType + "\n");
                                String insAuthor = insRevison.getAuthor();
                                insertRevision.append("Author: " + insAuthor + "\n");
                            }
                            else if (textRange.isDeleteRevision())
                            {
                                index_deleteRevision++;
                                deleteRevision.append("Index: " + index_deleteRevision +"\n");
                                //Determine if the textrange is a delete revision
                                EditRevision delRevison = textRange.getDeleteRevision();
                                EditRevisionType delType = delRevison.getType();
                                deleteRevision.append("Type: " + delType+"\n");
                                String delAuthor = delRevison.getAuthor();
                                deleteRevision.append("Author: " + delAuthor+"\n");
                            }
                        }
                    }
                }
            }
        }

        //Save to a .txt file
        FileWriter writer1 = new FileWriter("insertRevisions.txt");
        writer1.write(insertRevision.toString());
        writer1.flush();
        writer1.close();


        //Save to a .txt file
        FileWriter writer2 = new FileWriter("deleteRevisions.txt");
        writer2.write(deleteRevision.toString());
        writer2.flush();
        writer2.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Get all revisions

Accept or Reject Tracked Changes in Word

Spire.Doc for Java offers document.acceptChanges() method to accept all the revisions and document.rejectChanges() method to remove all the revisions.

  • Create a Document instance and load a sample Word document using Document.loadFromFile() method.
  • Accept all changes in the document using Document.acceptChanges() method.
  • Reject all changes in the document using Document.rejectChanges() method.
  • Save the document to another file using Document.saveToFile() method.
import com.spire.doc.*;

public class acceptChanges {
    public static void main(String[] args) throws Exception {

        //Load file
        Document document = new Document();
        document.loadFromFile("Result.docx");

        if (document.hasChanges()) 
            //Accept all changes
            document.acceptChanges();

            //Reject the changes
            //doc.rejectChanges();

    //Save the  document to file
        document.saveToFile("Acceptchangs.docx",FileFormat.Docx_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Accept Changes

Oldest comments (0)