Java: Get All Revisions from Word

After you enable the Track Changes feature in a Word document, it records all the edits in the document, such as insertions, deletions, replacements, and format changes. Track Changes is a great feature allowing you to see what changes have been made to a document.  This tutorial shows how to get all revisions from a Word document by 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>
    

Get All Revisions from Word

The detailed steps are as follows.

  • Create a Document instance and load a sample Word document using Document.loadFromFile() method.
  • Create a StringBuilder object and then 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. If yes, use Paragraph.getDeleteRevision() method to get the delete revision. Then get the revision type and author using EditRevision.getType() method and EditRevision.getAuthor() 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.
  • Java
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 the sample Word document
        Document document = new Document();
        document.loadFromFile("test file.docx");

        //Create a StringBuilder object to get the insertions 
        StringBuilder insertRevision = new StringBuilder();
        insertRevision.append("Insert revisions:"+"\n");
        int index_insertRevision = 0;

        //Create a StringBuilder object to get the deletions
        StringBuilder deleteRevision = new StringBuilder();
        deleteRevision.append("Delete revisions:"+"\n");
        int index_deleteRevision = 0;

        //Traverse all the 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();
    }
}

Java: Get All Revisions from Word

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.