在 Word 文档中启用跟踪更改功能后,会记录文档中的所有编辑行为,例如插入、删除、替换和格式更改。对插入或删除的内容,可通过本文中介绍的方法来获取。
方法1
手动引入:将 Free Spire.Doc for Java 下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:
方法2
通过 Maven 仓库下载。如下配置pom.xml:
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();
}
}
获取结果:
—END—
手机扫一扫
移动阅读更方便
你可能感兴趣的文章