In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java how to get all the insert and delete revisions in Word" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, I hope that this "how to get all the insert and delete revisions in Word" article can help you solve the problem.
When you enable tracking changes in an Word document, all editing behaviors in the document, such as inserts, deletions, replacements, and format changes, are recorded. The content inserted or deleted can be obtained by the method described in this article.
Introduce Jar method 1
Manual introduction: download Free Spire.Doc for Java locally, extract it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA and introduce the jar file in the local path into the Java program:
Method 2
Download through the Maven warehouse. Configure pom.xml as follows:
Com.e-iceblue https://repo.e-iceblue.cn/repository/maven-public/e-icebluespire.doc.free5.2.0 gets inserted and deleted revisions
Create an instance of Document and load a sample Word document using the Document.loadFromFile () method.
Create a StringBuilder object and record the data using the StringBuilder.append () method.
Iterate through every element under body in all Section and 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 inserted 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.
Iterate through all the elements in the paragraph to get a revision of the text range.
Use the FileWriter.write () method to write the contents of 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 = 0X StringBuilder / create a StringBuilder object to get the delete revision StringBuilder deleteRevision = new StringBuilder (); deleteRevision.append ("DELETE REVISIONS:" + "\ n"); int index_deleteRevision = 0 / / traversing all sections for (Section sec: (Iterable) document.getSections ()) {/ / traversing the element for (DocumentObject docItem: (Iterable) sec.getBody (). GetChildObjects ()) {if (docItem instanceof Paragraph) {Paragraph para = (Paragraph) docItem;// in section to determine whether the paragraph inserts revision if (para.isInsertRevision ()) {index_insertRevision++;insertRevision.append ("Index:" + index_insertRevision+ "\ n"). / / get insert revision EditRevision insRevison = para.getInsertRevision (); / / get inserted paragraph text content String insertRevisionString = para.getText (); / / get insert revision type EditRevisionType insType = insRevison.getType (); insertRevision.append ("Type:" + insType + "\ n"); / / get insert revision author String insAuthor = insRevison.getAuthor (); insertRevision.append ("Author:" + insAuthor + "\ n" + "InsertPara:" + insertRevisionString) } / / determine whether the paragraph is a deletion revision 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 for (DocumentObject obj: (Iterable) para.getChildObjects ()) {if (obj instanceof TextRange) {TextRange textRange = (TextRange) obj;// in the paragraph to 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 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") } / / determine whether the text range is an inserted revision, and get the type, author, and text content of the inserted revision. 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) } / / Save insert revision content as txt file FileWriter writer1 = new FileWriter ("insertRevisions.txt"); writer1.write (insertRevision.toString ()); writer1.flush (); writer1.close (); / / Save delete revision content as txt file FileWriter writer2 = new FileWriter ("deleteRevisions.txt"); writer2.write (deleteRevision.toString ()); writer2.flush (); writer2.close ();}}
Get the result:
This is the end of the introduction to "how Java gets all the insert and delete revisions in Word". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.