详解Spring获取配置的三种方式
332
2022-08-06
Java 获取Word中所有的插入和删除修订的方法
目录引入Jar方法1方法2获取插入、删除的修订
在 Word 文档中启用跟踪更改功能后,会记录文档中的所有编辑行为,例如插入、删除、替换和格式更改。对插入或删除的内容,可通过本文中介绍的方法来获取。
引入Jar
方法1
手动引入:将Free Spire.Doc for java下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:
方法2
通过Mahttp://ven仓库下载。如下配置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中body下的元素
for(DocumentObject docItem : (Iterable
{
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
{
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: iowWMzGX" + 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();
}
}
获取结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~