Java Get all inserted and deleted revisions in Word

When Track Changes is enabled in a Word document, all editing actions in the document, such as insertions, deletions, substitutions, and formatting changes, are logged. For inserted or deleted content, the method described in this article can be used to obtain it.

Import Jar

method 1

Manual introduction:  Download Free Spire.Doc for Java  locally, unzip it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:

Method 2

Download from  Maven  repository. Configure pom.xml as follows:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Get inserted, deleted revisions

  • Create a  Document  instance and use the  Document.loadFromFile()  method to load a sample Word document.
  • Create a  StringBuilder  object, and then use the  StringBuilder.append()  method to record the data.
  • Traverse all  Sections  and every element under body in the section.
  • Use the  Paragraph.isInsertRevision()  method to determine whether a paragraph is an insert revision. If yes, use the  Paragraph.getInsertRevision()  method to get the inserted revision. Then use  EditRevision.getType()  method and  EditRevision.getAuthor()  method to get revision type and author.
  • Use the  Paragraph.inDeleteRevision()  method to determine whether a paragraph is a delete revision. If yes, use the  Paragraph.getDeleteRevision()  method to get the delete revision. Then use  EditRevision.getType()  method and  EditRevision.getAuthor()  method to get revision type and author.
  • Iterates over all elements in a paragraph to get revisions for a range of text.
  • Use the  FileWriter.write()  method to write the contents of the  StringBuilder  to the txt document.

Java

import com.spire.doc.*; 
import com.spire.doc.documents.Paragraph; 
import com.spire.doc.fields.TextRange; 
import com.spire.doc.formatting.revisions.EditRevision; 
import com.spire.doc .formatting.revisions.EditRevisionType; 

import java.io.FileWriter; 

public class GetAllRevisions { 
    public static void main(String[] args)throws Exception { 
        //Load sample Word document 
        Document document = new Document(); 
        document.loadFromFile(" test.docx"); 

        //Create a StringBuilder object to get the insert revision 
        StringBuilder insertRevision = new StringBuilder(); 
        insertRevision.append("INSERT REVISIONS:"+"\n"); 
        int index_insertRevision = 0;

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

        //Iterate through all sections 
        for (Section sec : ( Iterable<Section>) document.getSections()) 
        { 
            // Traverse the elements under the body in the section 
            for(DocumentObject docItem : (Iterable<DocumentObject>)sec.getBody().getChildObjects()) 
            { 
                if (docItem instanceof Paragraph) 
                { 
                    Paragraph para = (Paragraph)docItem; 
                    //Determine whether the paragraph is an insert revision 
                    if (para.isInsertRevision())
                    { 
                        String insAuthor = insRevison.getAuthor();
                        index_insertRevision++; 
                        insertRevision.append("Index: " + index_insertRevision + " \n"); 
                        //Get the insert revision 
                        EditRevision insRevision = para.getInsertRevision(); 

                        //Get the text content of the inserted paragraph 
                        String insertRevisionString = para.getText(); 

                        //Get the insert revision type 
                        EditRevisionType insType = insRevision.getType(); 

                        insertRevision.append("Type: " + insType + " \n"); 
                        //Get the insert revision author 
                        insertRevision.append("Author: " + insAuthor + " \n" + "InsertPara:"+ insertRevisionString );

                    } 

                    //Determine whether the paragraph is a delete revision 
                     if (para.isDeleteRevision()) 
                    { 
                        index_deleteRevision++; 
                        deleteRevision.append("Index: " + index_deleteRevision + " \n"); 
                        EditRevision delRevision = para.getDeleteRevision(); 
                        EditRevisionType delType = delRevison.getType();
                        deleteRevision.append("Type: " + delType + " \n");
                        String delAuthor = delRevison.getAuthor();
                        deleteRevision.append( "Author: " + delAuthor + " \n"); 
                    } 
                    // Traverse the elements in the paragraph 
                    for(DocumentObject obj : (Iterable<DocumentObject>)para.getChildObjects())
                    {
                        if (obj instanceof TextRange)
                        { 
                            TextRange textRange = (TextRange)obj ; 

                            // Determine whether the text range is a deleted revision, and get the type, author and deleted text content of the deleted revision. 
                            if (textRange.isDeleteRevision()) 
                            { 
                                index_deleteRevision++; 
                                deleteRevision.append("Index: " + index_deleteRevision +" \n");
                                EditRevision delRevision = textRange.getDeleteRevision(); 
                                EditRevisionType delType = delRevision.getType(); 
                                deleteRevision.append("Type: " + delType+ " \n"); 
                                String delAuthor = delRevision.getAuthor(); 
                                deleteRevision.append("Author: " + delAuthor + " \n"); 
                                String deletetext = textRange.getText(); 
                                deleteRevision.append("Delete text:" + deletetext +" \n"); 
                            } 

                            //Determine whether the text range is an insert revision, and get Insert revision type, author, and text content.
                            else 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 = insRevision.getAuthor(); 
                                insertRevision.append("Author: " + insAuthor + " \n"); 
                                String insertText = textRange.getText(); 
        FileWriter writer2 = new FileWriter(" deleteRevisions.txt"); 
        writer2.write(deleteRevision.toString());
                                insertRevision.append("insertText:"+insertText);
                            }
                        }
                    } 
                } 
            } 
        } 
        //Save the inserted revisions as a txt file 
        FileWriter writer1 = new FileWriter("insertRevisions.txt"); 
        writer1.write(insertRevision.toString()); 
        writer1.flush(); 
        writer1.close(); 

        / /Save the deleted revision as a txt file 
        writer2.flush(); 
        writer2.close(); 
    } 
}

Get the result:

—END—

Guess you like

Origin blog.csdn.net/Trouvailless/article/details/124295810