Home >Java >javaTutorial >How to get all inserted and deleted revisions in Word using Java
When the Track Changes feature is enabled in a Word document, all editing actions in the document, such as insertions, deletions, substitutions, and formatting changes, are recorded. Inserted or deleted content can be obtained through the method described in this article.
Manual introduction: Download Free Spire.Doc for Java locally, unzip it, and find it in the lib folder Spire.Doc.jar file. Open the following interface in IDEA and introduce the jar file in the local path into the Java program:
Download through the 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>
Create a Document instance and use Document .loadFromFile() method loads a sample Word document.
Create a StringBuilder object, and then use the StringBuilder.append() method to record the data.
Traverse all Section and every element under the body in the section.
Use the Paragraph.isInsertRevision() method to determine whether a paragraph is an inserted revision. If so, use the Paragraph.getInsertRevision() method to get the insert revision. Then use the EditRevision.getType() method and the EditRevision.getAuthor() method to get the revision type and author.
Use the Paragraph.inDeleteRevision() method to determine whether a paragraph is a deleted revision. If so, use the Paragraph.getDeleteRevision() method to get the delete revision. Then use the EditRevision.getType() method and the EditRevision.getAuthor() method to get the revision type and author.
Traverse all elements in a paragraph to get revisions for a text range.
Use the FileWriter.write() method to write the contents of StringBuilder to a 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 { //加载示例 Word 文档 Document document = new Document(); document.loadFromFile("test.docx"); //创建一个 StringBuilder 对象以获取插入修订 StringBuilder insertRevision = new StringBuilder(); insertRevision.append("INSERT REVISIONS:"+"\n"); int index_insertRevision = 0; //创建一个 StringBuilder 对象以获取删除修订 StringBuilder deleteRevision = new StringBuilder(); deleteRevision.append("DELETE REVISIONS:"+"\n"); int index_deleteRevision = 0; //遍历所有节 for (Section sec : (Iterable<Section>) document.getSections()) { //遍历section中body下的元素 for(DocumentObject docItem : (Iterable<DocumentObject>)sec.getBody().getChildObjects()) { if (docItem instanceof Paragraph) { Paragraph para = (Paragraph)docItem; //确定段落是否为插入修订 if (para.isInsertRevision()) { index_insertRevision++; insertRevision.append("Index: " + index_insertRevision + " \n"); //获取插入修订 EditRevision insRevison = para.getInsertRevision(); //获取插入的段落文本内容 String insertRevisionString = para.getText(); //获取插入修订类型 EditRevisionType insType = insRevison.getType(); insertRevision.append("Type: " + insType + " \n"); //获取插入修订作者 String insAuthor = insRevison.getAuthor(); insertRevision.append("Author: " + insAuthor + " \n" + "InsertPara:"+ insertRevisionString ); } //确定段落是否为删除修订 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"); } //遍历段落中的元素 for(DocumentObject obj : (Iterable<DocumentObject>)para.getChildObjects()) { if (obj instanceof TextRange) { TextRange textRange = (TextRange)obj; //确定文本范围是否为删除修订,并获取删除修订的类型、作者及删除的文本内容。 if (textRange.isDeleteRevision()) { index_deleteRevision++; deleteRevision.append("Index: " + index_deleteRevision +" \n"); EditRevision delRevison = textRange.getDeleteRevision(); EditRevisionType delType = delRevison.getType(); deleteRevision.append("Type: " + delType+ " \n"); String delAuthor = delRevison.getAuthor(); deleteRevision.append("Author: " + delAuthor + " \n"); String deletetext = textRange.getText(); deleteRevision.append("Delete text:" + deletetext +" \n"); } //确定文本范围是否为插入修订,并获取插入修订的类型、作者及文本内容。 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 = insRevison.getAuthor(); insertRevision.append("Author: " + insAuthor + " \n"); String insertText = textRange.getText(); insertRevision.append("insertText:"+insertText); } } } } } } //保存插入修订内容为txt 文件 FileWriter writer1 = new FileWriter("insertRevisions.txt"); writer1.write(insertRevision.toString()); writer1.flush(); writer1.close(); //保存删除修订内容为txt 文件 FileWriter writer2 = new FileWriter("deleteRevisions.txt"); writer2.write(deleteRevision.toString()); writer2.flush(); writer2.close(); } }
Get results:
The above is the detailed content of How to get all inserted and deleted revisions in Word using Java. For more information, please follow other related articles on the PHP Chinese website!