Hi. I am trying to compare 2 documents, save the changes to docx and pdf, and also a simple XML file that lists the change and some surrounding text, plus original text prior to the change. Is there a way to do this? Here is some code I’ve written in vb. OriginalText always seems to be the entire document:
Dim co As New CompareOptions()
co.IgnoreFormatting = req.WhiteSpace
co.IgnoreHeadersAndFooters = False
co.IgnoreCaseChanges = False
co.IgnoreComments = req.ComparisonType.ToLower.Contains("no annotation")
co.IgnoreFootnotes = False
co.IgnoreTables = req.IgnoreTables
co.IgnoreTextboxes = True
'ComparisonTargetType with IgnoreFormatting setting determines which document has to be used as formatting source for ranges of equal text.
Doc1.Compare(Doc2, "junk", DateTime.Now, co)
'If doc1 And doc2 are different, doc1 now has some revisions after the comparison, which can now be viewed And processed.
If req.AutoRev Then 'generate XML of revisions
'Dim rev As Revision
Dim writer As New XmlTextWriter(sFilePath & "\" & "CompareDetail.xml", System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = System.Xml.Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement("Revisions")
Dim cloneDoc As Aspose.Words.Document = Doc1.Clone(True)
Dim removerevision As ArrayList = New ArrayList()
For Each rev As Revision In cloneDoc.Revisions
If rev.ParentNode.NodeType = NodeType.Run And rev.RevisionType = RevisionType.Insertion Then
removerevision.Add(rev)
End If
Next
For Each revision As Revision In removerevision
revision.Reject()
Next
For Each item As Revision In cloneDoc.Revisions
Dim p As Node = item.ParentNode.ParentNode
If p IsNot Nothing Then
Dim body As Node = p.ParentNode
If body IsNot Nothing Then
Dim originaltext As String = p.ToString(SaveFormat.Text)
Dim newtext As String = item.ParentNode.ToString(SaveFormat.Text)
writer.WriteStartElement("Revision")
writer.WriteStartElement("RevisionType")
writer.WriteString(item.RevisionType.ToString("g"))
writer.WriteEndElement()
writer.WriteStartElement("OriginalText")
writer.WriteString(originaltext)
writer.WriteEndElement() 'OriginalText
writer.WriteStartElement("RevisionText")
writer.WriteString(newtext)
writer.WriteEndElement() 'RevisionText
writer.WriteEndElement() 'Revision
End If
End If
Next
writer.WriteEndElement() 'Revisions
writer.Flush()
writer.Close()
writer.Dispose()