亲宝软件园·资讯

展开

获取Word插入和删除修订

Tobemia 人气:0

在 Word 文档中启用跟踪更改功能后,会记录文档中的所有编辑行为,例如插入、删除、替换和格式更改。对插入或删除的内容,可通过本文中介绍的方法来获取。

​​引入Jar​​

​​方法1​​

手动引入:将Free Spire.Doc for Java下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:​

Java 获取Word中的所有插入和删除修订_java

​​方法2​​

通过Maven仓库下载。如下配置pom.xml:

<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>

​​获取插入、删除的修订​​

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();
}
}

获取结果:

Java 获取Word中的所有插入和删除修订_Word_02

加载全部内容

相关教程
猜你喜欢
用户评论