前面我们介绍了如何比较两个word文档的内容。从spire.doc v8.12.14始,我们支持获取两个 word 文档之间比较出的差异并返回list. 该文将介绍如何获取两个word文档之间比较出的差异。
c#
using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
using spire.doc.formatting.revisions;
using system;
namespace getworddifferences
{
class program
{
static void main(string[] args)
{
//加载第一个word文档
document doc1 = new document();
doc1.loadfromfile("sample1.docx");
//加载第二个word文档
document doc2 = new document();
doc2.loadfromfile("sample2.docx");
//比较文档
doc1.compare(doc2, "author");
foreach (section sec in doc1.sections)
{
foreach (documentobject docitem in sec.body.childobjects)
{
if (docitem is paragraph)
{
paragraph para = docitem as paragraph;
if (para.isinsertrevision)
{
editrevision insrevison = para.insertrevision;
editrevisiontype instype = insrevison.type;
string insauthor = insrevison.author;
datetime insdatetime = insrevison.datetime;
}
else if (para.isdeleterevision)
{
editrevision delrevison = para.deleterevision;
editrevisiontype deltype = delrevison.type;
string delauthor = delrevison.author;
datetime deldatetime = delrevison.datetime;
}
foreach (paragraphbase paraitem in para.childobjects)
{
if (paraitem.isinsertrevision)
{
editrevision insrevison = paraitem.insertrevision;
editrevisiontype instype = insrevison.type;
string insauthor = insrevison.author;
datetime insdatetime = insrevison.datetime;
}
else if (paraitem.isdeleterevision)
{
editrevision delrevison = paraitem.deleterevision;
editrevisiontype deltype = delrevison.type;
string delauthor = delrevison.author;
datetime deldatetime = delrevison.datetime;
}
}
}
}
}
//获取差异
differrevisions differrevisions = new differrevisions(doc1);
var insetrevisionslist = differrevisions.insertrevisions;
var deletrevisionslist = differrevisions.deleterevisions;
}
}
}
vb.net
imports spire.doc
imports spire.doc.documents
imports spire.doc.fields
imports spire.doc.formatting.revisions
imports system
namespace getworddifferences
class program
private shared sub main(byval args() as string)
'加载第一个文档
dim doc1 as document = new document
doc1.loadfromfile("sample1.docx")
'加载第二个文档
dim doc2 as document = new document
doc2.loadfromfile("sample2.docx")
'比较两个文档
doc1.compare(doc2, "author")
for each sec as section in doc1.sections
for each docitem as documentobject in sec.body.childobjects
if (typeof docitem is paragraph) then
dim para as paragraph = ctype(docitem,paragraph)
if para.isinsertrevision then
dim insrevison as editrevision = para.insertrevision
dim instype as editrevisiontype = insrevison.type
dim insauthor as string = insrevison.author
dim insdatetime as datetime = insrevison.datetime
elseif para.isdeleterevision then
dim delrevison as editrevision = para.deleterevision
dim deltype as editrevisiontype = delrevison.type
dim delauthor as string = delrevison.author
dim deldatetime as datetime = delrevison.datetime
end if
for each paraitem as paragraphbase in para.childobjects
if paraitem.isinsertrevision then
dim insrevison as editrevision = paraitem.insertrevision
dim instype as editrevisiontype = insrevison.type
dim insauthor as string = insrevison.author
dim insdatetime as datetime = insrevison.datetime
elseif paraitem.isdeleterevision then
dim delrevison as editrevision = paraitem.deleterevision
dim deltype as editrevisiontype = delrevison.type
dim delauthor as string = delrevison.author
dim deldatetime as datetime = delrevison.datetime
end if
next
end if
next
next
'获取差异
dim differrevisions as differrevisions = new differrevisions(doc1)
dim insetrevisionslist = differrevisions.insertrevisions
dim deletrevisionslist = differrevisions.deleterevisions
end sub
end class
end namespace